Click here to Skip to main content
15,898,996 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
little confusion about string class
Creating instance of any non static class requires use of new keyword.
why it is not mandatory with string class. Like

string str = "Hello";


forgive me if it is stupied question

Thanks

:)
Posted

1 solution

Because the creation of the "Hello" effectively constructs a new object (it doesn't quite, as if you use "Hello" in two different places they both refer to the same string).
If you think about it, string is not the only class that behaves in this way: int, double, bool, etc. do not require the new keyword either (thank goodness!)

The real reason, I suspect, is that it would make programs difficult to read!
 
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