-2

i'm learning about encapsulation and have these examples of ways to control access of the data, both codes work and do the same thing, but through further researching i haven't been able to find any reference to the method access, so i was hoping someone could explain which one is better and reasons why? cause i'm a little confused

(when researching method access on msdn website it basically just gave me the properties example this is why i'm confused)

Method Access

public class LogInToken
{
    private string Name;
    private string Password;

    public string GetName()
    {
        return Name;
    }

    public string GetPassword()
    {
        return Password;
    }

    public void SetName(string NewName)
    {
        Name = NewName;
    }

    public void SetPassword(string newPassword)
    {
        if (newPassword != null)
        {
            Password = newPassword;
        }
        else
        {
            throw new Exception("no password");
        }
    }
}

Property Access

public class LogInToken
{
    private string name;
    private string password;

    public string Name  
    {       
        get     
        {           
            return name;        
        }       
        set     
        {           
            name = value; // C# uses the implicit parameter "value"     
        }   
    }
    public string Password
    {
        get
        {
            return password;
        }
        set
        {
            if (value != null)
            {
                password = value;
            }
            else
            {
                throw new Exception("no password");
            }
        }
    }
}
Kinetic
  • 2,640
  • 17
  • 35
Danny
  • 9
  • 4
  • 1
    Possible duplicate of [Properties vs Methods](http://stackoverflow.com/questions/601621/properties-vs-methods) – Kinetic Dec 08 '16 at 02:45

3 Answers3

2

properties are things that seem like properties, taking a Person class as an example you might have Height, FavoriteColor, Age, Name.

Methods are AskQuestion, Marry, .....

C# allows you to implement properties as code using get and set for the cases where, although this is logically a property, (say FavoriteColor) you actually need to run code to set or get it (maybe u write code that right there and then looks their profile up on FaceBook to see if its says FavoriteColor). In addition you can have a property that is free to read but only setable by some (internal set, public get). Also you could add code on the set that makes sure the values are reasonable colors, (you dont allow 'dog'.'tree' or 'Foof')

Note all of this could be done wiht Person.getFavoriteColor and Person.setFavoriteColor (this is Java's way), but the c# syntactical sugar is nice and clean

pm100
  • 48,078
  • 23
  • 82
  • 145
0

Based on this example which one would you prefer?

public class Student(){
  public string Name {get;set;}
  public string LastName {get;set;}
}

OR

 public class Student(){
  private string Name; 
  private string LastName;

  public string GetStudentName(){
     return this.Name;
  }
  public string GetStudentLastName(){
     return this.LastName;
  }
  public void SetStudentName(string name){
     this.Name = name;
  }
  public void SetStudentLastName(string lname){
     this.LastName = lname
  }
}

Essentially, the first one is a syntactic sugar to write less code for properties initialisation which was released in C# 6.0. C#3.0. Whereas the second one takes me waaaay back when I was still in University (LOL)!

In terms of which implementation is better, it comes down to preference doesn't it? As a lazy developer, I would write the code that will save me even even a few keystrokes :)

jmesolomon
  • 563
  • 5
  • 15
  • Credit to the writer of this article for breaking down what is an Auto-Property Initializer http://geekswithblogs.net/WinAZ/archive/2015/06/30/whatrsquos-new-in-c-6.0-auto-property-initializers.aspx – jmesolomon Dec 08 '16 at 02:50
  • Actually, your first example only show auto-property, which already exist on C# 3.0. – Martheen Dec 08 '16 at 02:57
  • aah! thanks @Martheen for pointing that out. In C# 6.0 you can do more with auto-initializer properties like setting the accessors visibility – jmesolomon Dec 08 '16 at 03:03
0

Properties are characteristics or attributes (not attributes as in .net sense but regular English) of something. So in your case properties will make sense. A LoginToken has a name and a password. Think adjectives here.

Methods are actions such as Login, Connect, Walk etc. Think verbs here.

Properties, by convention, should not do any heavy lifting. For example, in your LoginToken.Password setter if you were connecting to some web service or going to the database to confirm the password is valid or in a valid format (for whatever reason), then you would not do that in the property setter. Properties can do work but lightweight work because by convention this is implied and other developers may get or set the property in a loop many times. However, if you had a method SetPassword(string password) or better yet ValidatePassword(string password) then developers will most likely call the method, cache the return value and not call it within a loop. But then again this is convention.

CodingYoshi
  • 25,467
  • 4
  • 62
  • 64