Q: String (Capital S) vs string (small s)?

  • Thread starter Martin Arvidsson
  • Start date
M

Martin Arvidsson

Hi

What is the difference by using String myString or string myString when
declaring a string ?

Regards
Martin
 
M

Marina Levit [MVP]

None.

'string' is an alias for the 'String' class. Just as 'int' is an alias for
the Int32 structure, and so on.
 
G

gbgeek

Is there a benefit to using one over the other? I've sometimes found
myself wondering if I should declare all my int's as Int32's.
 
M

Marina Levit [MVP]

No, I am fairly certain the compiler knows to translate one to the other.
 
J

John J. Hughes II

int is mapped to System.Int32 so other then typing more you won't accomplish
much :)

Regards,
John
 
J

Jon Skeet [C# MVP]

Is there a benefit to using one over the other? I've sometimes found
myself wondering if I should declare all my int's as Int32's.

I use the keyword names in my code for the sake of keyword higlighting
- but for library methods, you should use the type name (eg
Convert.ToInt32) so that it's not tailored to one particular .NET
language.
 
G

gbgeek

That's sound advice. Is that the reason these aliases exist in C#? To
provide a way to refer to objects in a non-language specific way?
 
D

Dave Sexton

Hi Jon,

I do the same for keyword highlighting, but I'm not clear on why I shouldn't
in library methods. Once compiled to CIL, I don't see how it would matter.

Are you suggesting that I use Convert.ToInt32 instead of casting to (int),
even in cases where I know that an explicit cast exists?
 
J

Jon Skeet [C# MVP]

Dave Sexton said:
I do the same for keyword highlighting, but I'm not clear on why I shouldn't
in library methods. Once compiled to CIL, I don't see how it would matter.

Are you suggesting that I use Convert.ToInt32 instead of casting to (int),
even in cases where I know that an explicit cast exists?

No, I'm suggesting that you write methods with names of ToInt32 and
ToSingle rather than ToInt and ToFloat. I wasn't talking about
implementation, just naming - sorry to confuse :(
 
K

Kevin Frey

Is this alias something internal to the compiler, or declared somewhere in
code?

In other words, could I use this aliasing feature for my own types?

Kevin
 
J

Jon Skeet [C# MVP]

Kevin Frey said:
Is this alias something internal to the compiler, or declared somewhere in
code?

In other words, could I use this aliasing feature for my own types?

You can alias types with the "using" directive at the start of a file,
but you can't add your own "global" aliases.
 

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