-3

Default constructor is defined for "Class" and for "Struct". But If I declare a class "Point" which has only 1 constructor, and that too a parametrized one, the compiler would not allow me to write

Point p = new Point()

However, If I have a struct "Point", which again has only 1 parametrized constructor, the compiler will allow me to write

Point p = new Point()

The question I am asking is why the compiler allowed struct to create an object and stopped the class to create an object?

Note: I am writing this code on C# 6

IanS
  • 15,771
  • 9
  • 60
  • 84
Rajat
  • 410
  • 4
  • 19
  • 5
    And what is your question? You've failed to ask one. – Damien_The_Unbeliever May 04 '17 at 08:27
  • 1
    Correct, this is a behavior of structs. See [here](http://stackoverflow.com/q/575901/51) – Yaakov Ellis May 04 '17 at 08:30
  • 2
    And yet another question with answer from Jon Skeet - http://stackoverflow.com/questions/333829/why-cant-i-define-a-default-constructor-for-a-struct-in-net – Bakudan May 04 '17 at 08:32
  • I have updated the question, I hope it makes sense now. – Rajat May 04 '17 at 10:40
  • Even with your edit, you're still not *asking a question*. All you're doing at the moment is *making an observation*. You have correctly observed that there is a difference between `class` and `struct`, in that `struct`s always have a parameterless constructor (and until C#7, you're not allowed to write it yourself) - but that's covered by any basic introduction to the concept of `struct`s you may care to read. – Damien_The_Unbeliever May 04 '17 at 10:52
  • Ok, I understand your point. The question I am asking is why the compiler allowed struct to create an object and stopped the class to create an object? – Rajat May 04 '17 at 11:26
  • 1
    Actually, [Eric Lippert wrote a great comment](http://stackoverflow.com/questions/43121625/nullable-types-comparison#comment73323385_43121625) regarding "why" questions, i.e. _"An adequate answer to "why is the language this way" would be to re-hash the hours and hours of debate we had about it."_. – default May 04 '17 at 11:39
  • 1
    can you send your C# Code to understand your Question – Minhaj Patel May 07 '17 at 08:04

3 Answers3

0

Because a struct is a value type, its default value is not null, like a reference type, but an instance with all of its members set to their default values.

So even if you were only allowed to call the parameterized constructor, you could still create a default instance, which would be exactly the same as creating the struct with a default constructor.

For example, given this struct:

public struct Point
{
    public int X { get; private set; }
    public int Y { get; private set; }

    public Point(int x, int y)
    {
        X = x;
        Y = y;
    }
}

Even if you couldn't do this:

var p = new Point();

You could still do this:

var p = default(Point);

If Point were a class this wouldn't get you an instance, it would get you null. But with a struct it gets you an instance.

So there's no reason to prevent use of the default constructor and pretend that you can't create a default instance.

Scott Hannen
  • 27,588
  • 3
  • 45
  • 62
-1

Answer to your question(why there is no compile error) you can find on MSDN:

In C#, classes and structs are semantically different.

( https://learn.microsoft.com/en-us/dotnet/articles/csharp/programming-guide/classes-and-structs/using-structs )

A struct cannot declare a default constructor (a constructor without parameters) or a destructor.

( https://learn.microsoft.com/en-us/dotnet/articles/csharp/programming-guide/classes-and-structs/structs )

Each value type has an implicit default constructor that initializes the default value of that type. For information about default values of value types, see Default Values Table.

( https://learn.microsoft.com/en-us/dotnet/articles/csharp/language-reference/keywords/value-types )

Rekshino
  • 6,954
  • 2
  • 19
  • 44
-1

When you use classes, you can define whether its object can or cannot be initialized in any particular way. However, when it comes to struct, it should always be able to be initialized without parameters.

It is best illustrated by the fact that classes can be initialized with null when you can't initialize them in specified way, while there is no such option for structs. But structs can be initialized with default keyword, which basically calls the parameterless constructor.

Edited: While I was outlining the common rule for struct as valuetype, there are actually several ways to initialize struct with null (or simulate it). Th most common is, of course, Nullable<>, and it is sufficient for most, if not all, purposes. For those who are curious about how it works and what are the other ways to set null to the struct, I recommend C# in Depth: Chapter 4. Saying nothing with nullable types by Jon Skeet. Additional information could be found here.

Community
  • 1
  • 1
Dmitry Volkov
  • 1,347
  • 1
  • 18
  • 33
  • 1
    I thought the bounty reason was _"Looking for an answer drawing from credible and/or official sources"_. I am surprised that the awarded asnwer is an _"In my opinion..."_ type of answer. – DDan May 12 '17 at 03:21
  • @Dmitriy Volkov Which category belongs nullable struct? It is a struct and can be set to null: https://msdn.microsoft.com/en-us/library/b3h38hb0(v=vs.110).aspx – Rekshino May 12 '17 at 10:12
  • @Rekshino Well, yes, there are several ways to set null to a struct, as Jon Skeet discusses in his book. Generally speaking, it very rarely comes to implementing such mechanisms, but if you want to learn about it, you can read about it in "C# in Depth: Chapter 4. Saying nothing with nullable types" by Jon Skeet. – Dmitry Volkov May 12 '17 at 12:31
  • @Rekshino Here, found something that might interest you if you are curious about such mechanisms. http://stackoverflow.com/questions/2503811/how-are-nullable-types-implemented-under-the-hood-in-net – Dmitry Volkov May 12 '17 at 12:40
  • I am not curious about it and I do not need to implement it, it's a standard since c# 2.0. I mean, that in your answer you say, that struct can not to be set to null. – Rekshino May 12 '17 at 12:50