简体   繁体   中英

Calling a method in a C# class

Here's a piece of code in C# that I am trying to understand. A class has the following method available in an interface.

T GetLookupValue<T, S>(string sTName, string sFName, string sLuName, S value);

When I use dotPeek to look at the usage of this class, it shows this.

 public T GetLookupValue<T, S>(string sTName, string sFName, string sLuName, S value)
{
  return (T) this.db.a(sTName, sFName, sLuName, false, (object) value, false, false);
}

How do I call this method? What do I need to substitute T and S with?

This method presents itself as a generalized lookup function. If the implementation can really handle any type, then you can use any type at all for S and T.

Examples:

string result = GetLookupValue<string, int>("tname", "fname", "luname", 42);

MyClass result = GetLookupValue<MyClass, string>("tname", "fname", "luname", "blah");

Here is a great introduction to C# generics , which is what this is.

Basically, you just substitute T and S with whatever you want. T is your return type, and S will be the type that is used for the final parameter ( value )

For example:

var myObject = GetLookupValue<MyObject, MyOtherObject>("sTName", "sFName", "sLuName", (MyOtherObject)myOtherObject);

This allows for more robust code. Where you can mold a method to be whatever you need it to be

Generics are how you can put anything in a List<T> :

List<String> stringList = new List<String>();
List<int> intList = new List<int>();
...

At compile time, the values for your generics are built into your code. So, if you did a dotPeek on my above example, you would see something like this:

public MyObject GetLookupValue<MyObject, MyOtherObject>(string sTName, string sFName, string sLuName, MyOtherObject value)
{
  return (MyObject) this.db.a(sTName, sFName, sLuName, false, (object) value, false, false);
}

***However, in writing this out, having the parameter value as a generic seems fairly pointless as it is just cast to object ....they might as well have made it T GetLookupValue<T>(string sTName, string sFName, string sLuName, object value); . (which allows ends up boxing a Value Object (ie int, double, etc)

How do I call this method?

This will depend on what types you want to use. For example:

int result = GetLookupValue<int, string>("tname", "fname", "luname", "some value");

The types T and S are Types. To call this method and store the results, use:

SomeClass returnedObject = GetLookupValue<SomeClass, SomeOtherType>("", "", "", "");

The method will return a type equivalent to whatever you put in for T.

it appears that T is your return type, and S is a type that's used internally as some value.

So, you would need to specify the TYPES for T and S, thusly:

SomeType result = GetLookupValue<SomeType, SomeValueType>(...);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM