Confused with string reference type
-
Hi friends.. string str="ABC"; this means that we are assigning value to the object str of string class..right?But whn we are instantiating some class then we are assigning values to the feilds of that class(ex:Employee_1.Name="xyz",Employee_1.Id=1234,etc where Employee_1 is an object of class Employee)..thn how things are working differently for string class..I think this has to do something with user defined and predefined reference data types..
-
Hi friends.. string str="ABC"; this means that we are assigning value to the object str of string class..right?But whn we are instantiating some class then we are assigning values to the feilds of that class(ex:Employee_1.Name="xyz",Employee_1.Id=1234,etc where Employee_1 is an object of class Employee)..thn how things are working differently for string class..I think this has to do something with user defined and predefined reference data types..
All reference types are the same, regardless if the type is user defined or "predefined".
Upcoming events: * Glasgow: SQL Server 2005 - XML and XML Query Plans, Mock Objects, SQL Server Reporting Services... Never write for other people. Write for yourself, because you have a passion for it. -- Marc Clifton My website
-
Hi friends.. string str="ABC"; this means that we are assigning value to the object str of string class..right?But whn we are instantiating some class then we are assigning values to the feilds of that class(ex:Employee_1.Name="xyz",Employee_1.Id=1234,etc where Employee_1 is an object of class Employee)..thn how things are working differently for string class..I think this has to do something with user defined and predefined reference data types..
Literal "ABC" IS instance of type string.
"Throughout human history, we have been dependent on machines to survive. Fate, it seems, is not without a sense of irony. " - Morpheus "Real men use mspaint for writing code and notepad for designing graphics." - Anna-Jayne Metcalfe
-
Hi friends.. string str="ABC"; this means that we are assigning value to the object str of string class..right?But whn we are instantiating some class then we are assigning values to the feilds of that class(ex:Employee_1.Name="xyz",Employee_1.Id=1234,etc where Employee_1 is an object of class Employee)..thn how things are working differently for string class..I think this has to do something with user defined and predefined reference data types..
Hi, Even your question is not very clear:-) Let me explain you the difference between the predefined and user defined data types. See if "AA" is a user defined class, while creating an instance of the classs AA, "new" operator must be used. i.e. AA a = new AA(); Only, after this we can set/get the non-static members. In case of predfined types like "string", the C# compiler knows of them advance, hence uses "new" operator internally. i.e. In case of string str = "ABC"; C# compiler knows about string class, hence does str = new string("ABC") internally. Manoj
-
Hi, Even your question is not very clear:-) Let me explain you the difference between the predefined and user defined data types. See if "AA" is a user defined class, while creating an instance of the classs AA, "new" operator must be used. i.e. AA a = new AA(); Only, after this we can set/get the non-static members. In case of predfined types like "string", the C# compiler knows of them advance, hence uses "new" operator internally. i.e. In case of string str = "ABC"; C# compiler knows about string class, hence does str = new string("ABC") internally. Manoj
Thats nice explaination Keep it up :) :):)
Thanks and Regards Sandeep If If you look at what you do not have in life, you don't have anything, If you look at what you have in life, you have everything... "
-
Hi, Even your question is not very clear:-) Let me explain you the difference between the predefined and user defined data types. See if "AA" is a user defined class, while creating an instance of the classs AA, "new" operator must be used. i.e. AA a = new AA(); Only, after this we can set/get the non-static members. In case of predfined types like "string", the C# compiler knows of them advance, hence uses "new" operator internally. i.e. In case of string str = "ABC"; C# compiler knows about string class, hence does str = new string("ABC") internally. Manoj
-
Hi friends.. string str="ABC"; this means that we are assigning value to the object str of string class..right?But whn we are instantiating some class then we are assigning values to the feilds of that class(ex:Employee_1.Name="xyz",Employee_1.Id=1234,etc where Employee_1 is an object of class Employee)..thn how things are working differently for string class..I think this has to do something with user defined and predefined reference data types..
The only special about strings, is that you can write string literals in the code. When the code is compiled, the string literals are created as constant string objects. So, this code:
string str = "ABC";
works more like this:const string strconst001 = "ABC"; // created at compile time string str = strconst001; // just assign the reference
So, when you assign a string literal, you don't really create a string object, you only assign the reference of a string object that already exists. When you actually create a new string object, it works like any other class. Example:string str = new String('*', 42);
--- single minded; short sighted; long gone;