473,395 Members | 1,348 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

primitive types

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
Nov 15 '05 #1
3 3735
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

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 15 '05 #2
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" <ma********************@mvps.org> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...
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 besimilar???


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

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.

Nov 15 '05 #3
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" <mf****@idonotlikespam.cce.umn.edu> wrote in message
news:Oh**************@TK2MSFTNGP09.phx.gbl...
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" <ma********************@mvps.org> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...
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 besimilar???


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

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.


Nov 15 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Ken | last post by:
Hi. Does the Java spec mandate set sizes for primitive types (e.g., int, float, char, etc.)? I know the ANSI C++ standard does not do this, which makes these sizes compiler dependent. So I guess...
3
by: Tony Johansson | last post by:
Hello! I'm reading in a document about window system and here they talk about primitive class. So my question is what is a primitive class? It might not mean any spacial just a name of a...
3
by: case2005 | last post by:
Can anyone help with the following, I don't know if it's possible, but I'm certain there must be a standard way of dealing with this. I have the following: template<typename FooBar, typename...
9
by: Gonçalo Rodrigues | last post by:
Hi all, I have a few questions on primitive types that I will divide in two main questions. I realize that some of these questions -- especially 2. below -- are not directly related to C++ as a...
26
by: Kip | last post by:
Greetings everyone, Is there anyone here who could point me to a page or pdf that has a list of the sizes of all of the C primitive data types on various implementations such as SPARC, x86,...
3
by: Bear | last post by:
Hello there How come it's possible to add values of the type "int" into an ArrayList when the Add member function only accepts values of type "object"? Is an int just a "typedef" of the Int32...
14
by: Matt | last post by:
I want to know if "int" is a primitive type, or an object? For example, the following two approaches yield the same result. > int t1 = int.Parse(TextBox2.Text); //method 1 > int t2 =...
1
by: Neil Zanella | last post by:
Hello all, In C and C++ a primitive data type is represented by a minimum number of bits as defined by the corresponding standard. For instance an int is assumed to be at least 16 bits on all...
10
by: Noah Roberts | last post by:
I understand that different integer types might have time penalties because of word boundaries. Where can I get information clarifying this topic? A quick google didn't turn up much for me...nice...
4
by: jehugaleahsa | last post by:
Hello: When developing data structures for C#, there is an obvious performance hit when utilizing primitive types. For instance, a recent hash table implementation I wrote works exceedingly fast...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.