Why we need Properties in C#

前端 未结 8 1182
萌比男神i
萌比男神i 2020-12-01 09:31

Can you tell me what is the exact usage of properties in C# i mean practical explanation

in our project we are using properties like

/// 

        
相关标签:
8条回答
  • 2020-12-01 10:33

    Think about it : You have a room for which you want to regulate who can come in to keep the internal consistency and security of that room as you would not want anyone to come in and mess it up and leave it like nothing happened. So that room would be your instantiated class and properties would be the doors people come use to get into the room. You make proper checks in the setters and getters of your properties to make sure any unexpected things come in and leave.

    More technical answer would be encapsulation and you can check this answer to get more information on that: https://stackoverflow.com/a/1523556/44852

    class Room {
       public string sectionOne;
       public string sectionTwo;
    }
    
    Room r = new Room();
    r.sectionOne = "enter";
    

    People is getting in to sectionOne pretty easily, there wasn't any checking.

    class Room 
    {
       private string sectionOne;
       private string sectionTwo;
    
       public string SectionOne 
       {
          get 
          {
            return sectionOne; 
          }
          set 
          { 
            sectionOne = Check(value); 
          }
       }
    }
    
    Room r = new Room();
    r.SectionOne = "enter";
    

    now you checked the person and know about whether he has something evil with him.

    0 讨论(0)
  • 2020-12-01 10:33

    That's the way to use it, except for the way you're setting it, possibly. Instead of accessing the member variable, you may want to use the property from within the class, so you can use uniform rules regarding each member variable. This is the primary advantage to using properties, is to bring the access and setter logic all into one place. It really depends on your specific needs whether or not you want to set it using the Property or not. Note though, that in the constructor you want to be very careful when calling the Property, as you may or may not be relying on other parts of the class being initialized which would not be done yet if accessed through the constructor. Again, this depends on your specific implementation.

    It is also a little cleaner to use:

    myObject.Property1 = "Test String";
    Console.WriteLine(myObject.Property1);
    

    Than what you see in some other languages:

    myObject.setProperty1("Test String");
    System.out.writeln(myObject.getProperty1());
    

    Here's a case where you can encapsulate some logic:

    public int Order
    {
        get { return m_order; }
        set
        {
            // Put some rules checking here. Maybe a call to make sure that the order isn't duplicated or some other error based on your business rules.
            m_order = value;
        }
    }
    

    Another way they're useful would be like this:

    public int Order { get; private set; }
    

    And now you've got an auto-implemented property with backing member variable that can only be set inside the class but read everywhere else.

    Finally, if you need to control the logic, you can write this:

    public int Order
    {
        get { return m_order; }
        protected set
        {
            // Again, you can do some checking here if you want...
            m_order = value;
            // You can also do other updates if necessary. Perhaps a database update...
        }
    }
    
    0 讨论(0)
提交回复
热议问题