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

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.

⚡ Hurry! Secure Your Free Cyber Security Certification - December 2025
If you wish to look at all C# Programming examples, go to 1000 C# Programs.

advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification 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
I’m Manish - Founder and CTO at Sanfoundry. I’ve been working in tech for over 25 years, with deep focus on Linux kernel, SAN technologies, Advanced C, Full Stack and Scalable website designs.

You can connect with me on LinkedIn, watch my Youtube Masterclasses, or join my Telegram tech discussions.

If you’re in your 20s–40s and exploring new directions in your career, I also offer mentoring. Learn more here.