C# objects?

  • Thread starter Thread starter Eddie Pazz
  • Start date Start date
E

Eddie Pazz

While reading that everything in C# is an object, why is it that I can do:

string temp = "";

instead of

string temp = new string("");

Is this just a shortcut the compiler provides?
 
Do you know that you can do this in C#

(1).ToString();

or

("My Name").CompareNoCase("Your Name")!= 0

Even the literal string or integer are objects in C#.
 
Hi eddie,

strings are "special" and act a little bit like a value type (like int,
float ...) as well as a reference type (everything else).

I can't say for sure if C# acts the same way (hopefully someone can
confirm or deny this), but in Java, strings are collected in a pool and
reused, and I believe C# does so too.

string temp1 = "123";
string temp2 = "123";

This would cause 1 string to be generated and put in the pool, and both
temp1 and temp2 would point to the same string object.

string temp1 = new string("123");
string temp2 = new string("123");

This would cause 2 strings to be generated and the strings would not be
put in the pool.

Happy coding!
Morten Wennevik [C# MVP]
 
Eddie Pazz said:
While reading that everything in C# is an object, why is it that I can do:

string temp = "";

instead of

string temp = new string("");

Is this just a shortcut the compiler provides?

What constructor would you believe the second would call?

A string literal being present in code makes sure that a string with
the appropriate data in is available at run time. Strings are interned,
so that only one object is created for all literals with the same data.
 
Actually, you can't do

string temp = new string("")

in C#, no overload for string constructor accepts "";

Happy coding!
Morten Wennevik [C# MVP]
 
It's always worth pointning out in this area that strings are inavriant.
Once a string has been initialized, anything that might seem to change it
actaully creates a new string;

string temp = "foo";

temp = "bar"; // temp is now referencing a different object. We
have not changed the value of the existing object

Hence:

void func (string s)
{
s = "bar";
}

....

string temp = "foo";

func (temp);

Console.WriteLine (temp); // Outpus "foo", even though string is a
reference type.
 
While reading that everything in C# is an object, why is it that I can do:

string temp = "";

instead of

string temp = new string("");

Is this just a shortcut the compiler provides?

Just a shortcut to keep C++ people happy
 
Vincent Finn said:
Just a shortcut to keep C++ people happy

No it's not - otherwise string interning wouldn't get involved. It's
not calling a constructor at all - it's inserting a ldstr instruction
in the IL stream.
 
Yep, that's the "interning" Jon was talking about. here's the modified code that will to demonstrate your point

string a = "12"
string b = "12"
Console.WriteLine( "{0}", (object)a == (object)b )

string aa = a + "3"
string bb = b + "3"
Console.WriteLine( "{0}", (object)aa == (object)bb )

----- Morten Wennevik wrote: ----

I can't say for sure if C# acts the same way (hopefully someone can
confirm or deny this), but in Java, strings are collected in a pool and
reused, and I believe C# does so too.
 
The point is, there is nothing that can happen to s inside func () that will
change what you see when you look at temp.

Daniel Jin said:
no. your example has nothing to do with strings being immutable. you
didn't pass your string by reference, that's why it's not changed after
invoking the method. any object would have behaved that way if you don't
pass them in by reference.
 
Back
Top