Tuesday, June 28, 2011

Interfaces (C# Programming)

Interfaces consist of methods, properties, events, indexers, or any combination of those four member types. An interface cannot contain constants, fields, operators, instance constructors, destructors, or types. It cannot contain static members. Interfaces members are automatically public, and they cannot include any access modifiers.
When a class or struct implements an interface, the class or struct provides an implementation for all of the members defined by the interface. The interface itself provides no functionality that a class or struct can inherit in the way that base class functionality can be inherited. However, if a base class implements an interface, the derived class inherits that implementation. The derived class is said to implement the interface implicitly.
Classes and structs implement interfaces in a manner similar to how classes inherit a base class or struct, with two exceptions:
  • A class or struct can implement more than one interface.
  • When a class or struct implements an interface, it receives only the method names and signatures, because the interface itself contains no implementations

    public class Car : IEquatable<Car>
    {
        public string Make {get; set;}
        public string Model { get; set; }
        public string Year { get; set; }
    
        // Implementation of IEquatable<T> interface
        public bool Equals(Car car)
        {
            if (this.Make == car.Make &&
                this.Model == car.Model &&
                this.Year == car.Year)
            {
                return true;
            }
            else
                return false;
        }
    } 
     
     
     

    An interface has the following properties:
  • An interface is like an abstract base class: any non-abstract type that implements the interface must implement all its members.
  • An interface cannot be instantiated directly.
  • Interfaces can contain events, indexers, methods, and properties.
  • Interfaces contain no implementation of methods.
  • Classes and structs can implement more than one interface.
  • An interface itself can inherit from multiple interfaces.

No comments:

Post a Comment

Thank you for Commenting Will reply soon ......

Featured Posts

#Linux Commands Unveiled: #date, #uname, #hostname, #hostid, #arch, #nproc

 #Linux Commands Unveiled: #date, #uname, #hostname, #hostid, #arch, #nproc Linux is an open-source operating system that is loved by millio...