How to call constructor without new?

后端 未结 4 804
夕颜
夕颜 2021-01-20 17:50

i know that string is like a class, and when creating a new string the string itself doesnt owe the value but only the value\'s pointer. but when creating a string there is

4条回答
  •  长发绾君心
    2021-01-20 18:39

    You can use cast operator to implicitly:

        sealed class Student
        {
            public string Name
            {
                get;
                private set;
            }
    
            Student()
            {
            }
    
            public static implicit operator Student(string name)
            {
                return new Student
                {
                    Name = name
                };
            }
        }
    

    Then you can do Student student = "Sabrina";.

提交回复
热议问题