Thread: value type inheritance

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    1,579

    value type inheritance

    Hello everyone,


    When dealing with value types, I met with two conflicting points,

    1. We can not inherit one value type from another, for example, we can not make a struct inherit from another struct; -- I think it means there is no inheritance or derivation for value types.

    2. All value types are inherits from ValueType, and ValueType is inherits from Object, seems value types could have inheritance or derivation?

    How do you understand the two conflicting points? :-)


    thanks in advance,
    George

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    There is a difference between "int" and "System.Int32". One is a primitive type, one is a class. One can be inherited, one cannot. C# automatically converts between these two as needed so the user never has to think about it (both for good and bad, as seen by this post). The distinction is much clearer in say Java where you have to explicitly convert.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  3. #3
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Sorry Magos,


    I do not agree. In IL code, int is the same as Int32. :-)

    Quote Originally Posted by Magos View Post
    There is a difference between "int" and "System.Int32". One is a primitive type, one is a class. One can be inherited, one cannot. C# automatically converts between these two as needed so the user never has to think about it (both for good and bad, as seen by this post). The distinction is much clearer in say Java where you have to explicitly convert.

    regards,
    George

  4. #4
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    It's called boxing and unboxing, a class wrapping a primitive type.
    http://msdn.microsoft.com/en-us/library/yz2be5wk.aspx
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. strings Vs. Char pointers
    By aijazbaig1 in forum C Programming
    Replies: 49
    Last Post: 02-13-2008, 09:51 AM
  2. inheritance and performance
    By kuhnmi in forum C++ Programming
    Replies: 5
    Last Post: 08-04-2004, 12:46 PM
  3. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM
  4. odd errors from msvc std library files
    By blight2c in forum C++ Programming
    Replies: 6
    Last Post: 04-30-2002, 12:06 AM
  5. gcc problem
    By bjdea1 in forum Linux Programming
    Replies: 13
    Last Post: 04-29-2002, 06:51 PM