Click here to Skip to main content
15,888,521 members
Please Sign up or sign in to vote.
2.15/5 (4 votes)
See more:
I have one student class which has properties,methods and variables which any class in object orientation concept contains. When I gone through metadata of String class in dot net, It also contains properties,methods,variables. So when we assign value to the String we does this normally :

C#
String s = "Code Project"; // No compile error (1st line)

String test = new String(); // Compile error  (2rd line)


Error in 2nd line:
'string' does not contain a constructor that takes 0 arguments

but as I know every class in dot net should have at least one constructor

Secondly,

If String class contains properties,methods,variables. Then how the 1st line code works.
and the if same thing I have tried to do with my Student class it shows me compile error.

C#
Student s= "Code Project"; // Compile error  (3rd line)


Is there any extra things they did with the predefined classes?
which we cannot do in user define classes.

Can I make a another String class similar to this?

Thanks,
Suraj
Posted
Comments
Philippe Mori 6-Sep-14 16:52pm    
By the way string class does not have a constructor without parameters because a string object is immutable and cannot be modified once created. String functions that modify content returns a new object.
Raul Iloc 7-Sep-14 2:42am    
You agreed a solution that is only partially correct! See my comments in the 1st solution!
For details about string class and string literals you can search on MSDN or you can read the next article: http://www.dotnetperls.com/string-literal

1.Your first line declare a variable of type string and inits it with the "string literal"-the string expression, like a temp constant, given in the second part.

2.The second line return error, because String class does not have an constructor with no parameters.
For details about string class, and its constructors, see details in MSDN : http://msdn.microsoft.com/en-us/library/System.String(v=vs.110).aspx[^]

3.To create your own class that could init your object like in your 3rd example your should implement your implicit operator converter. See details in the next article: Understanding Implicit Operator Overloading in C#[^]
 
Share this answer
 
If you look at the documentation for the
String Class[^]
you will see that there is no constructor that takes 0 arguments, hence the first compiler error.

The first line works because the literal string "Code Project" is treated as a constant by the compiler and stored on the heap in table.
Then the reference of that constant is assigned to the variable s.
See this link for more information about string storage.
String.Intern Method[^]

To add assignment functionality to your own class, you need to add an implicit operator.
C#
public static implicit operator Student(string s)
{
    Student stud = new Student(s); //Internally call Student constructor
    return stud;
}

= Operator (C# Reference)[^]
implicit (C# Reference)[^]

Of course you need to have a constructor in your Student class that takes a string as an argument.

If you have other data types that you want to assign like this, you just add one operator per type.
 
Share this answer
 
v4
Comments
surajsurve 6-Sep-14 13:25pm    
Wow......!!!! what an answer!! Thank a lot. 5 stars also less for this one!!! Really appreciated.
George Jonsson 6-Sep-14 13:29pm    
You are welcome. It was a good question.
Raul Iloc 7-Sep-14 3:10am    
There are 3 errors, regarding String class, in the solution above, see details in my comment bellow!
George Jonsson 7-Sep-14 3:19am    
I have corrected the faulty part of the explanation.
Thanks7872 6-Sep-14 15:14pm    
I have always appreciated descriptive answers. Great explanation. +5.

I request you to add at least one such answer every day! :-)

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