3

Is it possible to enforce rules or throw an error when using object initializers in C#? I'd like to throw a compiler error or warning if an object is initialized but is missing a certain property.

public class Party
{
    public string Name { get; set; }
    public string Date { get; set; }
    public Location Location { get; set; }
}

public class SignUpForParty
{
    public void DoSomething()
    {
        Party party = new Party()
        {
            Name = "New Years Party!",
            Date = "Dec 31, 1999"
            // Show WARNING/ERROR here because no Location given
        };
    }
}

Essentially, I'm looking for a way to ensure that all objects of type Party are created with valid data for every instance.

Obviously I could do this with overloaded constructors, but in some cases I have a lot of properties and writing constructors to match is messy. I'd like to follow a cleaner C# style.

Obj p = new Obj(1, 2, 3,...n); // too many properties to be pretty
2
  • 1
    You can put the each constructor parameter on its own line, and that would look pretty similar. You mention constructor overloads, but you also said you want all properties set, which suggests a single uber-constructor.
    – dlev
    Commented Mar 29, 2012 at 1:16
  • Thanks for the answers. I am confusing object initiators with constructors. I wanted the best of both worlds - constructing an object with enforcement, but with the easy style of object initiators. I didn't want to a bunch of uber constructors. What @reed-copsey is getting at is also true in my scenario, in that what is required should be in a constructor and optional stuff should be left for the initiators.
    – daviddeath
    Commented Mar 29, 2012 at 2:53

5 Answers 5

4

Obviously I could do this with overloaded constructors, but in some cases I have a lot of properties and writing constructors to match is messy. I'd like to follow a cleaner C# style.

Object initializers really shouldn't be considered alternatives to writing a constructor.

You should always include constructors in your types if you have requirements like this. It is a good idea to create a default set of constructors (or use optional arguments on one constructor) which at least provide a guarantee that the object will always be created in a valid, meaningful state.

Object initializers are helpful for optional properties, but shouldn't be relied upon for requirements of your type itself.

3

You cannot force every property to be initialized with an object initializer.

Even if you could, a consumer of the object could provide default (0, null, ...) values. Depending on your needs, consider validating object state at key times (e.g. before it can be saved to a database).

If you go that route, have a look at the IDataErrorInfo interface.

2

If your type is not valid when only 2 properties are set then you need to fix your design, not emit an error.

You provide a default constructor, which tells me that I don't have to set anything to use the object after initialization. You provide getters and setters for each property, again, implicitly telling users of your class that it is ok to set on but not the other.

If this is not the case then I suggest you provide a constructor which forces me to supply all three values. Yes, I can still use (null, null, null), but you could check for that and throw an error.

Also, if PropertyA is dependent on PropertyB then either

A) Only one of them should have a setter, or

B) There should be logic in the setter of each to properly initialize the other after the value changes.

This is a design problem, not a language problem. You cannot force the initializer syntax to work differently than how it was spec'd.

Obj p = new Obj(1, 2, 3,...n); // too many properties to be pretty

Code isn't supposed to be 'pretty', it is supposed to work. Even then, a constructor which takes a few arguments is 'ugly'? Huh? Don't buy into the hipster nonsense, write code that works and works well.

1
  • Agreed on all points, thank you. "...you provide a constructor which forces me to supply all three values" - My question would have probably been better understood by asking, "can I provide behavior of a constructor with initializers?" Which, I've learned, is no.
    – daviddeath
    Commented Mar 29, 2012 at 3:11
0

The only way I could see this implemented is if there was some event (or equivalent) that was raised when the object initializer was completed. There is currently a Connect request for something to this effect.

Unfortunately, I didn't make the cut for .NET 4.5:

Thank you for your suggestion.

This is a great idea, and has the nice property that it doesn't add to the language surface - it just makes object initializers smarter. In principle that would probably make it a breaking change, but that is something we can look into.

Unfortunately we cannot add any more to the release we are currently building, so I am going to resolve as Won't fix for this release. However, I am capturing the suggestion on our list of features for future discussion.

Thanks again!

Mads Torgersen, C# Language PM

Maybe it'll make it's way into .NET 5+.

2
  • This is a very interesting idea. In a lot of ways, this is almost what I'm looking for.
    – daviddeath
    Commented Mar 29, 2012 at 2:49
  • @daviddeath - Unfortunately there are currently no workarounds that provide this ability so I can only suggest voting on the connect request in hopes that it'll make it into the next version. The only thing's I can see as workable today (given our current constraints) would be to use the ideas suggested. Maybe soon though.
    – M.Babcock
    Commented Mar 29, 2012 at 2:54
0

What about Code Contracts?. This will assert not only that you assigned a value, but you could also specify valid ranges.

Or for check at runtime with debug builds only, you could use Debug.Assert(...) calls to achieve the same as above.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.