Data Types in C#

Discussion in 'C#' started by pradeep, May 21, 2007.

  1. pradeep

    pradeep Team Leader

    Joined:
    Apr 4, 2005
    Messages:
    1,645
    Likes Received:
    87
    Trophy Points:
    0
    Occupation:
    Programmer
    Location:
    Kolkata, India
    Home Page:
    http://blog.pradeep.net.in
    C# allows you to define two types of variables: value types and reference types. The value types hold actual values, while reference types hold references to values stored somewhere in memory.

    Also value types are allocated on the stack and are available in most programming languages. Reference types are allocated on the heap and typically represent class instances.

    Predefined C# value types

    • sbyte: Holds 8-bit signed integers. The s in sbyte stands for signed, meaning that the variable's value can be either positive or negative. The smallest possible value for ansbyte variable is -128; the largest possible value is 127.
    • byte: Holds 8-bit unsigned integers. Unlike sbyte variables, byte variables are not signed and can only hold positive numbers. The smallest possible value for a byte variable is 0; the largest possible value is 255.
    • short: Holds 16-bit signed integers. The smallest possible value for a short variable is -32,768; the largest possible value is 32,767.
    • ushort: Holds 16-bit unsigned integers. The u in ushort stands for unsigned. The smallest possible value of an ushort variable is 0; the largest possible value is 65,535.
    • int: Holds 32-bit signed integers. The smallest possible value of an int variable is -2,147,483,648; the largest possible value is 2,147,483,647.
    • uint: Holds 32-bit unsigned integers. The u in uint stands for unsigned. The smallest possible value of a uint variable is 0; the largest possible value is 4,294,967,295.
    • long: Holds 64-bit signed integers. The smallest possible value of a long variable is 9,223,372,036,854,775,808; the largest possible value is 9,223,372,036,854,775,807.
    • ulong: Holds 64-bit unsigned integers. The u in ulong stands for unsigned. The smallest possible value of a ulong variable is 0; the largest possible value is 18,446,744,073,709,551,615.
    • char: Holds 16-bit Unicode characters. The smallest possible value of a char variable is the Unicode character whose value is 0; the largest possible value is the Unicode character whose value is 65,535.
    • float: Holds a 32-bit signed floating-point value. The smallest possible value of a float type is approximately 1.5 times 10 to the 45th power; the largest possible value is approximately 3.4 times 10 to the 38th power.
    • double: Holds a 64-bit signed floating-point value. The smallest possible value of a double is approximately 5 times 10 to the 324th; the largest possible value is approximately 1.7 times 10 to the 308th.
    • decimal: Holds a 128-bit signed floating-point value. Variables of type decimal are good for financial calculations. The smallest possible value of a decimal type is approximately 1 times 10 to the 28th power; the largest possible value is approximately 7.9 times 10 to the 28th power.
    • bool: Holds one of two possible values, true or false. The use of the bool type is one of the areas in which C# breaks from its C and C++ heritage. In C and C++, the integer value 0 was synonymous with false, and any nonzero value was synonymous with true. In C#, however, the types are not synonymous. You cannot convert an integer variable into an equivalent bool value. If you want to work with a variable that needs to represent a true or false condition, use a bool variable and not an int variable.

    Predefined C# reference types



    • string: Represents a string of Unicode characters. It allows easy manipulation and assignment of strings. Strings are immutable, meaning that once it is created it can't be modified. So when you try to modify a string, such as concatenating it with another string, a new string object is actually created to hold the new resulting string.
    • object: Represents a general purpose type. In C#, all predefined and user-defined types inherit from the object type or System.Object class.

    Summary



    Proper utilization of correct data types allows developers to make the most of the language, but may take some time for those who have used different programming languages prior to switching to C#. For more information about each type, visit the Microsoft Web site.
     
  2. coderzone

    coderzone Super Moderator

    Joined:
    Jul 25, 2004
    Messages:
    736
    Likes Received:
    38
    Trophy Points:
    28
    Can you tell me why string is reference type and not as value type. I never understood this.
     
  3. pradeep

    pradeep Team Leader

    Joined:
    Apr 4, 2005
    Messages:
    1,645
    Likes Received:
    87
    Trophy Points:
    0
    Occupation:
    Programmer
    Location:
    Kolkata, India
    Home Page:
    http://blog.pradeep.net.in
    Reference types actually hold the value of a memory address occupied by the object they reference. Consider the following piece of code, in which two variables are given a reference to the same object (for the sake of the example, this object is taken to contain the numeric property 'myValue').

    Code:
        
     object x = new object();
    x.myValue = 10;
    object y = x;
    y.myValue = 20; // after this statement both x.myValue and y.myValue equal 20
    This code illustrates how changing a property of an object using a particular reference to it is reflected in all other references to it. Note, however, that although strings are reference types, they work rather more like value types. When one string is set to the value of another, eg

    Code:
    string s1 = "hello";
    string s2 = s1;
    Then s2 does at this point reference the same string object as s1. However, when the value of s1 is changed, for instance with
    Code:
    s1 = "goodbye";
    what happens is that a new string object is created for s1 to point to. Hence, following this piece of code, s1 equals "goodbye", whereas s2 still equals "hello".

    The reason for this behaviour is that string objects are 'immutable'. That is, the properties of these objects can't themselves change. So in order to change what a string variable references, a new string object must be created.
     
  4. coderzone

    coderzone Super Moderator

    Joined:
    Jul 25, 2004
    Messages:
    736
    Likes Received:
    38
    Trophy Points:
    28
    I am not sure and would like to know if this can be done.
    Code:
    int a = new int();
    string s = new string();
    for int as well as for string.
     
  5. pradeep

    pradeep Team Leader

    Joined:
    Apr 4, 2005
    Messages:
    1,645
    Likes Received:
    87
    Trophy Points:
    0
    Occupation:
    Programmer
    Location:
    Kolkata, India
    Home Page:
    http://blog.pradeep.net.in
    string is an Object, that's why we instantiate it! int is not any class/object so, we can't do "new int();"
     
  6. coderzone

    coderzone Super Moderator

    Joined:
    Jul 25, 2004
    Messages:
    736
    Likes Received:
    38
    Trophy Points:
    28
    Code:
    int i = new int();
    is allowed.
     
  7. pradeep

    pradeep Team Leader

    Joined:
    Apr 4, 2005
    Messages:
    1,645
    Likes Received:
    87
    Trophy Points:
    0
    Occupation:
    Programmer
    Location:
    Kolkata, India
    Home Page:
    http://blog.pradeep.net.in
  8. coderzone

    coderzone Super Moderator

    Joined:
    Jul 25, 2004
    Messages:
    736
    Likes Received:
    38
    Trophy Points:
    28
    Test it and see.
     
  9. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    string is pretty much what is told but why you are able to do this
    Code:
    int i = new int();
    is for consistency but the allocation is not in heap and its not used as reference and the simple reason I can think of is the compiler never knows what is the type (reference / value) and so for basic types its always the value type.
     
  10. rekha11

    rekha11 New Member

    Joined:
    Jan 31, 2008
    Messages:
    13
    Likes Received:
    3
    Trophy Points:
    0
    A data type can be described as being either:

    A built-in data type such as an int or char

    OR

    A user-defined data type such as a class or interface.

    Data types can also be defined as being either:

    Value Types : which store values

    Reference Types: which store references to the actual

    Pointer types :Which can be used only in unsafe mode
     
  11. debleena_doll2002

    debleena_doll2002 New Member

    Joined:
    Feb 5, 2008
    Messages:
    119
    Likes Received:
    0
    Trophy Points:
    0
  12. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    We did not consider this as duplicate because the content is mainly from MSDN and also the same thing is there on other site
     
  13. aisha.ansari84

    aisha.ansari84 New Member

    Joined:
    Feb 13, 2008
    Messages:
    82
    Likes Received:
    1
    Trophy Points:
    0
    as told by debleena is correct only.. but anyone cant put their name as author. Just put the from MSDN or give the link of msdn.
     
  14. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    What I meant is the data/code is not that of author Its from MSDN
     
  15. aisha.ansari84

    aisha.ansari84 New Member

    Joined:
    Feb 13, 2008
    Messages:
    82
    Likes Received:
    1
    Trophy Points:
    0

    By any way it should come into that categories. means article should be deleted.


    One thing our aim is to increase the quality of this forum. So Imrantechi told me to do this.
     
  16. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    Its already there.
     
  17. aisha.ansari84

    aisha.ansari84 New Member

    Joined:
    Feb 13, 2008
    Messages:
    82
    Likes Received:
    1
    Trophy Points:
    0

    what is meant by this??
     
  18. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    As the content is from MSDN with a link to the content related pages and so its not duplicate
     
  19. aisha.ansari84

    aisha.ansari84 New Member

    Joined:
    Feb 13, 2008
    Messages:
    82
    Likes Received:
    1
    Trophy Points:
    0
    If we are going to coping some article from other author then we should responsible to ask him for permission. Then he should tell that i have taken this artiicle from other source(like in u'r case MSDN) then i have to go MSDN' responsible person to ask for permission.

    suppose the article(suppose A) which is copied from MSDN that person is responsible i.e. means he may be wrong or have taken permission from msdn.
    But this article(by pradeep suppose B) copied from article A or may be from msdn. In both cases it's duplicate of msdn or that article. In both case it's illegal.
     
  20. aisha.ansari84

    aisha.ansari84 New Member

    Joined:
    Feb 13, 2008
    Messages:
    82
    Likes Received:
    1
    Trophy Points:
    0
    shabbir why you are saying article by pradeep is not duplicate. this article is 100% duplicate from msdn or that article ( I have given a link)

    First thing shabbir, you are saying duplicate. But do you know the meaning of duplicate???

    This is not at all duplicate ... This is illegal Copied from some author article.

    there is very big difference b/w duplicate and Illegal copied.

    Duplicate: Author gives his same article to different different sites or magazine. Then those article are called duplicate.

    Illegal(Unauthorized) Copied: Without taking any permission from aouthor, copied that article .

    If he gave the permission then author name should be same author. Only you can advertize in your site with name of that author.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice