C# Program to Demonstrate Properties of the Interface

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

Problem Description

This C# Program Demonstrates Properties of the Interface.

Problem Solution

Here the syntax for a property type on an interface declaration is different and the interface declarations do not include modifiers such as “public.”

Program/Source Code

Here is source code of the C# Program to Demonstrate Properties of the Interface. 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 Interface
 */
using System;
 
interface IValue
{
    int Count { get; set; } 
    string Name { get; set; } 
}
 
class Image : IValue 
{
    public int Count
    {
        get;
        set;
    }
 
    string _name;
 
    public string Name 
    {
        get { return this._name; }
        set { this._name = value; }
    }
}
 
class Article : IValue 
{
    public int Count 
    {
        get;
        set;
    }
 
    string _name;
 
    public string Name 
    {
        get { return this._name; }
        set { this._name = value.ToUpper(); }
    }
}
 
class Program
{
    static void Main()
    {
        IValue value1 = new Image();
        IValue value2 = new Article();
 
        value1.Count++; 
        value2.Count++; 
 
        value1.Name = "Tom"; 
        value2.Name = "Jerry"; 
 
        Console.WriteLine(value1.Name); 
        Console.WriteLine(value2.Name);
        Console.ReadLine();
 
    }
}
Program Explanation

This C# program is used to demonstrate properties of the interface. The syntax for a property type on an interface declaration is different and the interface declarations do not include modifiers such as “public”. The interface IValue has a read-write property.Name, and a read-only property.count.

advertisement
advertisement

The class Image implements the IValue interface and uses these two properties. The program read the name of a new image and prints the name. The class Image and Article implement the IValue and have the Name property, the explicit interface member implementation will be necessary. That is, the following property declaration read-write instance property.

Runtime Test Cases
 
Tom
JERRY

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.