C#中的委托是否可以重载!下面的该如何写
今天在看泛型委托Action时,做个测试碰到这样一个问题,非泛型委托是否可以重载?--------------------编程问答-------------------- 重载???
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
//方法一:显式声明了一个名为 ShowName 的委托,并将对 Name.DisplayToWindow 实例方法的引用分配给其委托实例。
public delegate void ShowName();
public delegate void ShowName(string name);
public delegate void ShowAge(int age);
public delegate void ShowNameAndAge(string name, int age);
namespace ActionDemo
{
public class Name
{
private string instanceName;
public string InstanceName
{
get { return instanceName; }
set { instanceName = value; }
}
private int instanceAge;
public int InstanceAge
{
get { return instanceAge; }
set { instanceAge = value; }
}
public Name(string name,int age)
{
this.instanceName = name;
this.instanceAge = age;
}
public void DisplayName()
{
Console.WriteLine("Name:{0}",this.instanceName);
}
public void DisplayName(string name)
{
Console.WriteLine("Name:{0}",name);
}
public void DisPlayAge(int age)
{
Console.WriteLine("Age:{0}",age);
}
public void DisplayNameAndAge(string name, int age)
{
Console.WriteLine(string.Format("Name:{0} And Age:{1} ",name,age));
}
}
class Program
{
static void Main(string[] args)
{
Name name = new Name("joetao",21);
//实例化委托
//无参委托
ShowName showName1=new ShowName(name.DisplayName);
showName1();
//委托 DisplayName(string name)该如何写????
ShowName showName2=new ShowName(name.DisplayName);
}
}
}
没整过,不知道.
接点浮云.
--------------------编程问答-------------------- public delegate void ShowName();
public delegate void ShowName(string name);
可以编译通过??? --------------------编程问答-------------------- 可以直接用范型Action<T>
static void Main(string[] args)--------------------编程问答-------------------- 不可能的,泛型也不能重载... --------------------编程问答--------------------
{
Name name = new Name("joetao", 21);
Action showName1 = name.DisplayName;
Action<string> showName2 = name.DisplayName;
showName1();
showName2("hello");
}
我写了后没编译,呵呵,确实连编译都没通过。
但后来试了下,用Action<T>方法是可以的。
--------------------编程问答-------------------- 使用Action<T>和你把第二个委托声明为ShowNameWithParam有什么区别?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
//方法一:显式声明了一个名为 ShowName 的委托,并将对 Name.DisplayToWindow 实例方法的引用分配给其委托实例。
//public delegate void ShowName();
//public delegate void ShowName(string name);
//public delegate void ShowAge(int age);
//public delegate void ShowNameAndAge(string name, int age);
namespace ActionDemo
{
public class Name
{
private string instanceName;
public string InstanceName
{
get { return instanceName; }
set { instanceName = value; }
}
private int instanceAge;
public int InstanceAge
{
get { return instanceAge; }
set { instanceAge = value; }
}
public Name(string name,int age)
{
this.instanceName = name;
this.instanceAge = age;
}
public void DisplayName()
{
Console.WriteLine("Name:{0}",this.instanceName);
}
public void DisplayName(string name)
{
Console.WriteLine("Name:{0}",name);
}
public void DisplayAge(int age)
{
Console.WriteLine("Age:{0}",age);
}
public void DisplayNameAndAge(string name, int age)
{
Console.WriteLine(string.Format("Name:{0} And Age:{1} ",name,age));
}
}
class Program
{
static void Main(string[] args)
{
Name name = new Name("joetao",21);
////实例化委托
////无参委托
//ShowName showName1=new ShowName(name.DisplayName);
//showName1();
////委托 DisplayName(string name)该如何写????
//ShowName showName2=new ShowName(name.DisplayName);
//泛型委托Action
Action actionShowName = name.DisplayName;
actionShowName();
Action<string> actionShowName1 = name.DisplayName;
actionShowName1(name.InstanceName);
Action<int> actionShowAge = name.DisplayAge;
actionShowAge(name.InstanceAge);
Action<string, int> actionShowNameAndAge = name.DisplayNameAndAge;
actionShowNameAndAge(name.InstanceName, name.InstanceAge);
Console.ReadKey();
}
}
}
委托相当于定义了新的类型,它又不是函数。 --------------------编程问答-------------------- 编译过不去
你改变一下思路。。。。一个委托也可以判断几种情况的。 --------------------编程问答--------------------
--------------------编程问答-------------------- public delegate void ShowName();
public delegate void ShowName2(string name);
ShowName2 showName2 = new ShowName2(name.DisplayName);
showName2("oj");
public delegate void ShowName(string name);
这个不可以的,编译不过去。 --------------------编程问答-------------------- 楼主的问题可以通过反射调用方法来解决,这条路是不通的。 --------------------编程问答-------------------- 不可以重载
补充:.NET技术 , C#