String or string

G

Greg

I've noticed two different "String" variable declarations to choose from. One
uses an upper case "S" and the other uses a lower case "s". The upper case
appears in a light green color, while the lower case appears as blue. If I go
to the the variable's definition the same definition appears for each, which
suggests to me these must be the same type.

Is there actually a different meaning for each type and what would this
difference be? And, if different, is one better to use than the other, with
performance in mind?

Can anyone shed some light on this for me?

Thanks.
 
A

Alberto Poblacion

Greg said:
I've noticed two different "String" variable declarations to choose from.
One
uses an upper case "S" and the other uses a lower case "s". The upper case
appears in a light green color, while the lower case appears as blue. If I
go
to the the variable's definition the same definition appears for each,
which
suggests to me these must be the same type.

Is there actually a different meaning for each type and what would this
difference be? And, if different, is one better to use than the other,
with
performance in mind?

"string" is a compiler synonym for System.String (in the same way as "int"
is a synonym for System.Int32), and appears blue because it is a reserved
word.
"String", since you are already "using System;", becomes System.String (and
appears light green because it is a class name).

So in both cases you end up with a System.String. There shouldn't be any
difference in the compiled code.
 
W

William Stacey

System.String is the actual type. "string" is effectively an alias for
System.String.

System.String s = "";
string s2 = ""

The compiler alias "string" just maps to System.String. Same with the other
types.
 
G

Granville Barnett

MC said:
String and string mean the same thing but are in two different series of
names.

There is a C# series of names (string, int, etc.) and a .NET
language-independent series (String, Int32, etc.). Many types exist only
in the second series of names. When a type has names in both series, you
can use either one.

Greg, I would use string as it is the *norm* in most source code. The same
can be said for int but it has a few caveats, e.g. if you are working in a
code base where you delve in and out of using 32 and 64 bit ints then it may
make sense to be explicit, i.e. Int32 (not int), Int64 - this seems to
provide a nice differentiation between the two.

Granville
 
G

Göran Andersson

As already have been said in the thread, they result in the same type.

It's only in a few situations that the CRL type and the C# alias are not
fully interchangeable. For example as a base type for enums:

enum test1 : int { A, B, C }; // int is valid
enum test2 : Int32 { A, B, C }; // Int32 is not valid
 
A

Arne Vajhøj

Greg said:
I've noticed two different "String" variable declarations to choose from. One
uses an upper case "S" and the other uses a lower case "s". The upper case
appears in a light green color, while the lower case appears as blue. If I go
to the the variable's definition the same definition appears for each, which
suggests to me these must be the same type.

Is there actually a different meaning for each type and what would this
difference be? And, if different, is one better to use than the other, with
performance in mind?

No difference just a matter of style.

I believe string (C# language style) is favored over
String (.NET framework style) by the majority.

Arne
 
A

Arne Vajhøj

Granville said:
Greg, I would use string as it is the *norm* in most source code. The
same can be said for int but it has a few caveats, e.g. if you are
working in a code base where you delve in and out of using 32 and 64 bit
ints then it may make sense to be explicit, i.e. Int32 (not int), Int64
- this seems to provide a nice differentiation between the two.

Depends on whether you consider knowing that int is 32 bit in C#
is a basic requirement for all C# developers.

I tend to think so.

Arne
 

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