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

  • Thread starter Thread starter Martin Arvidsson
  • Start date Start date
M

Martin Arvidsson

Hi

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

Regards
Martin
 
None.

'string' is an alias for the 'String' class. Just as 'int' is an alias for
the Int32 structure, and so on.
 
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.
 
No, I am fairly certain the compiler knows to translate one to the other.
 
int is mapped to System.Int32 so other then typing more you won't accomplish
much :)

Regards,
John
 
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.
 
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?
 
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?
 
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 :(
 
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
 
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.
 
Back
Top