Click here to Skip to main content
15,886,919 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
In C# objects can be declared by using the new operator and also without using the new operator. Is there any difference between the methods other than there syntaxes?

for example-

C#
string s = "abc";


and

C#
char[] arr = { 'a', 'b', 'c' }; 
string s = new string(arr);


P.S- I am using Visual Studio 2015

What I have tried:

...............................................................................
Posted
Updated 9-Jun-16 11:24am

Like nearly all "what is the difference between X and Y", this is not a valid question (first case is one statement; and another case is a couple of statements :-)), but, fundamentally, it does have some sense.

The answer is: no functional difference at all. The result is exactly the same.

Here is the background: System.String, the type which can also be used via its C# alias "string" (what's the difference? no functional difference at all, just different text of the code :-)) is a reference type. But it perfectly mimics the value-type semantic, which is not a trivial feature, very non-trivial. All reference types, fundamentally, are instantiated via the constructor. There is only one System.String constructor; you show its use. As to the assignment of the string literal "abc" to the string variable, you can consider it as an overloaded assignment operator.

But things will look much more complicated if you assign the same thing to another string object. If it wasn't string, it would be two different references to the same object. In case of string, it's way, way more sophisticated. Too understand what's going on, you have to understand such advanced thing as string intern pool. Please see:
String interning — Wikipedia, the free encyclopedia,
String.Intern Method (String) (System),
String.IsInterned Method (String) (System).

If you feel that this is a bit over your head (but better learn and understand it, just to have an idea), don't worry: in practice, there is nothing much to worry about. Only you should clearly understand more fundamental fact: strings are strictly immutable.

—SA
 
Share this answer
 
Most (but not all) reference types need the new operator in order to create an instance of the class. The exception is strings, which don't require it - partly because it would make the code ugly, and partly because they are immutable.
All other reference types require new in order to allocate the space on the Heap and return a reference to it so the class instance can be used.

Value types on the other hand don't need new - it's only required if you create a value type that needs a constructor (like Point which needs two values to make a "proper" location). For the other value types, it is mostly implied. For example, you can say
C#
int i = new int();
i = 6;
But it's shorter an more readable to say
C#
int i = 6;
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900