Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is new String("Hello") invalid in C#?

What is the logic/reason behind making

String s= new String("Hello World");

Illegal in C#? The error is

The best overloaded method match for `string.String(char*)' has some invalid arguments

I'm not interested in the API docs, I am interested in why this is illegal.

Is is because of pooling static strings? like Java pools Integer(-128) to Integer(127) with horrendous results? ( of course strings too )

like image 326
Captain Giraffe Avatar asked Sep 30 '11 21:09

Captain Giraffe


People also ask

How do you initiate a string without escaping each backslash?

You can use " \\ " instead of " \ " as well as '@' sign in the beginning.

Why are strings in C# immutable?

Why should you use immutable strings? One advantage is that they are thread safe. If you are working with a multi threaded system, there will be no risk of a deadlock or any concurrency issues, since when you modify a string, you are really just creating a new object in memory.

What is string in C sharp?

A string is an object of type String whose value is text. Internally, the text is stored as a sequential read-only collection of Char objects. There's no null-terminating character at the end of a C# string; therefore a C# string can contain any number of embedded null characters ('\0').


1 Answers

It would be rather pointless to use the constructor to create a new string based on another existing string - that's why there is no constructor overload that allows this. Just do

string s = "Hello World";
like image 91
BrokenGlass Avatar answered Oct 12 '22 11:10

BrokenGlass