Sunday, 3 December 2017

C# Program to Demonstrate Properties of the Class


Code:

using System;
class Student
{
    private string myName = "N/A";
    private int myAge = 0;
    public string Name
    {
        get
        {
            return myName;
        }
        set
        {
            myName = value;
        }
    }
    public int Age
    {
        get
        {
            return myAge;
        }
        set
        {
            myAge = value;
        }
    }

    public override string ToString()
    {
        return "Name = " + Name + ", Age = " + Age;
    }

    public static void Main()
    {
        Student Student = new Student();
        Console.WriteLine("Student details - {0}", Student);
        Student.Name = "BOB";
        Student.Age = 99;
        Console.WriteLine("Student details - {0}", Student);
        Student.Age += 1;
        Console.WriteLine("Student details - {0}", Student);
        Console.ReadLine();
    }
}


Output:

Student details - Name = N/A, Age = 0
Student details - Name = BOB, Age = 99
Student details - Name = BOB, Age = 100


More C# Programs:




















100+ Best Home Decoration Ideas For Christmas Day 2019 To Make Home Beautiful

Best gifts for Christmas Day | Greeting cards for Christmas Day | Gift your children a new gift on Christmas day This Christmas d...