0

Look into following code

public class ABC
{
    public ABC(int a)
    {
    }
}

public struct XYZ
{
    public XYZ(int a)
    {
    }
}

public class Test
{
    //This is invalid.
    ABC _abc = new ABC();

    //This is valid. Why?
    XYZ _xyz = new XYZ();
}

Why struct dont required default constructor where as class required same?

Ankush Madankar
  • 3,689
  • 4
  • 40
  • 74
  • 1
    [Why can't I define a default constructor for a struct in .NET?](http://stackoverflow.com/questions/333829/why-cant-i-define-a-default-constructor-for-a-struct-in-net) – Tim Schmelter Aug 29 '13 at 12:24

3 Answers3

3

There is always a parameterless constructor in a struct - and you can't define your own one. It will always initialize all fields to their default values. This is effectively a requirement of the CLR, although the CLR itself doesn't refer to this as a constructor, and is described in section 11.3.8 of the C# spec. (Although C# doesn't allow you to declare your own parameterless constructor for structs, the CLR does - and it's sometimes called. See my blog post on the topic for more information.)

The value created by calling the parameterless constructor on a struct is always the same as an "uninitialized" value in an array or an instance/static field.

Classes, however, have a different "default" value, as a field (or array element) of a reference type will be null by default. There is no guaranteed way of creating an instance of a class without specifying any values. If you specify any constructors yourself for a class, the C# compiler will not provide a default constructor, as described in section 10.11.4 of the C# spec.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • The fact that there exists a parameterless constructor is I think less relevant than the fact that normal ways of creating a struct instance (e.g. declaring a field of a structure type) don't involve any code within the struct itself. As you've said on your blog, the Framework doesn't require structs to have do-nothing parameterless constructors, but the fact that struct instance creation doesn't involve struct code means there'd be little to gain by having a struct do anything else. – supercat Aug 30 '13 at 16:59
  • @supercat: I think all those aspects are at least somewhat relevant - in particular, the presence of a parameterless constructor (in C# terms) is very, very relevant to the question of "why is this code valid?" for `new XYZ()` – Jon Skeet Aug 30 '13 at 17:02
  • I see your point about what's "more" relevant to the direct question asked, but would nonetheless suggest that the original poster (and many other people) might find a link to your blog post to be helpful and informative. – supercat Aug 30 '13 at 17:16
  • @supercat: Sure, will add that now. – Jon Skeet Aug 30 '13 at 17:18
2

Because the default constructor of a class (reference type) is only exposed if an explicit one is not implemented (that is, it doesn't exist). A struct (value type) doesn't even need to be new'd, so to speak. You can use a variable that represents a struct without it - i.e. it won't be null, anyway (that is, it needn't exist at all).

Grant Thomas
  • 44,454
  • 10
  • 85
  • 129
1

When a storage location (variable, field, array slot, etc.) of a class type is first created, it holds null. When a storage location of a structure type is first created, it holds an instance of the type in which every byte has been set to zero. Unlike C++, .NET provides no means for a type to exercise any say over when storage locations of that type may be created, nor what should happen when they are.

If Foo is a class type and code creates an array bar = new Foo[100], the array will be created with 100 slots that don't contain references to Foo (they're initially null). Code that wants to make any array slot hold a reference to a Foo will have to somehow get its hands on one, and the only way any references to Foo will exist is if someone asks the class to create one (by calling its constructor).

By contrast, if Moo is a structure type, creating an array boz = new Moo[100], the array will be created with 100 slots, each one of which is a Moo instance. Whereas a Foo can hold a value (null) which doesn't refer to an instance of Foo, none of the array slots will be capable of holding anything other than Moo instances. Since creating an array of Moo inherently creates instances of Moo without the type having any say in the matter, there's really no mechanism by which a struct type could assert control over instance creation.

supercat
  • 77,689
  • 9
  • 166
  • 211