Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: .NET1.0 .NET2.0 C#1.0 C#2.0 , +
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 8-May-11 19:32pm
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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
  Permalink  
v2
Comments
Steven.Pinto at 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
SAKryukov at 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.Pinto at 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
SAKryukov at 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 at 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.
SAKryukov at 9-May-11 3:46am
   
Right. You could put it in your own answer, as this explanation has its own value...
--SA
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

See http://lmgtfy.com/?q=c%23+primitive+data+type[^] - first link I see will do!
  Permalink  
Comments
SAKryukov at 9-May-11 2:49am
   
I answered to this question.
--SA
SAKryukov at 9-May-11 2:50am
   
Makes sense, a 5.
--SA
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

  Permalink  
Comments
SAKryukov at 9-May-11 2:49am
   
I answered to this question.
--SA

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 345
1 _Amy 210
2 Peter Leow 175
3 Nirav Prabtani 167
4 Andreas Gieriet 160
0 OriginalGriff 7,540
1 Sergey Alexandrovich Kryukov 6,597
2 Maciej Los 3,849
3 Peter Leow 3,653
4 CHill60 2,712


Advertise | Privacy | Mobile
Web04 | 2.8.140721.1 | Last Updated 9 May 2011
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100