代码理解C#中的问题 [英] Code Understanding Problem in C#

查看:57
本文介绍了代码理解C#中的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hello Everyone!



i am从PDF文件中读取C#的代码请告诉我为什么我们设置设置访问者为私人虽然获取方法设置为公开。请告诉我原因,



代码



Hello Everyone!

i am Reading code of C# from the PDF File Please Tell me Why we "Set" Set Accessors As Private While Get Method Are Set A Public.Please tell me Reason,

CODE

class DateClass
    {
        private int Day;
        private int Month;
        private int Year;
        public int year
        {
            get { return Year;}
            private set
            {
                if (Year < 0 || Year <= 1970 || Year >= 2051)
                {
                    throw new ArgumentOutOfRangeException(
                    "Year", Year, "Outof Range From the Current Program");
                }
                else
                {
                    Year = value;
                }
            }
        }
         public DateClass ( int D, int M, int Y )
        {
            Day = D;
            Month = M;
            Year = Y;
        }
        /// <summary>
        /// Set Month And Check The Condition
        /// </summary>
        public int SetMonth
        {
            get
            {
                return Month;
            }
            private set
            {
                if (Month > 0 && Month < 12)
                {
                    Month = value;
                }
                else
                {
                    throw new ArgumentOutOfRangeException(
                        "Month", value, "Month Must Be Between (1-12)");
                }
            }
        }
        public int SetDay
        {
            get
            {
                return Day;
            }
            private set
            {
                int[] DayArray={0,31,28,31,30,31,30,31,31,30,31,30,31};
                if (value > 0 && value <= DayArray[Month])
                {
                    Day = value;
                }
                else if (Month == 2 && Day == 29 && (Year % 400 == 0 || Year % 4 == 0 && year % 100 != 0))
                {
                    Day = value;
                }
                else
                    throw new ArgumentOutOfRangeException(
                        "Day ", Day, "day out of range of current month && Year");
            }
        }

推荐答案

这样,类消费者只能通过构造函数设置日期。这是一个合法的设计选择,使每个 DateClass 实例成为事实上的常量。



[更新]

正如谢格尔正确地指出的那样,我忽视了在课堂上从未使用过的二传手。这使得它们变得毫无用处,而且整体设计也很糟糕。

[/ update]
This way the class consumers can set the date only via the constructor. This is a legitimate design choice making every DateClass instance a de-facto const.

[update]
As Sergey correctly pointed out, I overlooked setters are never used in the class. That makes them useless and the class overall design is bad.
[/update]


这是通常的重要技术,但请注意,课堂上从未提及过这些只读属性。



这可能意味着以下两点之一:1)并非显示该类的所有代码;一些使用这个私有setter的代码应该存在,否则setter根本就没有意义; 2)类的所有代码都是show,这意味着类的代码是一个大错误:如果某个私有的setter从未在类中使用过,它根本就无法使用,这意味着该属性永远不会被设置任何地方。



另见我对解决方案1和解决方案2的评论。



-SA
This is the usual and important technique, but pay attention that those read-only properties are never mentioned in the class.

It may mean one of the two things: 1) not all code of the class is shown; some code which uses this private setter should exist, otherwise the setter would make no sense at all; 2) all code of the class is show, which means that the code of the class is a big mistake: if some private setter is never used in the class, it cannot be used at all, it means that the property can never be set anywhere.

See also my comments to Solution 1 and Solution 2.

—SA


这称为readonly属性。假设您的属性依赖于类中的某些其他属性或逻辑,并且您不希望让对象手动设置该属性,那么此readonly属性将更符合逻辑。



示例:假设您有两个单选按钮。根据您的选择,您要启用或禁用某些控件。您应该在单选按钮的选择值属性中决定/设置控件的可见性/状态。
This is called readonly property. Suppose your property depends on some other property or logic inside your class and you don't want to let your object set that property manually, then this readonly property will be more logical.

Example: Suppose you have two radio button. based on your selection you want to enable or disable some controls. You should decide/set that controls' visibility/state in radio button's selection value property.


这篇关于代码理解C#中的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆