1.structs derive from System.ValueType whereas classes derive from System.Object or one of its descendants. Of course, System.ValueType again derives from System.Object, but that's beside the point
2. Structs cannot derive from any other class/struct, nor can they be derived from. However, a struct can implement any number of interfaces. But when you treat Struct as a interface, it gets implicitly boxed.
Example:-
struct Foo : IFoo
{
int x;
}
and then: IFoo iFoo = new Foo();
an instance of Foo is created and boxed. All interface method calls then execute only on the boxed instance.
3. C# does not allow structs to have a default parameterless constructor( But CLR allows).
Example:-
struct Foo
{
int x;
public Foo(int x)// C# doesn't allow parameterless constructor
{
this.x = x;
}
}
class FooTester
{
[STAThread] static void Main(string[] args)
{
Foo f = new Foo();//though you are calling a parameterless constructor here, all it does is initialize the struct's fields to null/zero
}
}
No comments:
Post a Comment