Menu Close

Blog

Slide 1

Microsoft Business Applications Blogposts, YouTube Videos and Podcasts

Helping Businesses with Technology

Slide 2

Microsoft Business Applications Blogposts, YouTube Videos and Podcasts

Helping Businesses with Technology

Slide 3

Microsoft Business Applications Blogposts, YouTube Videos and Podcasts

Helping Businesses with Technology

previous arrow
next arrow

MULITPLE CLASS INHERITANCE USING MULTIPLE INTERFACES

MULITPLE CLASS INHERITANCE USING MULTIPLE INTERFACES


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ConsoleApplication4;

namespace ConsoleApplication4
{
    interface IA
    {
        void AMethod();
    }
    class A : IA
    {
        public void AMethod()
        {
            Console.WriteLine(“A”);
        }
    }
    interface IB
    {
        void BMethod();
    }
    class B : IB
    {
        public void BMethod()
        {
            Console.WriteLine(“B”);
        }
    }
    class AB : IA,IB
    {
        A a = new A();
        B b = new B();
        public void AMethod()
        {
            a.AMethod();
        }
        public void BMethod()
        {
            b.BMethod();
        }
    }
    
    class Program
    {
        public static void Main()
        {
            AB ab = new AB();
            ab.AMethod();
            ab.BMethod();
            Console.ReadLine();
        }
    }
}

Share this:

MULTIPLE CLASS INHERITANCE PROBLEM IN C#

MULTIPLE CLASS INHERITANCE PROBLEM IN C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ConsoleApplication4;

namespace ConsoleApplication4
{

    /// <summary>
    /// 
    /// PROBELMS WITH MULTIPLE CLASS INHERITANCE
    /// </summary>
    class A
    {
        public virtual void print()
        {
            Console.WriteLine(“class A implemented”);
        }

    }
    class B : A
    {

        public override void print()
        {
            Console.WriteLine(“Class B overriding print() method”);
        }

    }

    class C : A
    {
        public override void print()
        {
            Console.WriteLine(“Class C overriding print() method”);
        }
    }
    class D : B, C
    {


    }
    class Program
    {
        public static void Main()
        {
            D d = new D();
            d.print(); // AMBIGUITY HERE, WHICH METHOD WILL IT EXECUTE, SO A DIAMOND PROBLEM.
        }
    }
}
Share this:

ABSTRACT CLASSES IN C#

ABSTRACT CLASSES IN C#

Abstract classes

The abstract keyword is used to create abstract classes

An abstract class is incomplete and hence cannot be instantiated

An abstract class can only be used as base class

An abstract class can not be sealed

An abstract class may contain abstract members(methods, properties,indexers,and events) but not mandatory.

A non- abstract class derived from an abstract class must provide implementations for all inherited abstract members.

If a class inherits an abstract class, there are 2 options available for that class

Option 1: Provide Implementation for all the abstract members inherited from the base abstract class.

Option 2: If the class does not wish to provide Implementation for all the abstract members inherited from the abstract class, then the class has to be marked as abstract.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ConsoleApplication4;

namespace ConsoleApplication4
{
    public abstract class Customer
    {
        public abstract void print();

    }

    public class program : Customer
    {
        public override void print()
        {
            Console.WriteLine(“Hello abstract method”);
        }
        public static void Main()
        {
            program p = new program();
            p.print();
            Console.ReadLine();
        }
    }
}

Share this:

INTERFACES IN C#

INTERFACE IN C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ConsoleApplication4;

namespace ConsoleApplication4
{
    interface  ICustomer1
    {
        void print1();
        
    }

    interface ICustomer2 : ICustomer1
    {
        void print2();

    }
  public  class Customer : ICustomer2
    {
        public void print1()
        {
            Console.WriteLine(“INTERFACE METHOD IMPELMENTED”);
            Console.ReadLine();
        }

        public void print2()
        {
            Console.WriteLine(“INTERFACE I2METHOD IS IMPLEMENTED TOO”);
            Console.ReadLine();
        }
    }
        

}
        public class Program 
        {
            static void Main()
            {
                Customer C1 = new Customer();
                C1.print1();
                C1.print2();
                Console.ReadLine();
            }       
}


===================================================================

EXPLICIT INTERFACE IMPLEMENTATION

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ConsoleApplication4;

namespace ConsoleApplication4
{
    interface I1
    {
        void INTERFACEMETHOD();

    }
    interface I2 : I1
    {
        void INTERFACEMETHOD();

    }
    public class Program : I1,I2
    {
//  PUBLIC VOID INTERFACEMETHOD()   DEFAULT INTERFACE METHOD
        void I1.INTERFACEMETHOD()
        {
            Console.WriteLine(“INTERFACE I1”);
            Console.ReadLine();
        }
        void I2.INTERFACEMETHOD()
        {
            Console.WriteLine(“INTERFACE I2”);
            Console.ReadLine();
        }

        static void Main()
        {
            Program P = new Program();
           // P.INTERFACEMETHOD(); DEFAULT INTERFACE CALLING
            ((I1)P).INTERFACEMETHOD();     
            ((I2)P).INTERFACEMETHOD();   // EXPLICIT INTERFACE METHOD CALLING
            Console.ReadLine();
        }

    }
}


Share this:

STRUCTS IN C#

STRUCTS

Just like classes structs can have
1. Private Fields
2. Public Properties
3. Constructors
4.Methods


Object initializer syntax, intriduced in c# 3.0 can be used to initialize either a structor or a class
Note: There are several differences between classes and structs which we will be looking at in a later session.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ConsoleApplication4;
namespace ConsoleApplication4
{
    public struct Customer
    {
        private int _id;
        private string _Name;
        public string Name
        {
            get { return _Name; }
            set { _Name = value; }
        }
        public int ID
        {
            get { return this._id; }
            set { this._id = value; }
        }
        public Customer(int Id, string Name)
        {
            this._id = Id;
            this._Name = Name;
        }
        public void PrintDetails()
        {
            Console.WriteLine(“id= {0}, name = {1}”, this._id, this._Name);
        }
     
    }
        public class Program
        {
            static void Main()
            {
                Customer C1 = new Customer(101, “Mallareddy”);
                C1.PrintDetails();
                Console.ReadLine();
                Customer C2 = new Customer();
                C2.ID = 102;
                C2.Name = “Mitali”;
                C2.PrintDetails();
                Console.ReadLine();
                Customer C3 = new Customer
                {
                    ID = 103,
                    Name = “AYAN”
                 
                };
                C3.PrintDetails();
                Console.ReadLine();
            }

        }
    }

=====================================================================

A struct is a value type where as a class is a reference type.
All the differences that are applicable to value types and reference types are also applicable to classes and structs.


Structs are stored on stack, where as classes are stored on the heap.
Value types hold their value in memory where they are declared, but reference types hold a reference to an object in memory.


Value types are destroyed immediately after the scope is lost, where as for reference types only the reference variable is destroyed after the scope is lost. the object is later destroyed by garbage collector. (We will talk about this in the garbage collection session).


When you copy a struct into another struct ,  a new copy of that struct gets created and modification on one struct will not affect the values contained by the other struct.


When you copy a class into another class, we only get a copy of the reference variable. Both the reference variable point to the same object on the heap. so , operations on one variable will affect the values contained by the other reference variable.  

using System;

using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ConsoleApplication4;

namespace ConsoleApplication4

{
    public class  Customer
    {
        public int ID{get; set;}
        public string Name{get;set;}
    }
        
}
        public class Program
        {
            static void Main()
            {
                int i = 0;
                
                if(i==10)
                {
                  int j = 20;
                    Customer C1 = new Customer();
                    C1.ID = 101;
                    C1.Name = “Mark”;
                }
                Console.WriteLine(“Hello”);
                Console.ReadLine();
            
            }       
}

=======================

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ConsoleApplication4;

namespace ConsoleApplication4

{
    public class  Customer
    {
        public int ID{get; set;}
        public string Name{get;set;}
    }
        
}
        public class Program
        {
            static void Main()
            {
                int i = 10;
                int j = i;
                j = j + 1;

                Console.WriteLine(“i = {0} && j= {1}”, i, j);


                Customer C1 = new Customer();

                C1.ID = 101;
                C1.Name = “malla”;
                                                  // when we copy reference the memory is mapped to same location//
                Customer C2 = C1;
                C2.Name = “mark”;
                Console.WriteLine(“C1.Name = {0}, C2.Name = {1}”, C1.Name, C2.Name);
                Console.ReadLine();              
                        }
}

> “classes” can have destructors but “struct” can not have destructors.


Classes vs Structs


> Structs can’t have destructors, but classes can have destructors


> Structs cannot have explicit parameter less constructors where as a class can.


> Struct can’t inherit from another class where as a class can, Both structs and classes can         inherit from an interface.



>Example of structs in the .NET Framework -int(System.int32), double(System.Double) etc..




Share this: