I may have this wrong, but I've seen the way of creating an overloaded method that calls itself in the definition. It's something like:
public void myFunction(int a, int b)
{
//Some code here
}
public void myFunction(int a) : this (a, 10)
{ }
Thi开发者_如何学Pythons is not the correct syntax, I know, but I can't find the correct syntax anywhere for some reason. What is the correct syntax for this?
You are confusing constructor syntax for method syntax. The only way to do that for a method is the obvious:
public void myFunction(int a, int b)
{
//Some code here
}
public void myFunction(int a)
{
myFunction(a, 10) ;
}
although as of C#4, you can use a optional parameters:
public void myFunction(int a, int b = 10)
{
//Some code here
}
What you wrote is close to right for constructors:
public class myClass
{
public myClass(int a, int b)
{
//Some code here
}
public myClass(int a) : this (a, 10)
{ }
}
Just do this:
public void myFunction(int a, int b)
{
//Some code here
}
public void myFunction(int a)
{
myFunction(a, 10)
}
You're confusing overloading with overriding. You can call a base constructor from an derived class like this:
public MyConstructor(int foo)
:base (10)
{}
public class BaseClass {
public virtual void SomeFunction(int a, int b) {}
}
public class Derived : BaseClass {
public override SomeFunction(int a, int b) {
base.SomeFunction(a, b * 10);
}
}
精彩评论