Just curious, what is the difference between:
int A = 100;
and
int A = new int();
I know new is used to allocate memory on the heap..but I really do not get the context here.
Just curious, what is the difference between:
int A = 100;
and
int A = new int();
I know new is used to allocate memory on the heap..but I really do not get the context here.
static void Main()
{
int A = new int();
int B = default(int);
int C = 100;
Console.Read();
}
Is compiled to
.method private hidebysig static void Main() cil managed
{
.entrypoint
// Code size 15 (0xf)
.maxstack 1
.locals init ([0] int32 A,
[1] int32 B,
[2] int32 C)
IL_0000: nop
IL_0001: ldc.i4.0
IL_0002: stloc.0
IL_0003: ldc.i4.0
IL_0004: stloc.1
IL_0005: ldc.i4.s 100
IL_0007: stloc.2
IL_0008: call int32 [mscorlib]System.Console::Read()
IL_000d: pop
IL_000e: ret
} // end of method Program::Main
As you can see first one just initialize it and second one is just the same and third one initialize and set to 100
. As for the IL code generated, they both get initialized in a single line.
so
int A = new int();
Is the same as
int A = default(int);
Difference?
the latter ends with A being 0, not 100.
When?
Pretty much never. Maybe in some generated code it is simpler to use the new TypeName()
syntax, but default(TypeName)
would be preferred even then.
And new
does not "allocate memory on the heap". It initializes an instance; that is all.
I strongly recommend that you read this article from dotnet mob: http://codaffection.blogspot.in/2015/02/difference-between-declarations-int-i.html
int A = 100; //Initialises A to 100 manually
Suppose you declare an integer as
int A;
For further operations on A
, you need to initialise A
with an integer value, otherwise the compiler shows
use of unassigned local variable
To declare A
as follows
int A = new int(); //A initialised to 0 (default value of int)
allows for further operations on A without a manual initialisation - I think you get my point now.
Now let's discuss using new ()
.
Use of new
doesn't mean memory will be allocated on the heap; if A
is a local variable (e.g. in a method) it will be allocated memory on the stack.
If A is a member of a class however, then memory will be allocated on the heap when an instance of the class is created.
Point to be remembered: if A is static, it will always be on stack memory!
int A=100;
Allocates an int on the stack and sets its value to 100.
int A=new int();
Allocates an int on the stack (yes, value types are always allocated on the stack, even with the new
keyword) and sets its value to the default, that is, 0.
I saw it from another link:
MSDN says "The new operator is also used to invoke the default constructor for value types."
Inside a method:
int x;
: allocates an int on stack and does not initialize it.int x=5;
: allocates an int on stack and sets it to 5;int x=new int();
: allocates an int on stack and sets it to 0;int x=new int(5);
: does not compile.myInt = new int(); // Invoke default constructor for int type.
This statement is equivalent to the following statement:
myInt = 0; // Assign an initial value, 0 in this example.
Using the new operator calls the default constructor of the specific type and assigns the default value to the variable. The default value of an integer is 0 BTW.
The difference is that you can't initialize and set anything but the default value value using the new operator.
I don't think it is necessary to add new on int(because int has default memory size).. new is used when we have a user-defined class of which we instantiate an object..
public static Main()
{
Cat cat1=new Cat();
}
Public class Cat
{
public bool Sex
public string Name
}
Now compiler will allocate that much memory to cat1(memory size which is defined in class Cat)
If you are using the int as a field in your class, you might even say simply int x;
- since int is a value type it cannot be null and therefore gets its default value - i.e. 0.
I end up discovering my problem
I was modifying the values but not the Object
This is the proper way to do it :
public ActionResult ChangeDefaultUserLockingSetting(int PasswordAttempts, int DefaultLockingTime)
{
var defaultAccountSettings = new DefaultAccountSettingsDataContext();
var accountSettings = defaultAccountSettings.DefaultAccountSettings.First(u => u.id == 1);
accountSettings.DefaultAccountLockoutTimeSpan = DefaultLockingTime;
accountSettings.MaxFailedAccessAttemptsBeforeLockout = PasswordAttempts;
defaultAccountSettings.SubmitChanges();
return View("Index", loadAdministrationViewModel());
}
"int" is a value type, so neither expression will allocate memory on the managed heap. The first statement will initialize A to the literal you specify. The second statement will leave A at a default value.