1

C# 6 allows declaration of parameterless constructors on structs. However they must be public and there are situations where one would like to force other developers to use parametrized constructor, so I'm wondering would it be ok from CLR perspective to make parameterless constructor unusable by throwing an exception inside, so it would look like that:

public struct Foo
{
    public Foo()
    {
        throw new InvalidOperationException("This constructor is not supported.");
    }

    public Foo(int i) { ... }
}
6
  • That would so blow up when you try to declare an array of Foo. new Foo[1]; What constructor will be run to set the "default state"? Commented Mar 17, 2015 at 17:19
  • I have tried that and didn't blow. Can you please post a blowing code snippet? :) Commented Mar 17, 2015 at 17:21
  • I don't have a compiler to test this. Try Foo[] array = new Foo[5]; and see what happens. Commented Mar 17, 2015 at 17:22
  • I have tried this and it works. Commented Mar 17, 2015 at 17:23
  • 1
    I think that it won't work to force a developer to use the parameterized constructor, because instead of using the parameterless constructor with a struct you could just do default(Foo) instead to avoid calling a constructor. Commented Mar 17, 2015 at 17:57

1 Answer 1

3

You could do it. As to whether or not it's a good idea is rather subjective. (I would strongly discourage it's use, but clearly I can't stop you from doing this.)

If your goal is to ensure that there is never a situation where the object is constructed without a call to the parameterized constructor, and that the fields are never left at their default values, this doesn't do that. There are ways, such as using default(Foo) to create an instance of the object without calling any of the user-defined constructors.

If you're asking if the CLR is going to crash and the world will implode into a black hole because you threw an exception from the constructor of a struct, then no, that's not going to happen. It'll throw, and work its way up the call stack until caught, just like any other thrown exception, when this constructor is called.

Sign up to request clarification or add additional context in comments.

3 Comments

I believe it's fair enough for value types to use default(Foo) to get default value. If someone use default(Foo) it means he knows what he is doing. For preliminary checking such solution seems to be enough IMO but it's great to know that our universe won't collapse :)
@user1121956 That's just one example, not the only situation. The point is that you cannot code the type under the assumption that one of your constructors was called, because it very possibly wasn't. It's also quite possible for default or other similar modes of construction will be used by a generic method, allowing inexperienced users to observe very unusual behaviors. Then of course there's the obvious fact that you're exposing a constructor that doesn't actually work; the API is a lie, as it were.
Good points. I will set some default value in parameterless constructor then, instead of throwing. Thanks for the answer :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.