Codechef4u is a community for computer professionals,by computer professionals,just like you; who loves sharing and helping each others,Join them
Share your post

Interface Properties

Interface Properties:

You can declare properties on interface. The accessor of an interface property does not have a body. Thus, the purpose of the accessors is to indicate whether the property is read-write, read-only, or write-only.

Syntax:

[attributes] [new] type identifier {interface-accessors}

 

attributes (Optional):Same as properties on classes.
type:Same as properties on classes.
identifier:The property name.
interface-accessors:The property accessors..

Points to remember:

1.  The accessor of an interface property does not have a body.
2.  Use of accessors is to indicate whether the property is read-write, read-only, or write-only.

Example:

implements the Name property on the ICollage interface, while the following declaration:
 public interface ICollage
    {
        // Property declaration:
        string Name
        {
            get;
            set;
        }
 
        int Counter
         {
          get;
         }
    }

 

implements the Name property on the TraningInstitute interface, while the following declaration:

    

public interface TraningInstitute
    {
        // Property declaration:
        string Name
        {
            get;
            set;
        }
    }

 

string ICollage.Name
{
    get { return "Employee Name"; }
    set { }
}
 

 

string TraningInstitute.Name
{
    get { return "techare name Name"; }
    set { }
}

 

 

ICollage interface implemented with Teacher class:

 

    public class Teacher :ICollage
    {
        public static int numberOfTeachers;
 
        private string name;
        public string Name  // read-write instance property
        {
            get
            {
                return name;
            }
            set
            {
                name = value;
            }
        }
 
        private int counter;
        public int Counter  // read-only instance property
        {
            get
            {
                return counter;
            }
        }
 
        public Teacher()  // constructor
        {
            counter = ++counter + numberOfTeachers;
        }
    }

  


class Program
    {
 
      static void Main(string[] args)
        {
            Console.Write("Enter number of Teacher: ");
            string s = Console.ReadLine();
            Teacher.numberOfTeachers = int.Parse(s);
            Employee e1 = new Employee();
            Console.Write("Enter the name of the new Teacher: ");
            e1.Name = Console.ReadLine();
            Console.WriteLine("The Tecaher information:");
            Console.WriteLine("Tecaher id: {0}", e1.Counter);
            Console.WriteLine("Tecaher name: {0}", e1.Name);
 
        }
     }

Invalid entry,please enter valid data.

Loading