简体   繁体   English

字段是否需要属性才能使用 get set?

[英]Do fields need properties to use get set?

I'm making classes and I wanted to know the differnce in the application of the getters and setters.我正在上课,我想知道 getter 和 setter 应用的区别。

eg例如

public class Employee
    {
       
        private string forename;
        public string Forename { get { return forename; } }
        private string surname;
        public string Surname { get { return surname; } }
        private int age;
   }

In what I have made I have 'private string forename;'.在我所做的事情中,我有“私人字符串名字;”。 Because it doesn't have {get;set;} is it a variable instead of a field in the class?因为它没有 {get;set;} 它是变量而不是 class 中的字段吗? Also because it is private I have used a property with the same name in order to access forename.也因为它是私有的,我使用了一个同名的属性来访问名字。 I guess my question is what is the point in having the separate Forename/forename if I have to write {get;我想我的问题是,如果我必须写 {get; ,那么拥有单独的 Forename/forename 有什么意义? set;} for the private one as well as the public one. set;} 对于私人的和公共的。 Is there a better way to write the fields?有没有更好的方法来编写字段? Couldn't I just have written:我不能只写:

private string forename{ get { return forename; } }   

eg for my password field I have:例如,对于我的密码字段,我有:

private string password;
        public string Password 

        { set
            {
                bool validPassword = false;
                if (value.Length > 7 & value.Length < 15)
                {
                    if (value.Any(char.IsLower))
                    {
                        if (!value.Contains(" "))
                        {
                            if (value.Any(char.IsUpper))
                            {
                                string specialChar = @"%!@#$%^&*()?/>.<,:;'\|}]{[_~`+=-" + "\"";
                                char[] specialCharArray = specialChar.ToCharArray();
                                foreach (char ch in specialCharArray)
                                {
                                    if (value.Contains(ch))
                                    {
                                        validPassword = true;
                                        Console.WriteLine("Password has been changed");
                                        break;
                                    }
                                }
                            }
                        }
                    }
                }

Couldn't I have just put this all in a {set} on the private password?我不能把这一切都放在私人密码的 {set} 中吗?

password is a private field, and Password is a public property. password是私有字段, Password是公共属性。

You are correct that you could put this logic directly in the set block of the private password field.你是对的,你可以把这个逻辑直接放在私有密码字段的 set 块中。

But by using a property with a set block instead of a field with a set block, you can specify the logic for validating and assigning the password value at a single place, and you can expose the password value to other codes through the property.但是通过使用带有set块的字段,您可以在一个地方指定验证和分配密码值的逻辑,并且可以通过该属性将password值公开给其他代码。 It makes your code easier to maintain and understand.它使您的代码更易于维护和理解。

user can simply assign a password like this employee.Password = "@$@#%@#%@#;";用户可以像这样简单地分配一个密码employee.Password = "@$@#%@#%@#;"; without knowing the internal details.在不知道内部细节的情况下。 you can also change this implementation in future without breaking anything.您也可以在将来更改此实现而不会破坏任何内容。

In short properties are the wrapper around fields.简而言之,属性是字段的包装器。

Forename property only has a getter, which means that it is a read-only property. Forename属性只有一个 getter,这意味着它是一个只读属性。

Here is the official documentation of properties, you can refer this as well. 这里有properties的官方文档,你也可以参考一下。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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