This is a C# Program to demonstrate properties of the class.
This C# Program Demonstrates Properties of the Class.
Here it demonstrates how properties are declared and used.
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(); } }
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.
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.
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.
- Practice MCA MCQs
- Apply for C# Internship
- Apply for Computer Science Internship
- Check MCA Books
- Check Computer Science Books