abhi
abhi

Reputation: 3136

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?

Upvotes: 1

Views: 107

Answers (5)

Justin Pihony
Justin Pihony

Reputation: 67065

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)

Upvotes: 1

Sam Axe
Sam Axe

Reputation: 33738

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>(...);

Upvotes: 1

HenryZhang
HenryZhang

Reputation: 1358

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.

Upvotes: 1

Darin Dimitrov
Darin Dimitrov

Reputation: 1038710

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");

Upvotes: 1

Eric J.
Eric J.

Reputation: 150108

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");

Upvotes: 2

Related Questions