Click here to Skip to main content
15,893,588 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
what is the difference between

C#
String s = "xyz";

and
C#
String s = new String("xyz");
Posted
Updated 18-Sep-12 9:46am
v3

The second statement won't compile. There is no String constructor that takes a string as an argument (.http://msdn.microsoft.com/en-us/library/system.string.string.aspx[^]).
 
Share this answer
 
v3
Comments
[no name] 18-Sep-12 15:26pm    
Do you perhaps mean that there is no string constructor that takes a string as a parameter?
Sergey Alexandrovich Kryukov 18-Sep-12 16:36pm    
You are right, but I don't see a way to understand "There is not such a string constructor" in any other way. This phrase says the same.
--SA
[no name] 18-Sep-12 16:42pm    
No actually it does not. One way means that there is not a constructor for a string and the other means that there is no constructor that takes a string.
CodeHawkz 18-Sep-12 15:28pm    
Indeed, it won't compile since the String class have not got a constructor with a string parameter. But there are other string constructors and is very useful in some cases :)
Sergey Alexandrovich Kryukov 18-Sep-12 16:35pm    
Correct. The answer deserves my 5 anyway, but this comment is a useful clarification.
--SA
String stands for System.String and it is a .NET Framework type. string is an alias in the C# language for System.String. Both of them are compiled to System.String in IL (Intermediate Language), so there is no difference. Choose what you like and use that. If you code in C#, I'd prefer string as it's a C# type alias and well-known by C# programmers
 
Share this answer
 
Comments
Herman<T>.Instance 18-Sep-12 15:17pm    
my 5!
CPallini 18-Sep-12 15:22pm    
How is it related to the OP question?
[no name] 18-Sep-12 15:32pm    
Not related at all. Almost smells of sock puppetry.
Sergey Alexandrovich Kryukov 18-Sep-12 16:33pm    
Somewhat related. The information in this answer is correct, only OP did not ask about "string" vs. "String" but about not calling a constructor explicitly (first like).
--SA
[no name] 18-Sep-12 16:39pm    
And therfore not at all related to the OPs actual question. Whether the information is correct or not is immaterial in that it does not in any way answer the OPs question.

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