brother14th

Hi All,

I cheked the Object.ToString method in MSDN this morning. It says that
This method can be overridden in a derived class to return values that are meaningful for that type.For example, the base data types, such as Int32, implement ToString so that it returns the string form of the value that the object represents...

After reading these, I've two questions.
Q1)Can Int32 be considered as a class I checked the Int32 definition. It's a struct that implements the following interfaces.
public struct Int32 : IComparable, IFormattable, IConvertible, 
IComparable<int>, IEquatable<int>

Q2)How can the ToString() be overridden in the Int32 since it's not derived from Object class (based on the above definition)

I know that I must have missed out something which is very important. Can someone please tell me
Thanks.









Re: Visual C# Language Object class and value data type

Jeff Wharton

Structures are Value Types, as apposed to Reference Types, and all 'Types' in the .NET Framework derive from the System.Object base type. Therefore, Int32 has a ToString() method which can be overridden.

Have a read of http://msdn2.microsoft.com/en-us/library/2hf02550.aspx






Re: Visual C# Language Object class and value data type

brother14th

I just checked the "The C# programming language", authored by Anders Hejlsberg.

On page 102, it says that

"All value types implicitly inherit from the class System.ValueType, which, in turn, inherits from class object.

...."

Since Int32(struct) can inherit from System.ValueType which is a class. Does this mean that a user defined struct can also inherit from a class (I tried to create a struct and inherit it from a class, but it did not work.It seems that only the built-in value types can inherit from a class.Am I right )






Re: Visual C# Language Object class and value data type

Paul Louth

The reason why you can't derive from the value-types is not because they're "built in", but because they are 'sealed'.  When you mark a class as sealed no other class can derive from it.

http://msdn2.microsoft.com/en-us/library/88c54tsw.aspx





Re: Visual C# Language Object class and value data type

brother14th

Hi Paul,

I think you've misunderstood my questions. My last post may be a bit confusing. Sorry.

 My question is very straightforward.

Q1)Does this sentence "All value types implicitly inherit from the class System.ValueType."  still hold true if I substitute the All value types with a),b) and c)

a) Int32

b) Predefined Struct (e.g. double,float, bool and..)

C) User-defined Struct 

Note:

If b) is true and c) is false, can you kindly give me some hints how the Int32 implemented. That's how a predefined struct implemented.

If c) is  true, can you give me an example as I've tried the code below. But it won't compile.

struct data:Person
    {
        string nric;
        string name;
        public Strange(string name):base(name)
        {
        }

    }

 class Person
    {
        string name;
        public Person(string name)
        {
            this.name = name;

        }
    }

 Last but not least, thank you very much.

 

 

 

 

 






Re: Visual C# Language Object class and value data type

Paul Louth

1) Yes, all value types implicitly inherit the class System.ValueType.

a) Int32 is a ValueType

b) Predefined struct is a ValueType

c) User defined struct is a ValueType

Your code doesn't compile because Person is a class, structs can only inherit from interfaces as they are already inheriting the special class: ValueType.  So to support that C# would also have to support multiple inheritance.

(Also your constructor name doesn't match the name of the struct)

 





Re: Visual C# Language Object class and value data type

brother14th

Thanks, Paul. You're superb!




Re: Visual C# Language Object class and value data type

Paul Louth

No problem, glad to help :)



Re: Visual C# Language Object class and value data type

brother14th

Hi Paul,

Me again. I've thought of another question which comes from your previous answer. Let's say I define class a and class b as follow.

Class a

{...}

Class b:a

{...}

Here comes my questions.

Since

a)b inherits from class a

and

b) b also inherits from object class implicitly

Q1)Does b inherit from object class directly or indirectly from class a Which of the following is correct

I)Object<-Class a<-Class b

II)Object<-Class b and Class a<-Class b

if I) is true, does this imply that C# is clever enough to detect that if a user-defined class is "independent" then it would inherit directly from Object class otherwise it would inherit from Object class indirectly

"Independent" class: a class which does not have : when it's defined. e.g Class a.

if II) is true, does this mean that C# support multiple inheritance(class) implicitly






Re: Visual C# Language Object class and value data type

Paul Louth

The way inheritance works internally is by taking each data-member of the entire inheritance tree and creating a composite of each of those members.  It also has what's called a vtable, which is a table of referrences to all of the methods in the class, this vtable is also a composite of the inherited members.  So when class A derives from Object the compiler builds a brand new class definition with all of the members of A and of Object. 

So when you derive A from Object, then B from A.  B takes all of the members from A, which has already taken all of the members from Object.

This is still single-inheritance, as there's no branching of the inheritance tree.  ie.  you can't have C inheriting A and B:

    A    B

     \   /                     <----- Multiple inheritance, not allowed

      C

But you can have C inheriting B, which in turn inherits A:

    A

    |

    B                       <------- Single inheritance, this works fine

     |

    C

Although this might seem to have a similar result, you'll realise as you use inheritance more that you can't just insert classes into the single-inheritance hierarchy.  It starts to become difficult to manage pretty quickly.

Ways around this are to use interfaces or to make your objects work more like plug-in components.  eg.

// Interface method
class B : A, IC      <------- IC is an interface
{
      // Implement IC's members
}

// Component method
class B : A
{
     C c;

     // provide methods for accessing C here
}

I personally prefer the component approach, but each has their merits.





Re: Visual C# Language Object class and value data type

brother14th

Thanks. You're very helpful.





Re: Visual C# Language Object class and value data type

Figo Fei - MSFT

hi, all

Additionally, here is the knowledge about Types in C#:

1. all types are derived from System.Object.

2. types are regarded as two kinds: Reference types and Value types

3. all value types are derived from System.ValueType and allocated from threadí»s stack,  while reference types are allocated from the managed heap.

4. some commonly used types are regarded as Primitive Types that many compilers allow code to manipulate them using simplified syntax. such as sbyte, byte, short, ushort, int, uint, long, ulong, char, float, double, bool, decimal, object, string.

And int has implemented the interface IConvertible which has ToString() method, although it's not directly derived from Object class.

BR