next


OverLoading
Polymorphism


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

namespace ConsoleApplication20
{
    class A
    {
        public void show()
        {
            Console.WriteLine("hi");
        }

        public void show()
        {
            Console.WriteLine("wel");
        }

        public void show()
        {
            Console.WriteLine("java");
        }


    }


    class Program
    {
        static void Main(string[] args)
        {
            A oa = new A();
            oa.show();
            Console.Read();
        }
    }
    }
 // OUTPUT: ERROR

Method Overloading

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

namespace ConsoleApplication20
{
    class A
    {
        public void show()
        {
            Console.WriteLine("hi");
        }

        public void show(int x)
        {
            Console.WriteLine("wel");
        }

        public void show(int x,int y)
        {
            Console.WriteLine("java");
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            A oa = new A();
            oa.show(5);
            Console.Read();
        }
    }
    }


Constructor Overloading

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

namespace ConsoleApplication20
{
    class A
    {
     public  A()
        {
            Console.WriteLine("hi");
        }

        public A (int x)
        {
            Console.WriteLine("wel");
        }

        public A(int x,int y)
        {
            Console.WriteLine("java");
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            A oa = new A();
            Console.Read();
        }
    }
}

Method Overridding

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

namespace ConsoleApplication20
{


    class A
    {
        public void show()
        {
            Console.WriteLine("hi");
        }
    }
    class B:A
    {
        public void show(int x)
        {
            Console.WriteLine("welome");
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            B ob = new B();
            ob.show();
            Console.Read();
        }
    }
}

Delegates

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

namespace DelegatesDemo
{
    class Sample
    {
        public int GetSum(int a, int b, int c)
        {
            return (a + b + c);
        }
    }
    class Program
    {
        delegate int SampleDelegate(int a, int b, int c);
        static void Main(string[] args)
        {
            Sample s = new Sample();
            SampleDelegate sd = new SampleDelegate(s.GetSum);
            Console.WriteLine("Target Method: " + sd.Target);
            Console.WriteLine("Target Method Signature: " + sd.Method);
            Console.WriteLine("Sum is: " + sd.Invoke(10, 20, 30)); //calls the method
            Console.Read();
        }
    }
}

Generic Classes
Generics


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

namespace GenericsDemo
{
    class Sample<T>
    {
        T n;
        public void Set(T n)
        {
            this.n = n;
        }
        public void Print()
        {
            Console.WriteLine(n);
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            Sample<int> s1 = new Sample();
            s1.Set(10);
            s1.Print();
            Sample<string> s2 = new Sample();
            s2.Set("hai");
            s2.Print();
            Console.Read();
        }
    }
}


Generic Methods


namespace MethodGenericsDemo
{
    class Sample
    {
        public void ReverseAndPrint<T>(T[] arr)
        {
            Array.Reverse(arr);
            foreach (T item in arr)
                Console.Write(item + ", ");
            Console.WriteLine();
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            int[] intArray = { 3, 5, 7, 9, 11 };
            string[] stringArray = { "first", "second", "third" };
            double[] doubleArray = { 3.567, 7.891, 2.345 };
            Sample s = new Sample();
            s.ReverseAndPrint<int>(intArray);
            s.ReverseAndPrint<string>(stringArray);
            s.ReverseAndPrint<double>(doubleArray);
            Console.Read();
        }
    }
}


List
Collections

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

namespace ListClass
{
    class Program
    {
        static void Main(string[] args)
        {
            List MyMessages = new List();
            MyMessages.Add("Good Morning");
            MyMessages.Add("Good Afternoon");
            MyMessages.Add("Good Evening");
            MyMessages.Add("Good Night");
            //MyMessages.Add(10); //error
            Console.WriteLine(MyMessages.Count + " messages found.\n");
            foreach (string s in MyMessages)
            {
                Console.WriteLine(s);
            }
            Console.Read();
        }
    }
}

Array List


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

namespace ArrayListClass
{
    class Program
    {
        static void Main(string[] args)
        {
            ArrayList al = new ArrayList();
            al.Add("Hai");
            al.Add("How r u");
            al.Add(1000);
            al.Add(true);
            al.Add(DateTime.Now);
            Console.WriteLine(al.Count + " values found.\n");
            for (int i = 0; i < al.Count; i++)
                Console.WriteLine(al[i]);
            Console.Read();
        }
    }
}

LINQ to Array
LINQ


namespace LINQtoArrayDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            //data source
            int[] numbers = { 2, 12, 5, 15 };
            //ling query with where clause
            var lowNums = from n in numbers where n < 10 select n;
            //output
            foreach (var x in lowNums)
                Console.Write(x + ",");
            Console.Read();
        }
    }
}

LINQ to Objects


namespace LINQtoObjectsDemo
{
    class Student
    {
        public int StudentID;
        public string Name;
        public string Course;
        public int Marks;
        public Student(int StudentID, string Name, string Course, int Marks)
        {
            this.StudentID = StudentID;
            this.Name = Name;
            this.Course = Course;
            this.Marks = Marks;
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            //data source
            Student[] stu = { new Student(101, "Prakash", "MBA", 765),
                                  new Student(102, "Pradeep", "MBA", 471),
                                  new Student(103, "Pushpa", "Msc", 590),
                                  new Student(104, "Purna", "MCA", 223),
                                  new Student(105, "Purnima", "MCA", 450)};
            //linq query with where clause
            var query1 = from s in stu where s.Course == "MCA" select s;
            Console.WriteLine("MCA Students:");
            foreach (var q in query1)
                Console.WriteLine(q.StudentID + ", " + q.Name + ", " + q.Course + ", " + q.Marks);
            //linq query with let and where clauses
            var query2 = from s in stu let avg=s.Marks/10 where avg<35 select s;
            Console.WriteLine("\nFailed Students:");
            foreach (var q in query2)
                Console.WriteLine(q.StudentID + ", " + q.Name + ", " + q.Course + ", " + q.Marks);
            //linq query with orderby clause
            var query3 = from s in stu orderby s.Marks select s;
            Console.WriteLine("\nStudents (sort on marks):");

            foreach (var q in query3)
                Console.WriteLine(q.StudentID + ", " + q.Name + ", " + q.Course + ", " + q.Marks);
            //linq query with orderby clause (descending)
            var query4 = from s in stu orderby s.Marks descending select s;
            Console.WriteLine("\nStudents (sort on marks - descending):");
            foreach (var q in query4)
                Console.WriteLine(q.StudentID + ", " + q.Name + ", " + q.Course + ", " + q.Marks);
            //linq query with group clause
            var query5 = from s in stu group s by s.Course;
            Console.WriteLine("\nStudents with grouping:");
            foreach (var p in query5)
            {
                Console.WriteLine(p.Key + ":");
                foreach (var q in p)
                    Console.WriteLine(" " + q.StudentID + ", " + q.Name + ", " + q.Course + ", " + q.Marks);
            }
            Console.Read();
        }
    }
}

Simple Demo on Exceptions
Exception
                      

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

namespace ExceptionDemo1
{
    class Program
    {
        static void Main(string[] args)
        {
            string[] Cities = { "Vijayawada", "New Delhi", "Banglore", "Hyderabad" };
            Console.WriteLine(Cities[3]);
            Console.WriteLine(Cities[4]);
            Console.Read();
        }
    }
}

In the above code, the highlighted line contains an error, because it is trying to access an array element which is in out of range of the array. So, it leads to abnormal application termination at run time. To avoid this, we have to implement exception handling for this code.

Exception Demo


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ExceptionDemo2
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                string[] Cities = { "Vijayawada", "New Delhi", "Banglore", "Hyderabad" };
                Console.WriteLine(Cities[3]);
                Console.WriteLine(Cities[4]);
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error occurred.");
            }
            Console.Read();
        }
    }
}


Exceptional Classes

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

namespace ExceptionDemo3
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                int n1, n2, n3;
                Console.WriteLine("Enter first value:");
                n1 = Convert.ToInt32(Console.ReadLine());
                Console.WriteLine("Enter second value:");
                n2 = Convert.ToInt32(Console.ReadLine());
                n3 = n1 / n2;
                Console.WriteLine("Result is: " + n3);
                Console.Read();
            }
            catch (DivideByZeroException ex)
            {
                Console.WriteLine("Can't divide the number with zero.");
            }
            Console.Read();
        }
    }
}

Multiple Catch Blocks


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

namespace ExceptionDemo4
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                int n1, n2, n3;

                Console.WriteLine("Enter first value:");
                n1 = Convert.ToInt32(Console.ReadLine());
                Console.WriteLine("Enter second value:");
                n2 = Convert.ToInt32(Console.ReadLine());
                n3 = n1 / n2;
                Console.WriteLine("Result is: " + n3);
                Console.Read();
            }
            catch (DivideByZeroException ex)
            {
                Console.WriteLine("This is divide by zero exception.");
            }
            catch (OverflowException ex)
            {
                Console.WriteLine("This is overflow excpetion.");
            }
            catch (FormatException ex)
            {
                Console.WriteLine("This is invalid cast exception.");
            }
            Console.Read();
        }
    }
}


Simple Demo on Multi Threading
Threading



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

namespace SimpleThreadingDemo
{
    class ThreadingDemo
    {
        private void FirstMethod()
        {
            for (int i = 1; i <= 300; i++)
                Console.Write("i=" + i + " ");
        }
        private void SecondMethod()
        {
            for (int j = 1; j <= 300; j++)
                Console.Write("j=" + j + " ");
        }
        public void Display()
        {
            Thread th1 = new Thread(FirstMethod);
            Thread th2 = new Thread(SecondMethod);
            th1.Start();
            th2.Start();
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            ThreadingDemo td = new ThreadingDemo();
            td.Display();
            Console.Read();
        }
    }
}

Demo on Thread States
                                
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace ThreadLifeCycleDemo
{
    class ThreadingDemo
    {
        private void FirstMethod()
        {
            for (int i = 1; i <= 50; i++)
            {
                Console.Write("i=" + i + " ");
                Thread.Sleep(1000);
            }
        }
        private void SecondMethod()
        {
            for (int j = 1; j <= 50; j++)
            {
                Console.Write("j=" + j + " ");
                Thread.Sleep(1000);
                if (j == 20)
                    th1.Suspend();
                else if (j == 30)
                    th1.Resume();
            }
        }
        Thread th1, th2;
        public void Display()
        {
            th1 = new Thread(FirstMethod);
            th2 = new Thread(SecondMethod);
            th1.Start();
            th2.Start();
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            ThreadingDemo td = new ThreadingDemo();
            td.Display();
            Console.Read();
        }
    }
}

Demo on “DriveInfo” class – For a Single Drive
Manipulating Drives
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace DriveManipulationsDemo
{
    class Program
    {
        static void Main(string[] args)

        {
            Console.WriteLine("Enter the drive letter (a to z):");
            string driveletter = Console.ReadLine();
            DriveInfo d = new DriveInfo(driveletter);
            if (d.IsReady)
                Console.WriteLine(d.Name + " - " + d.DriveType + " - " + d.VolumeLabel + " - " + d.DriveFormat + " - " + d.TotalSize + " bytes - " + d.TotalFreeSpace + " bytes.");
            else
                Console.WriteLine(d.Name + " - " + " Not Ready.");
            Console.Read();
        }
    }
}

“DriveInfo” class – For Multiple Drives



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

namespace GetDrivesDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            DriveInfo[] dinfo = DriveInfo.GetDrives();
            Console.WriteLine(dinfo.Length + " drives found on this computer.\n");
            foreach (DriveInfo d in dinfo)
            {
                if (d.IsReady)
                    Console.WriteLine(d.Name + " - " + d.DriveType + " - " + d.VolumeLabel + " - " + d.DriveFormat + " - " + d.TotalSize + " bytes - " + d.TotalFreeSpace + " bytes.");
                else
                    Console.WriteLine(d.Name + " - " + d.DriveType + " - Not Ready.");
            }
            Console.Read();
        }
    }
}



Demo on “DirectoryInfo” class
“Folders” Handling

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

namespace DirectoryInfoDemo1
{
    class Program
    {
        static void Main(string[] args)
        {
            string directorypath = "c:\\windows\\help";
            DirectoryInfo dinfo = new DirectoryInfo(directorypath);
            if (dinfo.Exists)
            {
                Console.WriteLine("Directory Name: " + dinfo.Name);
                Console.WriteLine("Directory Full Path: " + dinfo.FullName);
                Console.WriteLine("Created on: " + dinfo.CreationTime);
                Console.WriteLine("Last accessed on: " + dinfo.LastAccessTime);
                Console.WriteLine("Last modified on: " + dinfo.LastWriteTime);
                Console.WriteLine("Parent: " + dinfo.Parent.FullName);
                Console.WriteLine("Root: " + dinfo.Root);

                Console.WriteLine("\nFiles:");
                FileInfo[] fobjs = dinfo.GetFiles();
                foreach (FileInfo f in fobjs)
                    Console.WriteLine(f.FullName);

                Console.WriteLine("\nDirectories:");
                DirectoryInfo[] dobjs = dinfo.GetDirectories();
                foreach (DirectoryInfo d in dobjs)
                    Console.WriteLine(d.FullName);
            }
            else
                Console.WriteLine(directorypath + " is not available on the system.");
            Console.Read();
        }
    }
}

Create the Directory

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

namespace DirectoryInfoDemo2
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Enter the directory path:");
            string directorypath = Console.ReadLine();
            DirectoryInfo dinfo = new DirectoryInfo(directorypath);
            if (!dinfo.Exists)
            {
                dinfo.Create();
                Console.WriteLine("Directory created.");                
            }
            else
                Console.WriteLine(directorypath + " is already exists.");
            Console.Read();
        }
    }
}

Delete the Directory

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

namespace DirectoryInfoDemo3
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Enter the directory path:");
            string directorypath = Console.ReadLine();
            DirectoryInfo dinfo = new DirectoryInfo(directorypath);
            if (dinfo.Exists)
            {
                dinfo.Delete(true);
                Console.WriteLine("Directory deleted.");
            }
            else
                Console.WriteLine(directorypath + " doesn't exists.");
            Console.Read();
        }
    }
}

Demo on “FileInfo” class
File Manipulations

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

namespace FileInfoDemo1
{
    class Program
    {
        static void Main(string[] args)
        {
            string filepath;
            Console.WriteLine("Enter the file path:");
            filepath = Console.ReadLine();
            FileInfo fobj = new FileInfo(filepath);
            if (fobj.Exists)
            {
                Console.WriteLine("Name: " + fobj.Name);
                Console.WriteLine("Full Name: " + fobj.FullName);
                Console.WriteLine("Extension: " + fobj.Extension);
                Console.WriteLine("Directory: " + fobj.DirectoryName);
                Console.WriteLine("File Size: " + fobj.Length);
                Console.WriteLine("Created on: " + fobj.CreationTime);
                Console.WriteLine("Last Accessed on: " + fobj.LastAccessTime);
                Console.WriteLine("Last Modified on: " + fobj.LastWriteTime);
            }
            else
                Console.WriteLine("File not found.");
            Console.Read();
        }
    }
}

File Reading

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

namespace FileReadDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            string filepath;
            Console.WriteLine("Enter the file path:");
            filepath = Console.ReadLine();
            FileInfo fobj = new FileInfo(filepath);
            if (fobj.Exists)
            {
                StreamReader sr = new StreamReader(filepath);
                string content = sr.ReadToEnd();
                Console.WriteLine(content);
                sr.Close();
            }
            else
                Console.WriteLine("File not found.");
            Console.Read();
        }
    }
}

File Writing

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

namespace FileWriteDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            string filepath;
            Console.WriteLine("Enter the file path:");
            filepath = Console.ReadLine();
            FileInfo fobj = new FileInfo(filepath);
            if (!fobj.Exists)
            {
                string content;
                Console.WriteLine("\nEnter content to write:");
                content = Console.ReadLine();
                StreamWriter sw = new StreamWriter(filepath);
                sw.Write(content);
                sw.Close();
                Console.WriteLine("\nWritten successfully!");
            }
            else
                Console.WriteLine("File already exists.");
            Console.Read();
        }
    }
}