-1

I probably missing something but as I understand string and String are aliases as explained in difference-between-string-and-string so string is just an object!

Now what I don't understand is how the following code initialize new object of String?

string s="Hello World"

Can I do this trick for regular objects?

Community
  • 1
  • 1
ilay zeidman
  • 2,164
  • 4
  • 19
  • 41

2 Answers2

3

If you are compiling them in your code.. they are compile time constants.. i.e. code explicitly references them from your compiled binary, which is of course loaded in memory at runtime..

If you construct them at runtime.. like from char array, I would guess that CLR has the necessary implementation for doing so. For example - look at following code from http://referencesource.microsoft.com/#mscorlib/system/string.cs,97ccd50b20126543

[System.Security.SecuritySafeCritical]  // auto-generated
private static String ConcatArray(String[] values, int totalLength) {
    String result =  FastAllocateString(totalLength);
    int currPos=0;

    for (int i=0; i<values.Length; i++) {
        Contract.Assert((currPos <= totalLength - values[i].Length), 
            "[String.ConcatArray](currPos <= totalLength - values[i].Length)");

        FillStringChecked(result, currPos, values[i]);
        currPos+=values[i].Length;
    }

    return result;
}

[System.Security.SecurityCritical]  // auto-generated
[ResourceExposure(ResourceScope.None)]
[MethodImplAttribute(MethodImplOptions.InternalCall)]
internal extern static String FastAllocateString(int length);

Essentially strings get special treatment in the language, and although they are objects (immutable), you are right in your understanding that they are not instantiated with new operator in traditional sense. i.e. you don't do what you said in your comment String s=new String("Hello World"); because if you think about it, the new is redundant, because you have already defined your string in double quotes as a string literal.

And hence while you could use implicit operator, to convert a string to a given type.. It is not the same trick. The trick in case of strings is the native support from CLR.

EDIT: Here is another proof.. There is a special IL OpCode to load strings. OpCodes.Ldstr

"Pushes a new object reference to a string literal stored in the metadata."

If you'll look at your compiled code, you'd see ldstr opcode being used to load strings, instead of newobj opcode.

Community
  • 1
  • 1
Vikas Gupta
  • 4,329
  • 1
  • 17
  • 37
2

As mentioned by @KirkWoll, from Using Conversion Operators you could use an implicit operator

Something like

public class FOO
{
    private string _foo;

    public static implicit operator FOO(string foo)
    {
        FOO f = new FOO {_foo = foo};
        return f;
    }
}

and then calling it

FOO bar = "TADA";
Adriaan Stander
  • 150,765
  • 27
  • 267
  • 275