473,414 Members | 1,848 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,414 software developers and data experts.

c# generic and methods

Hi all!

I was wondering if it is possible to create methods within generic classes,
based on what type class operates on:

e.g.

public class ListEx<T: List<T>
{
public void MyMethod<int>(int param1, int param2)
{
}

public void MyMethod<string>(string param1, string param2)
{
}
}

So, MyMethod<intwould only be available if instance of class is declared:
ListEx<int>

and MyMethod<stringwould only be available if class declared like:
ListEx<string>

Is this at all possible with generics?

Thanks in advance!

Best regards, Jure
Aug 22 '07 #1
3 1394
No you can't overload generics methods like this.

Also you don't need to do this with generics.

You can create 2 methods like
public void MyMethod(int param1, int param2)
{
Console.WriteLine("int");
Console.WriteLine(param1.ToString());
Console.WriteLine(param2.ToString());
}
public void MyMethod(string param1, string param2)
{
Console.WriteLine(param1.ToString());
Console.WriteLine(param2.ToString());
}

and then you call them like this.

ListEx<stringlist = new ListEx<string>();
list.MyMethod(4, 5);
list.MyMethod("a", "b");

"Jure Bogataj" <ju**********@mikrocop.com>, iletisinde şunu yazdı,
news:e8**************@TK2MSFTNGP06.phx.gbl...
Hi all!

I was wondering if it is possible to create methods within generic
classes, based on what type class operates on:

e.g.

public class ListEx<T: List<T>
{
public void MyMethod<int>(int param1, int param2)
{
}

public void MyMethod<string>(string param1, string param2)
{
}
}

So, MyMethod<intwould only be available if instance of class is
declared:
ListEx<int>

and MyMethod<stringwould only be available if class declared like:
ListEx<string>

Is this at all possible with generics?

Thanks in advance!

Best regards, Jure
Aug 22 '07 #2

On Aug 22, 12:05 pm, "Jure Bogataj" <jure.boga...@mikrocop.comwrote:
I was wondering if it is possible to create methods within generic classes,
based on what type class operates on:
In your case, it sounds like it's a better idea to keep the class
generic and the methods nongeneric, like so:

class ListEx<T: List<T{
void MyMethod(T t) {
// ...
}
}

You can use generic constraints to define what types T ListEx will
accept.

Aug 22 '07 #3
Thank you both!

I've solved my problem using generic type (T) as type of parameters.

Best regards!
"Jure Bogataj" <ju**********@mikrocop.comwrote in message
news:e8**************@TK2MSFTNGP06.phx.gbl...
Hi all!

I was wondering if it is possible to create methods within generic
classes, based on what type class operates on:

e.g.

public class ListEx<T: List<T>
{
public void MyMethod<int>(int param1, int param2)
{
}

public void MyMethod<string>(string param1, string param2)
{
}
}

So, MyMethod<intwould only be available if instance of class is
declared:
ListEx<int>

and MyMethod<stringwould only be available if class declared like:
ListEx<string>

Is this at all possible with generics?

Thanks in advance!

Best regards, Jure

Aug 22 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Jim Newton | last post by:
hi all, i'm relatively new to python. I find it a pretty interesting language but also somewhat limiting compared to lisp. I notice that the language does provide a few lispy type nicities, but...
3
by: SimonH | last post by:
Hi all, I would like to make a generic set of methods that could be called regardless of the database behind the scenes. One of the methods I would like would take a string sql statement and...
2
by: ljlevend | last post by:
I've noticed that in VS.NET 2.0 Beta 1 that none of the methods in System.Collections.Generic.List are overridable. In my app I currently have over 50 strongly typed ArrayLists that inherit from...
3
by: Tigger | last post by:
I have an object which could be compared to a DataTable/List which I am trying to genericify. I've spent about a day so far in refactoring and in the process gone through some hoops and hit some...
0
by: Mikkel Blanné | last post by:
I haven't been able to find any references to using this combination of technologies (remoting + generic methods + method overloading). I don't think the problem has to do with C#, but I couldn't...
9
by: Steve Richter | last post by:
in a generic class, can I code the class so that I can call a static method of the generic class T? In the ConvertFrom method of the generic TypeConvert class I want to write, I have a call to...
13
by: rkausch | last post by:
Hello everyone, I'm writing because I'm frustrated with the implementation of C#'s generics, and need a workaround. I come from a Java background, and am currently writing a portion of an...
10
by: phancey | last post by:
I'm quite new to generics. I have 2 generic classes: MyClass<Tand MyOtherClass<T>. MyClass<Thas 2 public Add methods Add(MyOtherClass<T>); Add(MyOtherClass<Wrapper<T>>); (Wrapper<Tis another...
26
by: raylopez99 | last post by:
Here is a good example that shows generic delegate types. Read this through and you'll have an excellent understanding of how to use these types. You might say that the combination of the generic...
0
by: =?Utf-8?B?TW9ydGVuIFdlbm5ldmlrIFtDIyBNVlBd?= | last post by:
"Anders Borum" wrote: Hi Anders, I'm afraid the GetMethod() does not currently support filtering on generic parameters so you will have to loop through the existing methods using...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.