find a location for property in a new city

Monday, 28 February 2011

Multiple generic constraints in C#

I had some trouble using my nice new generic class in C#. I want to ensure one of the types passed in inherited from a particular class. This class can be instantiated using the new key word however the compile time is not aware of this

I found, using this unnatural syntax, it is possible to constrain the type parameters in multiple ways. Like so:

public class MyClass <T, U> : MyBaseClass <T> where T : class where U : AnotherClass, new()
{
    public AnotherClass AnotherClass { get; set; }
    public MyClass()
    {
        AnotherClass = new U();
    }
    //my class
}

This works fine and can be very useful at times

Follow britishdev on Twitter

7 comments:


  1. ارخص شركة شحن عفش

    نقل عفش من جدة الى مكة نقل عفش من جدة الى مكة
    شركة نقل عفش من جدة الى الامارات نقل عفش من جدة الى الامارات
    افضل شركة نقل عفش من جدة الى الرياض نقل عفش من جدة الى الرياض
    شركة نقل عفش من جدة الى الدمام نقل عفش من جدة الى الدمام
    شركة نقل عفش من المدينة المنورة الى مكة نقل عفش من المدينة المنورة الى مكة

    ReplyDelete
  2. Good day very nice web site!! Guy... Excellent.. Wonderful.. will bookmark your blog and take the feeds additionally. Im satisfied to search out a lot f helpful information right here in the publish; we need work out extra techniques on this regard, thanks for sharing.Your article is extremely striking. Beautifully written, too. I found so many interesting things on this blog and will certainly recommend it to all. custom essay writing service Its fastidious dialogue on the topic of this piece of writing at this place at this weblog, I have read all that, so now me also commenting at this place.

    ReplyDelete
  3. You can't inherit someone's skills but you can learn. If you do not want to anonymize yourself in class, I would advise you to seek help from Coursework Writing Service experts.

    ReplyDelete
  4. Great post! You discuss related to multiple generic constraints in C#. I like your post. Further, We bring a best service of Pressure washing services in Gainesville for you as your desire needs.

    ReplyDelete
  5. You deliver information related to C# in detail. I really like your post. Moreover, We provide the best services of Miami beach boat rentals for making your time more beautiful and memorable.

    ReplyDelete
  6. Great post! You share informative information related to C#. You discuss multiple generic constraints in detail. Moreover, We provide the best and affordable Residential Mover Services in Atlanta GA as your desire requirements.

    ReplyDelete
  7. I found informative information in this post related to generic constraints in C#. Thank you for sharing information. Further, Commercial Locksmith Services provide the best locksmith work as your desired needs.

    ReplyDelete