super newbie question

  • Thread starter Thread starter John Salerno
  • Start date Start date
J

John Salerno

Actually, I feel like my knowledge goes beyond this question, but it's
still something I wonder about. Why, when you declare variables such as

int x = 5;

or

string word = "Hello";


do you not use the 'new' keyword? Are these not objects? Or is there
something implicit about them that uses the new keyword? Or is the new
keyword used for something other than this? (I know when it *is* used,
but not necessarily why it isn't used in some cases.)

Thanks.
 
John Salerno said:
Actually, I feel like my knowledge goes beyond this question, but it's
still something I wonder about. Why, when you declare variables such as

int x = 5;

or

string word = "Hello";


do you not use the 'new' keyword? Are these not objects? Or is there
something implicit about them that uses the new keyword?

Basically, it's because C# gives you a few types that are especially easy to
declare.

"int" and "string" are keywords of C# and have the syntax you've just shown.

You could say
Int32 x = new Int32(5);
if you wanted to use the name of the type, rather than the C# keyword that
provides a shortcut to it. (If I'm remembering the syntax right. Too many
languages to keep up with!)
 
Are these not objects?

Everything's an object at its lowest level but, according to the C# docs:

The int keyword denotes an integral type

The string type represents a string of Unicode characters. string is an
alias for System.String in the .NET Framework.
Although string is a reference type, the equality operators (== and !=) are
defined to compare the values of string objects, not references
 
John,

Take it just as "syntactic sugar". It would be very unconfortable if we had
to type

string word = new string( { 'H', 'e', 'l', 'l', 'o' } );

whenever we want to create and initialize a string object. So the language
defines a "shortcut" or "nicer" way to do it:

string word = "Hello";

Regards - Octavio
 
Octavio said:
John,

Take it just as "syntactic sugar". It would be very unconfortable if we had
to type

So basically the functionality of the 'new' keyword is being used behind
the scenes? If so, then I feel more comfortable with it. Because there
have been cases where I've seen 'new' used, and other times not, and I
didn't get the difference, unless it's just that you can do it either way.

Thanks guys!
 
John Salerno said:
So basically the functionality of the 'new' keyword is being used behind
the scenes? If so, then I feel more comfortable with it. Because there
have been cases where I've seen 'new' used, and other times not, and I
didn't get the difference, unless it's just that you can do it either way.

No. There's a very important difference - whenever you use a string
literal twice within the same assembly (and, I believe as an
implementation details, within the same app domain) you end up with a
reference to the same string both times.

Using "new" would end up creating a new string instance every time you
went through that code.
 
Back
Top