To explain your question, look at the example below.
struct Person
{
public Person(int age)
{
this.Age = age + 10;
}
public int Age;
}
And then access it like
Person person =new Person();
person.Age = 10;
Person person1 = new Person(10);
What you expect Person's age is 10, but Person1's value will be 20. Using the
new
key word you can call a specific constructor. For your second case you are calling a default construtor which assign a 0 to the variable and then you are assigning value to it. In the first case the int will accept a compatible value which implicitly to the value type.
For the int, the both ways you are calling are same, because you are using simply a default constructor.
Hope this helps.
Note: value types are different from reference types In the above example if the person is a
class
then
Person1= Person; Person1.Age=30; then Person.Age also equals 30
. But in case of value type Person.Age will have the original value. Value types stores on the stack and a new variable will be in a new memory address. Reference variable pointers are in the stack which points to an address in the heap. So two variable can hold a common address in the heap.
Good luck