简体   繁体   English

如何将我的数组属性包含到类构造函数中并能够通过对象初始化来访问它?

[英]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.

相关问题 如何使用默认构造函数创建我的类的数组? - How can i create array of my class with default constructor? 如何为自定义地图类实现内联初始化的构造函数? - How can I implement a constructor with inline initialization for a custom map class? 我无法访问班级财产 - I can't access my class property 如何将这个xml数组序列化为类中的属性? - How can I serialize this xml array to a property in my class? 我无法通过Expression.Property访问对象的属性,除非所提供的表达式的类型是强类型的 - I can't access property on my object via Expression.Property unless the type of the fed expression is strongly-typed 我如何在课外访问属性 - How can I access the property outside of the class 我如何访问“父”class 的属性? - How can i access a property of a "parent" class? 如何在不使用方法或构造函数的情况下直接访问派生 class 中的基本 class 属性? - How can I directly access my base class property in the derived class without using methods or constructors? 当我的类中的属性具有默认构造函数来提供值 automapper 时,如何进行映射? - How can I map when I've property in my class which have default constructor to provide values automapper? 我可以在对象的 New() 构造函数中声明一个委托吗? 还是带初始化参数? - Can I declare a delegate in an Object's New() constructor? Or with initialization parameters?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM