The difference between A a = new A () and A a = null

In C #,

A a = new A();
A a = null;
A a;

How do these 3 lines work in relation to memory?

I know that the first line will create memory on the heap, but what about the other two lines?

How does it work if, A a; is a field and local variable.

+4
source share
6 answers
  • Creates a new instance Aand assigns it to a variable A.
  • Doing nothing. It just assigns a nulllink A. If Anot used, the compiler can optimize it.
  • . A a = default(A);, , 2, default(A) - null. , . , .
+4

A a = new A(); A. a - . a , .

A a = null; - .

A a; , , A a = null; - EDIT OP, .

+1

, A a - ,

  class MyClass {
    ...
    // A a is field of some class/structure
    A a = new A(); // A a = null; or A a;
    ...
  }

  A a = new A();

- a a a ; ( a a null):

  A a = null; 
  A a;

" , , "

https://msdn.microsoft.com/en-us/library/aa645756(v=vs.71).aspx

, .

  public void MyMethod() {
    ...
    // A a is a local variable in some method
    A a = new A(); // A a = null; or A a;
    ...
  }

https://msdn.microsoft.com/en-us/library/4y7h161d(v=vs.71).aspx

  A a = new A(); // "a" of type "A" declaration with new instance of A as an initial value
  A a = null;    // "a" of type "A" declaration with null initial value
  A a;           // just "a" declaration, "a" contains trash and should be initialized before using
+1

, A :

A a = new A(); A .

A a = null; A a; null A.

IL, A a = null; A a;

:

static void Main()
{
    string s;

    if (Environment.TickCount > 0)
        s = "A";
    else
        s = "B";

    Console.WriteLine(s);
}

IL, , :

.method private hidebysig static void Main() cil managed
{
    .entrypoint
    .maxstack 2
    .locals init (
        [0] string s)
    L_0000: call int32 [mscorlib]System.Environment::get_TickCount()
    L_0005: ldc.i4.0 
    L_0006: ble.s L_0010
    L_0008: ldstr "A"
    L_000d: stloc.0 
    L_000e: br.s L_0016
    L_0010: ldstr "B"
    L_0015: stloc.0 
    L_0016: ldloc.0 
    L_0017: call void [mscorlib]System.Console::WriteLine(string)
    L_001c: ret 
}

null:

static void Main()
{
    string s = null;

    if (Environment.TickCount > 0)
        s = "A";
    else
        s = "B";

    Console.WriteLine(s);
}

IL :

.method private hidebysig static void Main() cil managed
{
    .entrypoint
    .maxstack 2
    .locals init (
        [0] string s)
    L_0000: ldnull         <====== Lookie here
    L_0001: stloc.0        <====== and here
    L_0002: call int32 [mscorlib]System.Environment::get_TickCount()
    L_0007: ldc.i4.0 
    L_0008: ble.s L_0012
    L_000a: ldstr "A"
    L_000f: stloc.0 
    L_0010: br.s L_0018
    L_0012: ldstr "B"
    L_0017: stloc.0 
    L_0018: ldloc.0 
    L_0019: call void [mscorlib]System.Console::WriteLine(string)
    L_001e: ret 

, null IL (, , .locals init ([0] string s) ).

, JIT , , , IL.

( string , , .)

+1

, .. , "null"; A

0

a.

  • A a = new A(); (, A , ).

, var. var , :

var a = new A();
  1. A a = null; a, , .

:

Func<int, int> factorial = null;
factorial = n => n < 3 ? n : n * factorial(n-1);

, . = null , , , .

, null. var a = default(A);, .

  1. A a;

. , . , , . , .

, , (ReSharper) Split declaration and assignment, var A, Move to outer scope . , x SomeVerylongTypeName<EvenMoreLongTypeName>, .

var x = container.GetFirst(); // some imaginary GetFirst method which returns an instance of non-keyboard friendly type.

: var a = x; () () () (Alt-Enter)... ... (Enter), :

SomeVerylongTypeName<EvenMoreLongTypeName> a;
a = x;
0

Source: https://habr.com/ru/post/1616032/


All Articles