简体   繁体   中英

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); tittles is my string array i tried tittles[1] forexample but end up with an error. 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:

        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 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. 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

Person p = new Person("John", "Doe");

When instantiating with 3 args provide an array of strings like this:

Person p = new Person ("John", "Doe", new string[]{"title1", "title2"})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM