Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/276.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 当我在属性中创建条件时,我是否必须再次在自定义构造函数中创建相同的条件?_C#_If Statement_Struct_Properties_Encapsulation - Fatal编程技术网

C# 当我在属性中创建条件时,我是否必须再次在自定义构造函数中创建相同的条件?

C# 当我在属性中创建条件时,我是否必须再次在自定义构造函数中创建相同的条件?,c#,if-statement,struct,properties,encapsulation,C#,If Statement,Struct,Properties,Encapsulation,我的全部问题是: 当我在属性中创建条件时,我是否必须再次在自定义构造函数中创建相同的条件,或者我可以以某种方式使用属性自定义构造函数? 如果我有这样的代码: class Program { struct Student { private int _id; private string _name; private int _age; public int ID // Property {

我的全部问题是:
当我在属性中创建条件时,我是否必须再次在自定义构造函数中创建相同的条件,或者我可以以某种方式使用属性自定义构造函数?

如果我有这样的代码:

class Program
{
    struct Student
    {
        private int _id;
        private string _name;
        private int _age;

        public int ID // Property
        {
            get
            {
                return _id;
            }
            set
            {
                if (value <= 0)
                {
                    Console.WriteLine("You cannot assign id less then 1");
                    Console.ReadLine();
                    Environment.Exit(0);
                }
                else
                {
                    _id = value;
                }
            }
        }

        public string NAME // Property
        {
            get
            {
                return _name;
            }
            set
            {
                if (String.IsNullOrEmpty(value))
                {
                    _name = "No Name";
                }
                else
                {
                    _name = value;
                }
            }
        }

        public int AGE // Property
        {
            get
            {
                return _age;
            }
            set
            {
                if(value <= 0)
                {
                    Console.WriteLine("Your age cannot be less then 1 year.");
                    Console.ReadLine();
                    Environment.Exit(0);
                }
                else
                {
                    _age = value;
                }
            }
        }

        public Student(int initID, string initName, int initAge) // Defining custom constructor
        {
            if (initID <= 0)
            {
                Console.WriteLine("You cannot assign id less then 1");
                Console.ReadLine();
                Environment.Exit(0);
            }
            if (String.IsNullOrEmpty(initName))
            {
                _name = "No Name";
            }

            if (initAge <= 0)
            {
                Console.WriteLine("Your age cannot be less then 1 year.");
                Console.ReadLine();
                Environment.Exit(0);
            }
            _id = initID;
            _name = initName;
            _age = initAge;
        }

        public void Status() // struct member - method
        {
            Console.WriteLine("ID: {0}", _id);
            Console.WriteLine($"Name: {_name}");
            Console.WriteLine($"Age: {_age}\n");
        }


    }

    static void Main(string[] args)
    {
        Student s1 = new Student(1, "James", 10);
        s1.Status(); 
        Console.ReadLine();
    }
}
类程序
{
体类型
{
私人内部id;
私有字符串\u名称;
私人互联网;
公共int ID//属性
{
得到
{
返回_id;
}
设置
{

if(value首先:使用
class
而不是
struct
,只要你没有很好的理由使用
struct

这是一个使用方法而不是公共属性的好例子

举个简单的例子,只包括年龄,来说明我在说什么

public class Student
{
    private int _age = 1;

    public Student(int initAge)
    {
        SetAge(initAge);
    }

    public void SetAge(int age)
    {
        if (age <= 0)
        {
            Console.WriteLine("Your age cannot be less then 1 year.");
            Console.ReadLine();
            Environment.Exit(0);
        }
        else
        {
            _age = age;
        }
    }

    public int GetAge()
    {
        return _age;
    }
}

并返回true或false。如果可以设置值,则返回true,否则返回false。

首先:使用
class
而不是
struct
,只要您没有很好的理由使用
struct

这是一个使用方法而不是公共属性的好例子

举个简单的例子,只包括年龄,来说明我在说什么

public class Student
{
    private int _age = 1;

    public Student(int initAge)
    {
        SetAge(initAge);
    }

    public void SetAge(int age)
    {
        if (age <= 0)
        {
            Console.WriteLine("Your age cannot be less then 1 year.");
            Console.ReadLine();
            Environment.Exit(0);
        }
        else
        {
            _age = age;
        }
    }

    public int GetAge()
    {
        return _age;
    }
}

并返回true或false。如果可以设置值,则返回true,否则返回false。

您不需要重新创建完整的逻辑。您只需调用构造函数中的属性设置器并依赖其验证逻辑即可:

public Student(int initID, string initName, int initAge) // Defining custom constructor
{
    this.ID = unitID;
    this.NAME = initName;
    this.AGE = initAge;
}
现在,当任何参数出错时,属性设置程序都会抱怨,无需在构造函数中再次检查


顺便说一句,我不会因为一个whrong参数而退出应用程序。不过,你可以抛出一个
ArgumentException
,然后在调用代码中捕获它,或者使用@Mighty Badaboom的方法,使用
TryParse
-模式。

你不需要重新发明你的完整逻辑。你可以在你的应用程序中调用属性设置器构造函数并依赖其验证逻辑:

public Student(int initID, string initName, int initAge) // Defining custom constructor
{
    this.ID = unitID;
    this.NAME = initName;
    this.AGE = initAge;
}
现在,当任何参数出错时,属性设置程序都会抱怨,无需在构造函数中再次检查


顺便说一句,我不会因为一个whrong参数而退出应用程序。但是,你可以抛出一个
ArgumentException
,然后在调用代码中捕捉到它,或者使用@Mighty Badaboom的方法,使用
TryParse
-模式。

除非你在构造函数中的条件不同,否则没有理由复制setter co德

public Student(int initID, string initName, int initAge) // Defining custom constructor
{
        ID = initID;
        NAME = initName;
        AGE = initAge;
}
或者,您可以删除构造函数,并对公共属性使用标准语法

var student = new Student
{
    ID = id,
    NAME = name,
    AGE = age
};

除非构造函数中的条件不同,否则没有理由复制setter代码

public Student(int initID, string initName, int initAge) // Defining custom constructor
{
        ID = initID;
        NAME = initName;
        AGE = initAge;
}
或者,您可以删除构造函数,并对公共属性使用标准语法

var student = new Student
{
    ID = id,
    NAME = name,
    AGE = age
};

您的第一个问题是使用
struct
而不是
class
。您应该有很好的理由这样做。您很少需要使用
struct
。您的第一个问题是使用
struct
而不是
class
。您应该有很好的理由这样做。您很少需要使用
struct