Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

A parameter type ruined my Func<shui>

Tags:

c#

generics

I got yelled at for trying to use the word question in the title so this is what I came up with. At any rate, this is a purely academic question about parameter types.

OK, so here is what I get.

using System;
namespace TypeParamTest 
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            PrintType(1, new object());
            Console.ReadKey();
        }
        static void PrintType<T, Ttwo>(T first, Ttwo second)
        {
            Console.WriteLine(typeof(T) + " : " + typeof(Ttwo));
        }
    }
}

That is clear and unambiguous to me (Astute readers will recognize this as a simple extension of an example found on page 249 of C# in Depth). I totally get what is happening, the compiler says there is some type (to be defined for T and then for Ttwo). What I don't understand is why on page 65 the example only has one type parameter.

List<TOutput> ConvertAll<TOutput>(Converter<T, TOutput> conv);

How does the compiler know about T in this case?

like image 429
Kenn Avatar asked Jan 17 '12 01:01

Kenn


1 Answers

We don't have enough information to answer for sure. Maybe T is defined on the surrounding class?

public class SomeClass<T> {
    List<TOutput> ConvertAll<TOutput>(Converter<T, TOutput> conv);
}

If this is an example of List<T>'s ConvertAll method, then my suggestion above is the case.

like image 116
Chris Shain Avatar answered Oct 05 '22 23:10

Chris Shain