ASP.NET WEB - Interface and Abstract & Programs

  //data types

            int a;

            long b = 2323;

            a = 32;


            string c = "abcsfsd 34223";


            bool d = true;

            bool e = false;



            var f = "sdf";

            var x = 123;

            var y = 23.33;


            //var m;

            //m = 23;


            dynamic k;

            k ="2sdfs";


            Console.WriteLine("Test "+ a);

            Console.WriteLine("Test 2" + b);

            Console.WriteLine("Test 3" + c);

            Console.WriteLine("Test 4" + d);

-----------------------------------

 int a = 10;

        if(a==20)

        {

            Console.WriteLine("Test 1");

        }

        else

        {

            Console.WriteLine("Test 2");

        }

-----------------------------------

 int a = 1;

        if(a==20)

        {

            Console.WriteLine("Test 1");

        }

        else if(a > 10)

        {

            Console.WriteLine("Test 2");

        }

                //10 >= 5 --> True

        else if(a >= 5)

        {

            Console.WriteLine("Test 3");

        }

        else

        {

            

            Console.WriteLine("End");

        }

-----------------------------------

Assignment operator

 int x = 20;

      x += 10;

      Console.WriteLine(x);  

-------


x= x+ 10;


 Console.WriteLine(x != y); 

 if(x!=y)

      {

      Console.WriteLine("X not equal to y");

      }

 int x = 20;

int y = 10;

 if(x!=y && x >10)

 if(x!=y || x >10)

      {

      Console.WriteLine("X not equal to y");

      }

-----------------------------------

 //intialize - will work first time only

        //incre/decre - will execute from 2time

        // i++   = i

        

          //intialize; //Condition //incre/decre

       for(int i=0;   i<5;       i++  )

       {

           Console.WriteLine(i);

       }


-----------------------------------

int i =0;  

      while(i<5)

      {

          Console.WriteLine(i);

          i++;

      }


-----------------------------------

 int x = 25;

            switch (x)

            {

                case 10: Console.WriteLine("Case 1"); break;

                case 20: Console.WriteLine("Case 2"); break;

                default: Console.WriteLine("default"); break;

            }

            Console.ReadLine();


-----------------------------------

 public void Test()

    {

         Console.WriteLine ("Test Function");

    }

    

    public int Test2(int a, int b)

    {

        return a+ b;

    }

    public string Test3()

    {

        return "Test 3 Function";

    }

    

    public static void Main(string[] args)

    {

        HelloWorld obj = new HelloWorld();

        obj.Test();

        int x = obj.Test2(5,20);

        Console.WriteLine(x);

        Console.WriteLine(obj.Test3());

        Console.ReadLine();

    }

-----------------------------------

 public void Test(int s)

    {

        int res = s + 10;

         Console.WriteLine("Test"+ res);

    }

    

    public static void Main(string[] args)

    {

        int a = 100;

        HelloWorld obj = new HelloWorld();

        Console.WriteLine("Inside Main "+ a);

        obj.Test(a);

        Console.WriteLine("After calling Test- Inside Main "+ a);

       

        Console.ReadLine();

    }

-----------------------------------

public void Test(ref int s)

    {

         s = s + 10;

         Console.WriteLine("Test "+ s);

    }

    

    public static void Main(string[] args)

    {

        int a = 100;

        HelloWorld obj = new HelloWorld();

        Console.WriteLine("Inside Main "+ a);

        obj.Test(ref a);

        Console.WriteLine("After calling Test- Inside Main "+ a);

       

        Console.ReadLine();

    }

-----------------------------------



 int[] a = new int[3];

        a[0] = 12;

        a[1] = 23;

        a[2] = 88;

        

        for(int i=0; i<a.Length;  i++)

        {

         Console.WriteLine(a[i]) ;  

        }

using System.Collections.Generic;

List<int> lst = new List<int>();

        lst.Add(33);

        lst.Add(13);

        lst.Add(43);

        lst.Add(63);

        

        foreach(int i  in lst )

        {

            Console.WriteLine(i);

        }

        

------------------------------------

 HashSet<int> lst = new HashSet<int>();

       //List<int> lst = new List<int>();

        lst.Add(33);

        lst.Add(13);

        lst.Add(43);

        lst.Add(63);

        lst.Add(63);

        

        foreach(int i  in lst )

        {

            Console.WriteLine(i);

        }

----------------------------------

SortedSet<int> lst = new SortedSet<int>();

       //List<int> lst = new List<int>();

        lst.Add(33);

        lst.Add(13);

        lst.Add(43);

        lst.Add(10);

        lst.Add(10);

        lst.Add(63);

        

        foreach(int i  in lst )

        {

            Console.WriteLine(i);

        }

-----------------------------------

        //LIFO - Stack

        

        Stack<string> st = new  Stack<string>();

        st.Push("Test 3");

        st.Push("Test 1");

        st.Push("Test 2");

      

        

        foreach(var v in st)

        {

            Console.WriteLine(v);

        }

      


-----------------------------------

 //FIFO - Queue

        

        Queue<string> q = new  Queue<string>();

        q.Enqueue("Test 3");

        q.Enqueue("Test 1");

        q.Enqueue("Test 2");

        q.Enqueue("Test 2");

      

        

        foreach(var v in q)

        {

            Console.WriteLine(v);

        }


-----------------------------------

using System;


public class HelloWorld

{

    //Called first

    public HelloWorld(int a)

    {

         Console.WriteLine ("Test Constructer"+a);

    }

    

    //Called last

    ~HelloWorld()

    {

        Console.WriteLine ("Destructer ");

    }

    

    public static void Main(string[] args)

    {

        HelloWorld ob = new HelloWorld(10);

        Console.WriteLine("Middle Operations");

        Console.WriteLine("Middle Operations 2");

       // ob.HelloWorld();

        //Console.ReadLine();

    }

}

-----------------------------------

//struct    

// value type

//class - reference type

public struct Test

{

  public  int a;

}


public class HelloWorld

{

    public static void Main(string[] args)

    {

        Test obj = new Test();

        obj.a = 20;

        Console.WriteLine(obj.a);

  

    }

}

------------------------------------

//struct    

// value type

//class - reference type

public struct Test

{

  public  int a;

}


public class HelloWorld

{

    public static void Main(string[] args)

    {

        Test obj = new Test();

        obj.a = 20;

        Console.WriteLine(obj.a);

  

    }

}

----------------------------------


using System;

//single inheritance

public class surya

{

    public string movie = "avatar";

}


public class padma : surya

{

    

}


public class HelloWorld

{

    public static void Main(string[] args)

    {

        padma p = new padma();

        

        Console.WriteLine(p.movie);

    }

}

-----------------------------------


using System;


//multi level inheritance

public class surya

{

    public string movie = "avatar";

}


public class padma : surya

{

     public string movie2 = "avatar-2";

}


public class prabhu : padma

{

    public string movie3 = "Thor";

}


public class HelloWorld

{

    public static void Main(string[] args)

    {

        prabhu p = new prabhu();

        Console.WriteLine(p.movie);

        Console.WriteLine(p.movie2);

        Console.WriteLine(p.movie3);

    }

}

-----------------------------------


using System;


//interface - we can't provide definition

//we can add only return type, name , param..


public interface calc1

{

   void add(int a, int b); 

}


public interface calc2

{

   void mul(int a, int b); 

}

public interface calc3

{

   void div(int a, int b); 

}

public interface calc4

{

   void sub(int a, int b); 

}


public class calculation : calc1,calc2,calc3,calc4

{

   public  void add(int a, int b)

   {

       Console.WriteLine(a+b);

   }

   public  void mul(int a, int b)

   {

       Console.WriteLine(a*b);

   }

   public  void div(int a, int b)

   {

       Console.WriteLine(a/b);

   }

   public  void sub(int a, int b)

   {

       Console.WriteLine(a-b);

   }

}


public class HelloWorld

{

    public static void Main(string[] args)

    {

     calculation c = new calculation();

     c.add(20,40);

     c.mul(20,40);

     c.div(20,40);

     c.sub(20,40);

    }

}

-----------------------------------



-----------------------------------

 int[,] a = new int[3,3];

        a[0,0] = 20;

        a[0,1] = 0;

        a[0,2] = 30;

        a[1,0] = 35;

        a[1,1] = 15;

        a[1,2] = 25;

        

        for(int i=0;i<2;i++)

        {

            for(int j=0;j<2;j++)

            {

                Console.WriteLine(a[i,j] + " ");

            }

        }

-----------------------------------

   try

        {

           int a = 10;

           int b =  0;

           int c = a/b;

           Console.WriteLine(c);

        }

        catch(Exception ex)

        {

             Console.WriteLine("Something went wrong!");

        }

       Console.WriteLine("Test");

-----------------------------------

   try

        {

           int a = 10;

           int b =  8;

           int c = a/b;

           Console.WriteLine(c);

        }

        catch(Exception ex)

        {

             Console.WriteLine("Something went wrong!");

        }

        finally

        {

             Console.WriteLine("finally");

        }

       Console.WriteLine("Test");

-----------------------------------


using System;


public class Test

{

    public void add(int a,int b)

    {

        Console.WriteLine(a+b);

    }

    public void add(int a,int b,int c)

    {

        Console.WriteLine(a+b + c);

    }

}


public class HelloWorld

{

    public static void Main(string[] args)

    {

       Test t = new Test();

       t.add(10,20);

       t.add(15,20,30);

    }

}

-----------------------------------

using System;


public class Test

{

    public virtual void printTest()

    {

        Console.WriteLine("Test 1");

    }

}


public class Test2 : Test

{

    public override void printTest()

    {

         Console.WriteLine("Test 2");

    }

}



public class HelloWorld

{

    public static void Main(string[] args)

    {

      Test2 t = new Test2();

      t.printTest();

    }

}

-----------------------------------

public class Test

{

    public static int a = 10;

    public static void printTest()

    {

        Console.WriteLine("Test 1");

    }

}


public class HelloWorld

{

    public static void Main(string[] args)

    {

        Console.WriteLine(Test.a);

        Test.printTest();

      

    }

}


----------------------------------------------------------------

using System;

//delegare 

delegate void procedure();


public class HelloWorld

{

    public static void channel1()

    {

        Console.WriteLine(" channel1  ");

    }

    public static void channel2()

    {

        Console.WriteLine(" channel  2 ");

    }

    public static void channel3()

    {

        Console.WriteLine(" channel 3  ");

    }

    

    public static void Main(string[] args)

    {

        procedure user1 = null;

        user1 += new procedure(channel2);

        user1 += new procedure(channel3);

        user1();

    }

}

----------------------------------------------------------------


----------------------------------------------------------------

----------------------------------------------------------------


----------------------------------------------------------------

----------------------------------------------------------------


Constructer & Destructor

using System;

public class HelloWorld

{

    public HelloWorld()  

        {  

            Console.WriteLine("Constructor Invoked");  

        }  

        ~HelloWorld()  

        {  

            Console.WriteLine("Destructor Invoked");  

        }  

        

    public void test()

    {

        Console.WriteLine("Test");  

    }


   

    public static void Main(string[] args)

    {

      HelloWorld h1 = new HelloWorld();

       HelloWorld h2 = new HelloWorld();

      

        Console.WriteLine ("Hello  World");

        h2.test();

    }

}

 ----------------------------------------------

In C#, classes and structs are blueprints that are used to create instance of a class. Structs are used for lightweight objects such as Color, Rectangle, Point etc.

Unlike class, structs in C# are value type than reference type. It is useful if you have data that is not intended to be modified after creation of struct.


using System;

public struct Rectangle  

{  

    public int width, height;  

  // it will make error , cannot assign values here 

  //public int width = 3, height = 5; 

 }  


public class HelloWorld

{

   

    public static void Main(string[] args)

    {

      Rectangle r = new Rectangle();

      r.width = 3;

        Console.WriteLine(r.width); 

    }

}


---------------------

  1.         int i = 1;  
  2.         object o = i; // boxing  
  3.         int j = (int)o; // unboxing  


---------------------

https://www.programiz.com/csharp-programming/abstract-class

https://www.programiz.com/csharp-programming/interface


Abstract Class

In C#, we cannot create objects of an abstract class. We use the abstract keyword to create an abstract class. For example,


using System;


public abstract class BasePromotion

{

    public abstract void promotion();

    

    public void display()

    {

       Console.WriteLine("Thanks");

    }

}


public class Manager : BasePromotion

{

    public override void promotion()

    {

        Console.WriteLine("Manager promotion");

    }

}


public class Developer : BasePromotion

{

    public override void promotion()

    {

        Console.WriteLine("Developer promotion");

    }

}


public class HelloWorld

{

    public static void Main(string[] args)

    {

        Manager m = new Manager();

        m.promotion();

        m.display();

        

        Developer d = new Developer();

         d.promotion();

        d.display();

        //Cannot create an instance of the abstract type

      // BasePromotion b = new BasePromotion();

       

    }

}


// create an abstract class

abstract class Language {

  // fields and methods

}

...


// try to create an object Language

// throws an error

Language obj = new Language();




An abstract class can have both abstract methods (method without body) and non-abstract methods (method with the body). For example,


abstract class Language {


  // abstract method

  public abstract void display1();


  // non-abstract method

  public void display2() {

    Console.WriteLine("Non abstract method");

  }

}



Inheriting Abstract Class

As we cannot create objects of an abstract class, we must create a derived class from it. So that we can access members of the abstract class using the object of the derived class. For example,


using System;

namespace AbstractClass {


  abstract class Language {


    // non-abstract method

    public void display() {

      Console.WriteLine("Non abstract method");

    }

  }


  // inheriting from abstract class

  class Program : Language {


    static void Main (string [] args) {

      

      // object of Program class

      Program obj = new Program();


      // access method of an abstract class

      obj.display();


      Console.ReadLine();

    }

  }

}




C# Abstract Method

A method that does not have a body is known as an abstract method. We use the abstract keyword to create abstract methods. For example,


public abstract void display();




Here, display() is an abstract method. An abstract method can only be present inside an abstract class.


When a non-abstract class inherits an abstract class, it should provide an implementation of the abstract methods.


Example: Implementation of the abstract method

using System;

namespace AbstractClass {


  abstract class Animal {


    // abstract method

    public abstract void makeSound();

  }


  // inheriting from abstract class

  class Dog : Animal {


    // provide implementation of abstract method

    public override void makeSound() {


      Console.WriteLine("Bark Bark");


    }

  }

  class Program  {

    static void Main (string [] args) {

      // create an object of Dog class

      Dog obj = new Dog();

      obj.makeSound();    


      Console.ReadLine(); 

    }

  }

}


-------------------------------

Delegates


In C#, delegate is a reference to the method. It works like function pointer in C and C++. But it is objected-oriented, secured and type-safe than function pointer.

For static method, delegate encapsulates method only. But for instance method, it encapsulates method and instance both.

The best use of delegate is to use as event.

Internally a delegate declaration defines a class which is the derived class of System.Delegate.

C# Delegate Example

Let's see a simple example of delegate in C# which calls add() and mul() methods.

  1. using System;

  2. delegate void Procedure();

    public class DelegateDemo 

    {

        public static void channel1()

        {

            Console.WriteLine("channel 1");

        }

        public static void channel2()

        {

            Console.WriteLine("channel 2");

        }

        public static void channel3()

        {

            Console.WriteLine("channel 3");

        }

        public static void Main(string[] args)

        {

          Procedure someProcs = null;

            someProcs += new Procedure(channel1);

            someProcs += new Procedure(channel2);  // Example with omitted class name

            someProcs();

        }

    }

    Output:
After c1 delegate, Number is: 120
After c2 delegate, Number is: 360
Next TopicC# Reflection





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback

  • Send your Feedback to feedback@javatpoint.com

Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA




Why do you want to report this ad?

X

// Online C# Editor for free

// Write, Edit and Run your C# code using C# Online Compiler


using System;

delegate void Procedure();

public class DelegateDemo 

{

    public static void channel1()

    {

        Console.WriteLine("channel 1");

    }

    public static void channel2()

    {

        Console.WriteLine("channel 2");

    }

    public static void channel3()

    {

        Console.WriteLine("channel 3");

    }

    public static void Main(string[] args)

    {

      Procedure someProcs = null;

        someProcs += new Procedure(channel1);

        someProcs += new Procedure(channel2);  // Example with omitted class name

        someProcs();

    }

}


------------------------------


C# Abstraction

The abstract classes are used to achieve abstraction in C#.


Abstraction is one of the important concepts of object-oriented programming. It allows us to hide unnecessary details and only show the needed information.


This helps us to manage complexity by hiding details with a simpler, higher-level idea.


A practical example of abstraction can be motorbike brakes. We know what a brake does. When we apply the brake, the motorbike will stop. However, the working of the brake is kept hidden from us.


The major advantage of hiding the working of the brake is that now the manufacturer can implement brakes differently for different motorbikes. However, what brake does will be the same.


Example: C# Abstraction

using System;

namespace AbstractClass {

  abstract class MotorBike {

    

    public abstract void brake();

  }


  class SportsBike : MotorBike {


    // provide implementation of abstract method

    public override void brake() {

      Console.WriteLine("Sports Bike Brake");

    }

   

  }


  class MountainBike : MotorBike {


    // provide implementation of abstract method

    public override void brake() {      

      Console.WriteLine("Mountain Bike Brake");

    }

   

  }

  class Program  {

    static void Main (string [] args) {

      // create an object of SportsBike class

      SportsBike s1 = new SportsBike();  

      s1.brake();


      // create an object of MountainBike class

      MountainBike m1 = new MountainBike();

      m1.brake();


      Console.ReadLine();

    }

  }

}



---------------

C# interface


Multiple Inheritance

https://www.c-sharpcorner.com/blogs/multiple-inheritance-in-c-sharp-using-interfaces1



We use the interface keyword to create an interface. For example,


interface IPolygon {


  // method without body

  void calculateArea();

}

Here,


IPolygon is the name of the interface.

By convention, interface starts with I so that we can identify it just by seeing its name.

We cannot use access modifiers inside an interface.

All members of an interface are public by default.

An interface doesn't allow fields.

Implementing an Interface

We cannot create objects of an interface. To use an interface, other classes must implement it. Same as in C# Inheritance, we use : symbol to implement an interface. For example,


using System;

namespace CsharpInterface {


  interface IPolygon {

    // method without body

    void calculateArea(int l, int b);


  }


  class Rectangle : IPolygon {


    // implementation of methods inside interface

    public void calculateArea(int l, int b) {


      int area = l * b;

      Console.WriteLine("Area of Rectangle: " + area);

    }

  }


  class Program {

    static void Main (string [] args) {


      Rectangle r1 = new Rectangle();

    

      r1.calculateArea(100, 200);


    }

  }

}



Implementing Multiple Interfaces

Unlike inheritance, a class can implement multiple interfaces. For example,


using System;

namespace CsharpInterface {


  interface IPolygon {

    // method without body

    void calculateArea(int a, int b);


  }


  interface IColor {


    void getColor();

  }

   

  // implements two interface

  class Rectangle : IPolygon, IColor {


    // implementation of IPolygon interface

    public void calculateArea(int a, int b) {


      int area = a * b;

      Console.WriteLine("Area of Rectangle: " + area);

    }


    // implementation of IColor interface

    public void getColor() {


      Console.WriteLine("Red Rectangle");

            

    }

  }


  class Program {

    static void Main (string [] args) {


      Rectangle r1 = new Rectangle();

    

      r1.calculateArea(100, 200);

      r1.getColor();

    }

  }

}





Practical Example of Interface

Let's see a more practical example of C# Interface.


using System;

namespace CsharpInterface {


  interface IPolygon {

    // method without body

    void calculateArea();


  }   

  // implements interface

  class Rectangle : IPolygon {


    // implementation of IPolygon interface

    public void calculateArea() {

      

      int l = 30;

      int b = 90;

      int area = l * b;

      Console.WriteLine("Area of Rectangle: " + area);

    }

  }


  class Square : IPolygon {


    // implementation of IPolygon interface

    public void calculateArea() {

      

      int l = 30;

      int area = l * l;

      Console.WriteLine("Area of Square: " + area);

    }

  }


  class Program {

    static void Main (string [] args) {


      Rectangle r1 = new Rectangle();  

      r1.calculateArea();


      Square s1 = new Square();  

      s1.calculateArea();

    }

  }

}



In the above program, we have created an interface named IPolygon. It has an abstract method calculateArea().


We have two classes Square and Rectangle that implement the IPolygon interface.


The rule for calculating the area is different for each polygon. Hence, calculateArea() is included without implementation.


Any class that implements IPolygon must provide an implementation of calculateArea(). Hence, implementation of the method in class Rectangle is independent of the method in class Square.





Advantages of C# interface

Now that we know what interfaces are, let's learn about why interfaces are used in C#.


Similar to abstract classes, interfaces help us to achieve abstraction in C#.


Here, the method calculateArea() inside the interface, does not have a body. Thus, it hides the implementation details of the method.

Interfaces provide specifications that a class (which implements it) must follow.


In our previous example, we have used calculateArea() as a specification inside the interface IPolygon. This is like setting a rule that we should calculate the area of every polygon.


Now any class that implements the IPolygon interface must provide an implementation for the calculateArea() method.

Interfaces are used to achieve multiple inheritance in C#.

Interfaces provide loose coupling(having no or least effect on other parts of code when we change one part of a code).


In our previous example, if we change the implementation of calculateArea() in the Square class it does not affect the Rectangle class.





Comments

Popular Posts