How can I pass thru from one instantiator to another? Suppose we had this class. How do I pass from foo(string, string) to foo(Uri)?
public foo
{
string path { get; private set; }
string query { get; private set; }
public foo (Uri someUrl)
{
// ... do stuff here
}
public foo (string path, string query)
{
Uri someUrl = new Uri(String.Concat(path, query);
// ... do stuff here to pass thru to foo(someUrl)
}
}
This method has four parameters: the loan amount, the interest rate, the future value and the number of periods. The first three are double-precision floating point numbers, and the fourth is an integer.
Answer: Constructor chaining is the process of calling one constructor from another constructor with respect to current object. Constructor chaining can be done in two ways: Within same class: It can be done using this() keyword for constructors in same class.
Constructor chaining is the process of calling a sequence of constructors. We can do it in two ways: by using this() keyword for chaining constructors in the same class. by using super() keyword for chaining constructors from the parent class.
Parameterized Constructor in C# A constructor with at least one parameter is called a parameterized constructor. The advantage of a parameterized constructor is that you can initialize each instance of the class with a different value.
For simple constructor chaining, you can use the special keywords this
or base
to refer to the current or parent class's other constructors. You are allowed to use any parameters for the executing constructor as parameters to the chained constructor, and you can combine them using any legal single expression. This is basically the same rules that apply to any other inline expression supplied to a function call, except that you should avoid using any members of the class (since it's not been constructed yet) and restrict yourself to constants and pass-through parameters:
public foo (Uri someUrl)
{
this.url = someUrl;
}
public foo (string path, string query)
: this(new Uri(String.Concat(path, query)))
{
// this.url is now set and available.
}
This will work as long as the processing you need to do can be done in a single valued expression. If you needed, for example, to do something else with the Uri
before it's sent to the other constructor, or if you needed some complex if/then logic, you wouldn't be able to get away with this technique. The alternative is to refactor your code into an initialization method:
public foo (Uri someUrl)
{
this.init(someUrl);
}
public foo (string path, string query)
{
var url = String.Concat(path, query);
url = url.Replace("http://", "https://");
this.init(url);
}
private void init (Uri someUrl)
{
this.url = someUrl;
}
You can also do something like:
class Frob
{
public Frob (Uri uri)
{
}
public Frob(string path, string query)
: this(TidyUpUri(path, query))
{
}
private static Uri TidyUpUri(string path, string query)
{
var uri = new Uri(string.Concat(path, query));
// etc.
return uri;
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With