[英]how can i include my array property into class constructor and be able to access it via object initialization?
i'm very new to programming and in the progress of learning. 我对编程和学习进程都很陌生。 here is my code
这是我的代码
namespace ConsoleApp9
{
class Program
{
static void Main(string[] args)
{
Person p = new Person("john", "doe", tittles:null);
Person td = new Person("tom","jones");
Person w = new Person();
Console.WriteLine(td);
Console.WriteLine(p.SayHello("vahid"));
var str = p.SayHello("nick");
Console.WriteLine(str);
p.DoSome();
var m = w.Tittles[0];
Console.WriteLine(m);
}
}
public class Person
{
public string FirstName { get; private set; }
public string LastName { get; private set; }
private string[] tittles = new string[6] {
"Mr","Mrs", "Miss","Sir", "Doctor","Sister"
};
public string[] Tittles
{
get { return tittles; }
set { tittles = value; }
}
public Person()
{
}
public Person(string firstName, string lastName)
{
FirstName = firstName;
LastName = lastName;
}
public Person(string firstName, string lastName, string[] tittles )
{
FirstName = firstName;
LastName = lastName;
Tittles = tittles;
}
public override string ToString()
{
return "Welcome to C# " + Tittles[0] + " " + FirstName + " " + LastName;
}
public string SayHello(string name)
{
return "hello " + name;
}
public void DoSome()
{
Console.WriteLine(FirstName + " "+ LastName + " this is a void method.");
}
}
}
my question is how to give other value than null in Person p = new Person("john", "doe", tittles:null); 我的问题是如何在Person p = new Person(“john”,“doe”,tittles:null)中给出除null之外的其他值; tittles is my string array i tried tittles[1] forexample but end up with an error.
tittles是我的字符串数组我尝试了tit例[1]例如但最终出现错误。 is there a way this could be done?
有没有办法做到这一点? thanks
谢谢
Here's one way to do it: 这是一种方法:
Person p = new Person("john", "doe", new string[] { "one", "two" });
Or, you could use the params
keyword to define a constructor that takes any number of strings: 或者,您可以使用
params
关键字来定义带有任意数量字符串的构造函数:
public Person(string firstName, string lastName, params string[] tittles)
{
FirstName = firstName;
LastName = lastName;
Tittles = tittles;
}
Then you can create Person objects with any number of titles without having to create a temporary string array: 然后,您可以创建具有任意数量标题的Person对象,而无需创建临时字符串数组:
Person p = new Person("john", "doe", "one", "two");
Person j = new Person("jane", "doe", "one", "two", "three");
Person td = new Person("tom", "jones", "mr");
If you are instantiating your class with the constructor that take 3 arguments you will override the private field array titles
in your class. 如果使用带有3个参数的构造函数实例化类,则将覆盖类中的私有字段数组
titles
。 I am assuming that you want to keep the values in there and therefore you should instantiate the class with the constructor that takes 2 arguments as that does not touch the Titles property 我假设您希望保留值,因此您应该使用带有2个参数的构造函数实例化该类,因为它不会触及Titles属性
Person p = new Person("John", "Doe");
When instantiating with 3 args provide an array of strings like this: 使用3个args实例化时,会提供如下字符串数组:
Person p = new Person ("John", "Doe", new string[]{"title1", "title2"})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.