Skip to main content

Posts

Override in built ToString() method in C# example

Below is the small source code which demonstrate how to override in built ToString() method in C# which is mostly used in exception handling. using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace override_tostring_demo {     class A     {         public override string ToString()         {             return "Hello";             //return base.ToString();         }         class M         {             static void Main(string[] args)             {                 A a = new override_tostring_demo.A();                 Console.WriteLine(a);            ...

Nesting of namespaces and class libraries in C#

Below is the assignment in which we: 1) create a class library with namespaces N3 inside N2 and N2 inside N1. N1 contains abstract class A which contains abstract method abc. Namespace N2 contains interface Int1 which has Power method. N3 namespace has Interface Int2 contains reverse number function. We build this file. 2) In second file we create another class library having namespace N4 which has interface Int3 which inherits Int2. and contains ispalindrome method. N5 namespace is nested within namespace N4 which has a class B which inherits Class A , int1 and Int3. Class B has implementation of all 4 methods such as abc, pow, revno and isPal. We create build of this file. Also we import the dll of previous file into this. 3) Now we create a console application contains class M and this class has main method. We create an object of B class and call all the 4 methods into main method. In this file we will add references(dll's) of above 2 files in order to access there data....

Class library reference demo in C#

Below is the class library project in which namespaces are nested. Here N2 namesapces is within N1 namespace. Both contains one class and method. we will first build the solution. After that, dll is created in bin folder in the same package of below program. we will add the reference of this newly created dll file into another program so that it will use the N1 and N2 namespaces contents i.e. all classes and method implementations. using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace N1 {     public class A     {         public void ABC()         {             Console.WriteLine("ABC of A");         }     }     namespace N2     {         public class B         {             public vo...

Minus operator overload in C#

Below is the code to overload "minus" operator in C#. using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace oproverloadDemo {     class A     {         int x;         A()         {             x = 0;         }         A(int x)         {             this.x = x;         }         public static A operator+(A a1, A a2)         {             A a3 = new A();             a3.x = a1.x + a2.x;             return a3;         }         void show()         {           ...

Constructor example in C#

When a class is created, its  constructor  is called.  Constructors  have the same name as the class, and they usually initialize the data members of the new object. using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace constructorDemo {     class Emp     {         int eno, sal;         string name;         public Emp()         {             eno = 0;             sal = 1200;             name = "-";         }         public Emp(int eno, String name, int sal)         {             set(eno, name, sal);         }         public void set(int eno,string name, int...

Property in C# with examples

Below is the small example of get() and set() properties in C#. using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace studentpropertyDemo {     class student     {         int rno;         string name,std;         float per;         public int studrno         {             set             {                 rno = value;             }             get             {                 return rno;             }         }         public float studper         { ...

Function overloading example in C#

Method Overloading :  is using the same method name with different type of parameters or different set of parameters using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace funoverloadingDemo {     class Program     {         void add(int x, int y)         {             int z = x + y;             Console.WriteLine("addtion is {0}", z);         }         void add(float x, float y)         {             float z = x + y;             Console.WriteLine("addtion is {0}", z);         }         void add(int a, int b, int c)         {             int s = a + b + c; ...