C# Program to Demonstrate Properties of the Class

This is a C# Program to demonstrate properties of the class.

Problem Description

This C# Program Demonstrates Properties of the Class.

Problem Solution

Here it demonstrates how properties are declared and used.

Program/Source Code

Here is source code of the C# Program to Demonstrate Properties of the Class. The C# program is successfully compiled and executed with Microsoft Visual Studio. The program output is also shown below.

/*
 *  C# Program to Demonstrate Properties of the Class
 */
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();
    }
}
Program Explanation

This C# program is used to demonstrate the properties of the class. In name procedure we are getting the name of the student entered by the user and setting the value to the myname variable and in Age procedure. We are getting the age of the student entered by the user and assigning the value to myAge variable.

advertisement
advertisement

The ToString() function is used to add “BOB” value with the value of ‘name’ variable and ‘99’ value with the value of ‘Age’ variable. This program demonstrates how properties are declared and used.

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

Sanfoundry Global Education & Learning Series – 1000 C# Programs.

Note: Join free Sanfoundry classes at Telegram or Youtube
If you wish to look at all C# Programming examples, go to 1000 C# Programs.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.