Default case
Suppose the following sample problem: I want to create a method that simply displays the number of elements in any collectionList<>
.
I created the following static class with one method:
public static class MyClass
{
public static void MyMethod<T>(T obj) where T : List<int>
{
Console.WriteLine(obj.Count);
}
}
Note that T
is a subclass List<int>
. Now I can call:
List<int> li = new List<int>();
MyClass.MyMethod<List<int>>(li);
Now, the IDE tells me that "The specification of the type argument is redundant." It can infer a type from use:
List<int> li = new List<int>();
MyClass.MyMethod(li); // OK. li is List<int>, type argument is not required
General case
As far as you remember, I want to display the counter of List
anyone . Something like this would be great:
public static void MyComplexMethod<T>(T obj) where T : List<any>
{
Console.WriteLine(obj.Count);
}
However, this is not the correct syntax. I have to implement the following method:
public static void MyComplexMethod<T1, T2>(T1 obj) where T1 : List<T2>
{
Console.WriteLine(obj.Count);
}
" ":
List<int> li = new List<int>();
MyClass.MyComplexMethod(li);
MyClass.MyComplexMethod<List<int>>(li);
MyClass.MyComplexMethod<List<int>, int>(li);
MyClass.MyComplexMethod<List<double>, double>(new List<double>());
MyClass.MyComplexMethod<List<string>, string>(new List<string>());
MyClass.MyComplexMethod<List<string>, double>(new List<string>());
, , . List<int>
- T1 List<int>
, T2 - int
, . ? (where T : List<any>
)?
- , . , , WCF, :
public static void Call<TServiceProxy, TServiceContract>(Action<TServiceProxy> action)
where TServiceProxy : ClientBase<TServiceContract>, new()
where TServiceContract : class
{
TServiceProxy serviceProxy = new TServiceProxy();
try
{
action(serviceProxy);
serviceProxy.Close();
}
catch (Exception ex)
{
serviceProxy.Abort();
throw;
}
}
Service.Call<EchoServiceClient>(x => {
int v = DateTime.Now.ToString();
x.Echo(v);
});
Service.Call<EchoServiceClient, IEchoService>(x => {
int v = DateTime.Now.ToString();
x.Echo(v);
});
where TServiceProxy : ClientBase<TServiceContract>
serviceProxy.Abort()
. , where TServiceProxy : ClientBase<any>
, TServiceContract
- where
.