Click here to Skip to main content
15,885,032 members
Please Sign up or sign in to vote.
4.00/5 (2 votes)
I had a Q does C# have primitive data type if yes then how to know that a data type is of primitive type and what are the rules for a data type to be a primitive data type can any one give me a link where i could learn more on this
Posted

See http://lmgtfy.com/?q=c%23+primitive+data+type[^] - first link I see will do!
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 9-May-11 2:49am    
I answered to this question.
--SA
Sergey Alexandrovich Kryukov 9-May-11 2:50am    
Makes sense, a 5.
--SA
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 9-May-11 2:49am    
I answered to this question.
--SA
The rule is using Reflection: for a variable v, call v.GetType(), or for a type T, call typeof(T). Both expression will return an object of the type System.Type. Check the instance property System.Type.IsPrimitive.

For type overview, read this article: http://msdn.microsoft.com/en-us/magazine/cc301569.aspx[^]. Pay attention for boxing.
For more detail, see: http://msdn.microsoft.com/en-us/library/ms173104.aspx[^].

For the type Type overview (Reflection), read this MSDN help page: http://msdn.microsoft.com/en-us/library/system.type.aspx[^].

For more information, read this tutorial: http://www.brainbell.com/tutors/C_Sharp/The_Type_System.htm[^].

—SA
 
Share this answer
 
v2
Comments
Steven.Pinto2000 9-May-11 2:05am    
i Had read that primitive data type cannot have functions with it so how does int has int.parse function. i had also read that in C# primitive data types are actually objects is it true
Sergey Alexandrovich Kryukov 9-May-11 2:11am    
In my answer I insisted you pay attention to boxing. Make sure you read and understand it first.
They do have methods in boxed form, and do no not in normal form. The method int.Parse is the method of boxed form of System.Int32. Good question. Read about boxing.
--SA
Steven.Pinto2000 9-May-11 2:52am    
ok one last Q why does .net has to do boxing on a primitive type without the user actually doing it on its own
Sergey Alexandrovich Kryukov 9-May-11 3:07am    
This is .NET philosophy. You can suggest your own. What, for example "doing it on his own"?

object a = 3; //boxing. why do you think it's not by the user?
object unboxing = a as System.Int32;
int i;
if (unboxing != null)
i = (int)unboxing; //i is unboxed "unboxing".

Again, which part is done not by the user?

OK, thanks for formally accepting this answer.
Good luck,
--SA
NuttingCDEF 9-May-11 3:36am    
Agree with all this.

In C#, EVERYTHING is ultimately an object - System.Int32 is the 'real' type - int is a shorthand - but some types, although still objects, are 'primitive' and are value types, so the semantics work the way you expect primitive types to work - when you use int / System.Int32, C# will normally do automatic boxing / unboxing (conversion between things that work like primitives and things that work like ordinary objects) for you to make your life a little easier - but sometimes you may have to worry about it and 'convert' between the two types manually.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900