ADVERTISEMENT
ADVERTISEMENT

C# program to demonstrate the properties in the interface

Here, we are going to demonstrate the properties in the interface in C#.
Submitted by Nidhi, on November 05, 2020

Here, we create an interface with properties then we will implement the properties in the Student class.

Program:

The source code to demonstrate the properties in the interface is given below. The given program is compiled and executed successfully on Microsoft Visual Studio.

//C# program to demonstrate the properties in the interface.

using System;

interface Inf
{
    int ID { get; set; }
    string Name { get; set; }
}

class Student : Inf
{
    string _name;

    public int ID
    {
        get;
        set;
    }

    public string Name
    {
        get { return this._name; }
        set { this._name = value.ToUpper(); }
    }
}

class Program
{
    static void Main()
    {
        Inf inf = new Student();

        inf.ID = 101;
        inf.Name = "Rohit Sharma";

        Console.WriteLine(inf.ID);
        Console.WriteLine(inf.Name);
    }
}

Output:

101
ROHIT SHARMA
Press any key to continue . . .

Explanation:

In the above program, we created an interface Inf that contains properties ID and Name then we implemented interface properties in the Student class.

Now look to the Program class, the Program class contains the Main() method, The Main() method is the entry point for the program. Here we created the object of Student class and initialized the inf reference.

inf.ID = 101;
inf.Name = "Rohit Sharma";

Here, we set the properties ID and Name.

Console.WriteLine(inf.ID);
Console.WriteLine(inf.Name);

In the above code, we get values using Get property and print them on the console screen.

C# Basic Programs »


ADVERTISEMENT



ADVERTISEMENT



Comments and Discussions


ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.