【转载】#274 - Can't Overload if Methods Differ Only by ref and out Modifiers

You can overload a method in a class, i.e. define two methods with the same name, if the methods have different lists of parameters. The parameter lists must differ in the number of parameters or in their types.

For example, we can overload the Dog.Bark method if the parameters differ only in type:

1 public void Bark(int numBarks)
2 {
3   // implementation here
4 }
5 
6 public void Bark(short numBarks)
7 {
8   // implementation here
9 }

We cannot overload a method if the parameters differ only in a ref vs out modifiers:

1 public void FindDog(ref Dog aDog)
2 {
3   // implementation here
4 }
5 
6 public void FindDog(out Dog aDog) 7 { 8   // implementation here 9 }

 原文地址:#274 - Can't Overload if Methods Differ Only by ref and out Modifiers

posted on 2014-03-10 16:13  yuthreestone  阅读(243)  评论(0编辑  收藏  举报

导航