primitive types

  • Thread starter Thread starter Mark
  • Start date Start date
M

Mark

What is the difference between declaring variables like ...

string strSomething;

versus

String strSomething;

My understanding is that the first uses a primitive type, and the second
creates an instance of a String class. While it likely will never make a
difference, is there reason to consider always using the primitive data type
when that's all you need? What are the best practices related to this?

Perhaps String/string is a unique case ... but bool and Boolean appear to be
similar???

Thanks in advance.

Mark
 
Mark,
What is the difference between declaring variables like ...

string strSomething;

versus

String strSomething;

"string" is a language keyword and an alias for System.String.
"String" and "string" should pretty much always resolve to the same
thing. You can, however, use "string" without importing the System
namespace. To use "String" you must either add "using System;" or
fully qualify it as System.String.

Perhaps String/string is a unique case ... but bool and Boolean appear to be
similar???

Yes, the same applies to int/Int32, float/Single and so on.



Mattias
 
Thanks Mattias. I'd assume this means that the most straight forward way to
do this is to always using string and bool rather than String and Boolean.

Correct?

Thanks again.

Mark
 
In C#, the alias is preferred, yes. As Mattias said, it's just an alias...
the compiler actually writes the same thing behind the scenes regardless of
which one you use. If you are coming from the Java world, this isn't like
boolean vs. Boolean, where one is a primitive and the other is an "object
version" of the primitive.

-Rob Teixeira [MVP]

Mark said:
Thanks Mattias. I'd assume this means that the most straight forward way to
do this is to always using string and bool rather than String and Boolean.

Correct?

Thanks again.

Mark

Mattias Sjögren said:
Mark,


"string" is a language keyword and an alias for System.String.
"String" and "string" should pretty much always resolve to the same
thing. You can, however, use "string" without importing the System
namespace. To use "String" you must either add "using System;" or
fully qualify it as System.String.
to
 
Back
Top