What is the difference between "string" and "String" in C#?

G

Guest

I thought this would already be covered here, but my search turned up nothing.

In VS2005, if I use "String" to define a new variable/class, it colors it in
the Aqua color as it does other classes. But if I use "string", it colors it
in Navy blue as in C# reserved words (e.g. private, void, foreach, etc).

What is the diff?
 
P

Peter Bradley

J

jehugaleahsa

I thought this would already be covered here, but my search turned up nothing.

In VS2005, if I use "String" to define a new variable/class, it colors it in
the Aqua color as it does other classes. But if I use "string", it colors it
in Navy blue as in C# reserved words (e.g. private, void, foreach, etc).

What is the diff?

It is all about politics. Some people think of a String as a class and
others think of it as a built-in type. We gotta please everybody.

Personally, I use String when I am using the methods like Format,
Join, IsNullorEmpty, Empty and I use string when I am assigning it to
a literal. It just looks weird to me to see a .(dot) following a navy-
blue. It doesn't really matter though.

In case you are wondering, the same is true for the other built-in
types. Int32, Boolean, Byte, Char, blah blah . . .
 
J

jehugaleahsa

It is all about politics. Some people think of a String as a class and
others think of it as a built-in type. We gotta please everybody.

Personally, I use String when I am using the methods like Format,
Join, IsNullorEmpty, Empty and I use string when I am assigning it to
a literal. It just looks weird to me to see a .(dot) following a navy-
blue. It doesn't really matter though.

In case you are wondering, the same is true for the other built-in
types. Int32, Boolean, Byte, Char, blah blah . . .

Whoever made that blog made a mistake. There is *no* difference in
speed. It all compiles it all to the same underlying code. The
additional names are to help people make the relationship between MSIL
and C# (C# is a flagship). Just as int in managed C++ translate to
Int32 in MSIL, same with C#.
 
G

Guest

Jeff said:
I thought this would already be covered here, but my search turned up nothing.

In VS2005, if I use "String" to define a new variable/class, it colors it in
the Aqua color as it does other classes. But if I use "string", it colors it
in Navy blue as in C# reserved words (e.g. private, void, foreach, etc).

What is the diff?

The difference is that string is a keyword in C# and is an alias for
System.String. The String class is the System.String class, but as you
have using System; at the top of the file, you don't have to specify the
namespace.

The only difference when the code is compiled is how the type is
identified. Once the compiler has identified the type, there is no
difference at all.

The aliases and the framework classes works exactly the same, except for
some special cases. You can for example specify that an enum should use
an int as internal storage, but you can't specify System.Int32 instead.
 
C

Chris Dunaway

Whoever made that blog made a mistake. There is *no* difference in
speed. It all compiles it all to the same underlying code. The
additional names are to help people make the relationship between MSIL
and C# (C# is a flagship). Just as int in managed C++ translate to
Int32 in MSIL, same with C#.

I think you missed the point in her blog. Here is the line I think
you are referring to (correct me if I'm wrong):

<quote>
Actually in VB, Integer is more work than Int32, but in C# int is
faster than Int32.
</quote>

I think the blogger is referring to how fast you can type each word.
In VB, Integer is a longer word than Int32 so you can type Int32
faster. In C# int is shorter than Int32 so you can type it faster.

So I think the blogger was not making a statement on the speed of
execution, which is, of course, the same.

This was just my impression, I could be wrong.

Chris
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top