Support us .Net Basics C# SQL ASP.NET Aarvi MVC Slides C# Programs Subscribe Download

Part 27 – Properties in C#




1 comment:

  1. Code:
    namespace DotNetBasics
    {

    class Student
    {
    private int _id;
    private string _name;
    private int _passMark = 35;

    public int Id
    {
    set
    {
    if (value < 0)
    {
    throw new Exception("Student ID cannot be negative");
    }
    this._id = value;
    }
    get
    {
    return this._id;
    }
    }
    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 PassMark
    {
    get
    {
    return this._passMark;
    }
    }

    }
    class Program
    {
    static void Main()
    {
    Student c1 = new Student();
    c1.Id = 101;
    c1.Name = "Mark";

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

    }
    }
    }

    ReplyDelete

It would be great if you can help share these free resources