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

2 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