Q:

C# program to demonstrate the properties in the interface

0

C# program to demonstrate the properties in the interface

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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.

 

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C# Basic Programs | Class, Object, Methods

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
C# program to demonstrate the IList interface... >>
<< C# program to check a specified type is a value ty...