473,813 Members | 4,215 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Method overloading

Not that this is a real-life problem but how would you call a "second" Foo
method ?

using System;

public class Boo<T>
{
public void Foo(int index)
{
Console.WriteLi ne("Foo(int)") ;
}

public void Foo(T key)
{
Console.WriteLi ne("Foo(T)");
}
}

internal static class Program
{
private static void Main()
{
Boo<intboo = new Boo<int>();
boo.Foo(0);
}
}
Jun 27 '08 #1
3 1078
Take away the compiler's ability to know it as an integer:

private static void Main()
{
Boo<intboo = new Boo<int>();
boo.Foo(0);
CallFooByKey(bo o, 0);
}
static void CallFooByKey<T> (Boo<Tboo, T key)
{
boo.Foo(key);
}
Jun 27 '08 #2
I should note that in C# 3 you can make this slightly more intuitive via
"extension methods" - but it is also better to avoid it by naming the
methods differently to begin with (even the language spec highlights
this as a problem scenario, best avoided...)

private static void Main()
{
Boo<intboo = new Boo<int>();
boo.Foo(0);
boo.FooByKey(0) ;
}
public static void FooByKey<T>(thi s Boo<Tboo, T key)
{
boo.Foo(key);
}
Jun 27 '08 #3
This is really a tricky situation. but in real time, you will get to know
the object by specific entity. anyhow, if you want to eliminate the
confusion between the first method and second method, i recommend the
following suggtion.

public void foo(T key)
{
if (key.GetType(). Equals(typeof(i nt)))
foo((int)key);
else
Console.WriteLi ne("Foo(T)");
}

HTH
"pagerintas pritupimas" <or*@com.netwro te in message
news:uN******** *****@TK2MSFTNG P05.phx.gbl...
Not that this is a real-life problem but how would you call a "second" Foo
method ?

using System;

public class Boo<T>
{
public void Foo(int index)
{
Console.WriteLi ne("Foo(int)") ;
}

public void Foo(T key)
{
Console.WriteLi ne("Foo(T)");
}
}

internal static class Program
{
private static void Main()
{
Boo<intboo = new Boo<int>();
boo.Foo(0);
}
}
Jun 27 '08 #4

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

Similar topics

4
9330
by: John Smith | last post by:
can we overload a javascript function with different argument? example: function a(a){} function a(a,b){}
18
4116
by: Daniel Gustafsson | last post by:
Hi there, hmm, I've just started with C# and I'm experimenting with method overloading. It seems like it's not possible to override method using return types, only parameters. Is that by design, anyway around that or something? I.e. I'd like to have the same method return either a DataReader or a DataAdapter depending on the lvalue when calling it. And another question. If I have my method returning a value, can it return it by...
9
20592
by: Ryan Taylor | last post by:
Hello. I am trying to overload a method so that I have four possible working copies. The only difference between the four methods is what to search by and in what manner to return the results. So I have the following. public static ArrayList GetRoles(int userID) public static ArrayList GetRoles(string username) public static string GetRoles(int userID) // Error 1 public static string GetRoles(string username) // Error 2
2
3028
by: Iter | last post by:
Hi Guys, In my company, we have java application which is runing in unix platform, and we have web service application build by microsoft .net runing in IIS in microsoft windows 2000 platform. Jave application needs to call method in web services which are overloading function. Now We got error that couldn't find method of web services in java application. I don't know if java application can call microsoft web services methods which are...
1
4974
by: Ratnakar .N | last post by:
HELLO, Please tell me the main difference between method overriding and method overloading Thank you
10
3385
by: Mihai Osian | last post by:
Hi everyone, Given the code below, can anyone tell me: a) Is this normal behaviour ? b) If it is, what is the reason behind it ? I would expect the A::method(int) to be inherited by B. Compiler: gcc 4.1, Linux Thanks,
11
28154
by: placid | last post by:
Hi all, Is it possible to be able to do the following in Python? class Test: def __init__(self): pass def puts(self, str): print str
5
1323
by: Andreas Schmitt | last post by:
I have a problem here. I've read a book about C# already and got the basics of how the language handles polymorphism I think but I ran into a problem here that I simply never even thought of as a problem coming from C++: I wrote some code similar to this: abtract public class Base { }
1
3075
by: Zach | last post by:
Consider the following code: void Test(int i) { System.Console.WriteLine("int function"); } void Test(object o) { System.Console.WriteLine("object function");
10
3496
by: Matthew | last post by:
Am I correct in thinking there is no method/function overloading of any kind in any version of PHP? Thanks, Matthew
0
9734
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10406
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10420
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10139
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7681
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6897
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5704
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3881
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3029
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.