0

What is difference between these two codes:

MyClass a = new MyClass();
a = "something"

and

MyClass a = "something";

In first code it creates new copy of MyClass object. But what happens when not using new keyword? Will it affect on initial class (MyClass)?

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
Elshad Shabanov
  • 483
  • 10
  • 17

2 Answers2

8

Well, the second form is illegal, unless a conversion operator has been defined.

To implement this conversion operator, you will have to use new MyClass to construct a new instance.

So provided the second form is legal at all, the method and end result is the same.

Example of conversion operator:

public class MyClass
{
    public static implicit operator MyClass(string s)
    {
        return new MyClass();
    }
}

Note that this conversion operator also handles this line:

a = "something";

Which simply overwrites the original reference stored in a with the new one returned by the conversion operator.

A conversion operator defines a static method named op_Implicit (in this case) that will be called, so your code really looks like this under the hood:

MyClass a = new MyClass();
a = MyClass.op_Implicit("something");

or this:

MyClass a = MyClass.op_Implicit("something");

You can verify this using LINQPad if you can read IL.

This code:

void Main()
{
    MyClass a = "something";
    a = "xyz";
}

Translates to this IL:

IL_0000:  nop         
IL_0001:  ldstr       "something"
IL_0006:  call        UserQuery+MyClass.op_Implicit
IL_000B:  stloc.0     // a
IL_000C:  ldstr       "xyz"
IL_0011:  call        UserQuery+MyClass.op_Implicit
IL_0016:  stloc.0     // a
IL_0017:  ret  

Note the two calls to op_Implicit.

Now, if you don't implement the conversion operator then:

MyClass a = new MyClass();
a = "something"                      // error

MyClass a = "something";             // error

The error message will in both cases be:

CS0029
Cannot implicitly convert type 'string' to 'MyClass'

Lasse V. Karlsen
  • 380,855
  • 102
  • 628
  • 825
0

If you remove the double quotes around "something", and something is the name of a variable containing an instance of MyClass, then both will work.

i.e., if code was: Case 1:

MyClass something - new MyClass();
MyClass a = new MyClass();
a = something;   -- <--- This overwrites a with a different instance of 
                 --      MyClass, and leaves original instance in [a]
                 --      orphaned and subject to garbage collection.

or, Case 2:

MyClass something - new MyClass();
MyClass a = something;

Then both are just fine. and there is no difference between the two forms, except that in your source code one is represented in one statement, and the other it is in two statements. In both forms, two separate operations are being executed by the underlying code, a memory allocation on the heap for an instance of MyClass, and the creation and initialization of a pointer variable (something), populated with the address of that instance. I would not be surprised to find that both are exactly the same in the underlying Intermediate Language (IL) code.

The only difference (in my code samples above), is in the state after the operation has finished. In case1 above, two instances of MyClass are created, one represented by the variable something, and one by the variable a. Then the variable a is overwritten with the address of the one in something, leaving the one initially created in something orphaned and subject to garbage collection.

In case2 above, only one instance of MyClass is created, in the first line (MyClass something - new MyClass(); MyClass a = something;). In this same line the address of this instance is placed in the variable 'something. assigningsomethingtoain the second line does notcreate a new instance ofMyClass` it just creates a new reference variable which now also points to this same instance.

So the difference between the two cases is that in the first case two instances are created, one of which is orphaned, (the second one), and in the second case only one instance is created. In both cases you end up with two variables (a and something) both pointed to the same instance.

Charles Bretana
  • 143,358
  • 22
  • 150
  • 216