Getter and Setter in C# and C# Properties

Using Getter and Setter:

using System;

public class Student
{
    private int _id;
    private string _name;
    private int _passmark = 35;

    public int GetPassMark()
    {
        return this._passmark;
    }


    public void SetName (string Name)
    {
        if (string.IsNullOrEmpty(Name))
        {
            throw new Exception("Name cannot be null or empty");
        }
        this._name = Name;
    }
    public string GetName()
    {
        return string.IsNullOrEmpty(this._name) ? "No Name" : this._name;

        //if (string.IsNullOrEmpty(this._name))
        //{
        //    return "No Name";
        //}
        //else
        //{
        //    return this._name;
        //}
    }



    public void SetId(int Id)
    {
        if (Id <= 0)
        {
            throw new Exception("Student ID cannot be a negative");
        }
        this._id = Id;
    }
    public int GetId()
    {
        return this._id;
    }
}

public class Program
{
    public static void Main()
    {
        Student C1 = new Student();
        C1.SetId(403130);
        C1.SetName("Charlie");

        Console.WriteLine("Student ID = {0}", C1.GetId());
        Console.WriteLine("Student Name = {0}", C1.GetName());
        Console.WriteLine("Student PassMark = {0}", C1.GetPassMark());
    }
}

ID number cannot be negative. 

Name cannot be empty

Set passmark to read only...

 

using "private" and getter and setter to impose these restrictions mentioned above.

 

 

Using C# properties:

using System;

public class Student
{
    private int _id;
    private string _name;
    private int _passmark = 35;

    public int PassMark
    {
        get
        {
            return this._passmark;  //read only
        }
    }


    public string Name
    {
        set
        {
            if (string.IsNullOrEmpty(value))
            {
                throw new Exception("Name cannot be null or empty");
            }
            this._name = value;

        }
        get
        {
            return string.IsNullOrEmpty(this._name) ? "No Name" : this._name;
        }
    }

    public int Id
    {
        set
        {
            if (value <= 0)
            {
                throw new Exception("Student ID cannot be a negative");
            }
            this._id = value;
        }

        get
        {
            return this._id;
        }
    }    
}

public class Program
{
    public static void Main()
    {
        Student C1 = new Student();
        C1.Id = 403130;
        C1.Name = "Charlie";

        Console.WriteLine("Student ID = {0}", C1.Id);
        Console.WriteLine("Student Name = {0}", C1.Name);
        Console.WriteLine("Student PassMark = {0}", C1.PassMark);
    }
}

this is exactly the same code compared to the one above, but this time we are using C# properties. 

 

Bonus

Auto Implemented Properties:

If there is no additional logic in the property accessors, then we can make use of Auto Implemented Properties.

 

Normally, we have to write

using System;

public class Student
{
    private string _city;

    public string City
    {
        get
        {
            return this._city;
        }
        set
        {
            this._city = value;
        }
    }
}

Using Auto Implemented Properties, we only need to write

using System;

public class Student
{
    public string City {get;set;}
}

When you use auto implemented properties, the compiler creates a private, anonymous field that can only be accessed through the property's get and set methods.

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值