English 中文(简体)
Can a Type Derive from Itself?
原标题:

When enforcing a Generic constraint

class GenericTest
{
    public void Sample<T>(T someValue) where T:Racer,new()
    {
        Console.WriteLine(someValue.Car);
    }
}

The Type T should be derived from the Base Type Racer (Correct me if anything wrong).

public class Racer
    {
        string name;
        string car;
        public Racer() { }
        public Racer(string name, string car)
        {
            this.name = name;
            this.car = car;
        }

        public string Name
        {
            get { return name; }
        }
        public string Car
        {
            get { return car; }
        }

    }

In "Main()" I am executing as

static void Main(string[] args)
    {

        List<Racer> rcr = new List<Racer>();
        rcr.Add(new Racer("James","Ferrari"));
        rcr.Add(new Racer("Azar","Bmw"));

        rcr.Sort(delegate(Racer a, Racer b)
        {return a.Name.CompareTo(b.Name); });

        GenericTest tst = new GenericTest();

        tst.Sample<Racer>(rcr[0]);

        Console.ReadLine();

    }

My Question is:

The constraint I implemented is where T:Racer,new(),So T should be derived from Racer. But In Main() I am passing ( tst.Sample<Racer>(rcr[0]);) the type "Racer".The code is working.

How come the Racer be derived from Racer?

最佳回答

MSDN: http://msdn.microsoft.com/en-us/library/d5x73970.aspx

where T : <base class name>
The type argument must be or derive from the specified base class.

The point to note is must be or derive from. That means Racer and any class that derives from Racer.

问题回答

Derived from is the wrong way to read this constraint. It is better read as "is convertible to". This is why Racer works in this scenario.

EDIT : More details

This is covered in section 10.1.5 in the C# language spec. ...

If a constraint is a class type, an interface type, or a type parameter, that type specifies a minimal “base type” that every type argument used for that type parameter must support.

What you are saying with that constraint is that the generic type argument T must be Racer, or a type that descends from Racer, not that it must be a descendant only.

One way to look at it is "Can an object of type T be assigned to a Racer reference?"

The constraint would be true if the object that you are passing in can be cast to the type you ve specified on the constraint.





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签