English 中文(简体)
C# Deriving Generic Methods
原标题:
  • 时间:2009-11-13 15:43:54
  •  标签:
  • c#
  • generics

When i need to pass a Generic Type I can use the Syntax

(Example : Ofcourse it is not a Generic Method)

public void Sample(T someValue)
{
  ......
}

What is the benefit of declaring Sample<T> ?

I mean

public void Sample<T> (T someValue)
{
    ......
}
最佳回答

For this to work:

public void Sample(T someValue)
{
  ......
}

The type T has to be declared in the system already. And the method will only accept the type T or its derivatives.

By declaring this:

public void Sample<T> (T someValue)
{
    ......
}

you say the method will accept any type that comes.

问题回答

Generic types and generic methods are very different things. It sounds like you have a generic type:

class SomeType<T> {
    public void Sample(T someValue) {...}
}

and are discussing having a generic method inside it:

class SomeType<T> {
    public void Sample<T>(T someValue) {...}
}

These are very different. In the first, inside Sample, then T means "the T that got passed to SomeType<T>". In the second, inside Sample, this is a separate and independent T - "the T that got passed to Sample<T>". In fact, calling it T (in this case) is a mistake. You could have, for example:

var obj = new SomeType<int>(); // here T for SomeType<T> is int
obj.Sample<decimal>(123.45M); // here T for Sample<T> is decimal

Note that there is no easy way (within Sample<T>) of saying "the T in SomeType<T>" - hence why you should rename the method s generic type parameter.

There are valid scenarios for this type of scenario (generic methods on generic types), for example (and note the new name):

class SomeType<T> {
    public void Sample<TActual>(TActual someValue) where TActual : T, new() {...}
}

This allows us to do some very interesting things in terms of inheritance, etc - or you might want a generic method that has little or no relation to T. That is fine too.

Consider the following:

class SomeClass<T>
{
    public void Sample(T value)
    {
        // code goes here
    }
}

or this:

class SomeClass
{
    public void Sample<T>(T value)
    {
        // code goes here
    }
}

In the first case, for all calls to Sample in a specific instance T will be the same type. In the second case each call in a specific instance can have its own type of T, since the generic type argument is supplied with the method call.

The second approach can have many uses, but one that I have used myself sometimes is in factory methods:

public static class SomeFactory
{
    public static T CreateSomeObject<T>()
    {
        T result = Activator.CreateInstance<T>();
        // perform any extra initialization
        return result;
    }
}

You can use the 2nd example if your class isn t generic. It means you can make just that method generic. If your class is generic, you should use your 1st example.





相关问题
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. ...

热门标签