How do I declare diverse constructors in C#
-
In C#, can I declare more than one constructors? If I do create a second constructor in order to pass variables to, how can I do this and, if so, how? Also, if this is allowed, why does Visual Studio show this as a fault? https://tinyurl.com/ydesvncq
-
In C#, can I declare more than one constructors? If I do create a second constructor in order to pass variables to, how can I do this and, if so, how? Also, if this is allowed, why does Visual Studio show this as a fault? https://tinyurl.com/ydesvncq
What's with the shortened URL at the bottom of your message, pointing to a Google User Content page? That makes me suspect your account has be hijacked by spammers. :suss:
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
-
In C#, can I declare more than one constructors? If I do create a second constructor in order to pass variables to, how can I do this and, if so, how? Also, if this is allowed, why does Visual Studio show this as a fault? https://tinyurl.com/ydesvncq
First, nobody in their right mind is going to click that link. Next, yes, you can declare multiple constructor methods. You just have to make sure they differ in the list of parameters they take.
public class Something
public Something()
{
}public Something(string arg) { } public Something(int value) { } public Something(string arg, int value) { }
}
Asking questions is a skill CodeProject Forum Guidelines Google: C# How to debug code Seriously, go read these articles.
Dave Kreskowiak -
In C#, can I declare more than one constructors? If I do create a second constructor in order to pass variables to, how can I do this and, if so, how? Also, if this is allowed, why does Visual Studio show this as a fault? https://tinyurl.com/ydesvncq
Keep in mind that with .NET 4.0, and later, you can use optional parameters that are assigned a default value in the constructor's parameters: [^] I suggest you review the use of named and positional parameters, and the change to their behavior implemented in .NET 7.2: [^] You can have multiple constructors, and chain them as shown in the example below to support a variable number of parameters. This code sample is written to illustrate the several ways you can handle parameters and their default values: it is not intended as an example of code I'd write for "production." For example, what would be the use of an instance of the 'Person class with only a value for 'Age ?
public class Person
{
const string defaultData = "default";
const int defaultAge = -1;public string FirstName { get; } = defaultData; public string LastName { get; } = defaultData; public string MiddleName { get; } = defaultData; public int Age { set; get;} public Person(int age = defaultAge) { Age = age; } public Person(int age, string fname, string lname, string mname = null) : this(age) // chaining example { FirstName = fname; LastName = lname; MiddleName = mname; } public override string ToString() { return $"{FirstName} {MiddleName}, {LastName} Age: {Age}"; }
}
«... thank the gods that they have made you superior to those events which they have not placed within your own control, rendered you accountable for that only which is within you own control For what, then, have they made you responsible? For that which is alone in your own power—a right use of things as they appear.» Discourses of Epictetus Book I:12
-
Keep in mind that with .NET 4.0, and later, you can use optional parameters that are assigned a default value in the constructor's parameters: [^] I suggest you review the use of named and positional parameters, and the change to their behavior implemented in .NET 7.2: [^] You can have multiple constructors, and chain them as shown in the example below to support a variable number of parameters. This code sample is written to illustrate the several ways you can handle parameters and their default values: it is not intended as an example of code I'd write for "production." For example, what would be the use of an instance of the 'Person class with only a value for 'Age ?
public class Person
{
const string defaultData = "default";
const int defaultAge = -1;public string FirstName { get; } = defaultData; public string LastName { get; } = defaultData; public string MiddleName { get; } = defaultData; public int Age { set; get;} public Person(int age = defaultAge) { Age = age; } public Person(int age, string fname, string lname, string mname = null) : this(age) // chaining example { FirstName = fname; LastName = lname; MiddleName = mname; } public override string ToString() { return $"{FirstName} {MiddleName}, {LastName} Age: {Age}"; }
}
«... thank the gods that they have made you superior to those events which they have not placed within your own control, rendered you accountable for that only which is within you own control For what, then, have they made you responsible? For that which is alone in your own power—a right use of things as they appear.» Discourses of Epictetus Book I:12
BillWoodruff wrote:
For example, what would be the use of an instance of the 'Person class with only a value for 'Age ?
Because some of us old farts are useless for anything else?
Software rusts. Simon Stephenson, ca 1994. So does this signature. me, 2012
-
In C#, can I declare more than one constructors? If I do create a second constructor in order to pass variables to, how can I do this and, if so, how? Also, if this is allowed, why does Visual Studio show this as a fault? https://tinyurl.com/ydesvncq
YES, you can create more than one constructor in c# by passing different parameters based on your requirements. check this article constructors in c# it will help you to understand constructors easily.