google-site-verification=ECX1M6_Vb39ty4VtQDXQce6wOjmPTxSettd3hTIZb9Q

C# Interface Tutorial for Beginners

random

C# Interface Tutorial for Beginners

Interfaces in C# (For Beginners)


An interface contains only the signatures of methods, properties, events or indexers/delegates. A class or struct that implements the interface must implement the members of the interface that are specified in the interface definition.

Example Imagine that you have a pizza store. You know that all pizzas need to be ordered, prepared, baked and boxed. Well, you can define this common behavior into an interface.


C# Interface, Tutorial, for Beginners, what is interface?, how to, implement interface, define interface, with c#, interview question, advantages of interfaces, all about interface

C# - Learning to use Interfaces effectively

Create interface by right click on application - > Add new item -> Interface1.cs (named IPizza)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace consoleapp
{
   public interface IPizza
    {
        void order();      
        void prepare();
        void baked();
        void box();
    }
}

I have different kinds of pizzas implement in that interface. When you implement an interface, you're forcing that class to have the same methods and parameters as the interface, for example:

Creating a Class to inherit the interface (IPizza)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace consoleapp
{
    class DoubleCheesePizza:IPizza
    {
        public void order()
        {
            Console.WriteLine("order");

        }
        public void prepare()
        {
            Console.WriteLine("prepare");
        }
        public void baked()
        {
            Console.WriteLine("baked");
        }

        public void box()
        {
            Console.WriteLine("box");
        }
    }
   
}

Call it from main() program

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace consoleapp
{
    class Program
    {
        static void Main(string[] args)
        {
            DoubleCheesePizza obj = new DoubleCheesePizza();
            obj.order();
            obj.prepare();
            obj.baked();
            obj.box();
            Console.ReadKey();
        }
    } 

}

Advantage of using Interface
  • Supports multiple inheritance(Single Class can implement multiple interfaces).
  • Contains only incomplete method.
  • Can not Contains data members/variables/fields.
  • By Default interface members is public (We Can not set any access modifier into interface members).
  • Interface can not contain constructors.

Download the source code click here
Please leave your comments, suggestions and queries about this post in the comment sections in order for me to improve my writing skills and to showcase more useful posts. Thanks for reading .. :)
C# Interface Tutorial for Beginners Reviewed by Ravi Kumar on 2:59 PM Rating: 5

No comments:

All Rights Reserved by Etechpulse © 2012 - 2017

Contact Form

Name

Email *

Message *

Powered by Blogger.