C#

Drop Down MenusCSS Drop Down MenuPure CSS Dropdown Menu

Wednesday, 30 July 2014

Convert One List to Another List without For loop or Foreach Loop

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

namespace OverloadingConstructor
{
    class TargetList
    {
        public TargetList(string Id,string FirstName,String LastName)
        {
            this.LastName = LastName;
            this.FirstName = FirstName;
            this.Id = Id;
        }
        public string Id { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        
    }

    class SourceList
    {
        public string Id { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
    
    }

    class Program
    {
        public static TargetList PointFToPoint(SourceList pf)
        {
            return new TargetList(pf.Id,pf.FirstName,pf.LastName);
        }

        static void Main(string[] args)
        {
            List<SourceList> sourceList = new List<SourceList>();

            sourceList.Add(new SourceList { FirstName = "Anurag", LastName = "Nayak", Id = "1" });
            sourceList.Add(new SourceList { FirstName = "Abhishek", LastName = "Nayak", Id = "2" });
            sourceList.Add(new SourceList { FirstName = "Siba", LastName = "Dalai", Id = "3" });
            sourceList.Add(new SourceList { FirstName = "Manju", LastName = "Rath", Id = "4" });

            List<TargetList> Target = sourceList.ConvertAll(new Converter<SourceList, TargetList>(PointFToPoint));
            Console.ReadLine();
        }
    }

}

----Another Nice way to convert is 

List<TargetList> Target1 = sourceList.ConvertAll(x => new TargetList {  FirstName=x.FirstName, Id=x.Id}).ToList();


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

namespace OverloadingConstructor
{
    class TargetList
    {
       
        public string Id { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }

        public List<string> DepId { get; set; }
    }

    class SourceList
    {
        public string Id { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }

        public List<string> DepId { get; set; }
    
    }

    class Program
    {
    
        static void Main(string[] args)
        {
            List<SourceList> sourceList = new List<SourceList>();

            sourceList.Add(new SourceList { FirstName = "Anurag", LastName = "Nayak", Id = "1", DepId = new List<string> { "1","2","3"} });
            sourceList.Add(new SourceList { FirstName = "Abhishek", LastName = "Nayak", Id = "2", DepId = new List<string> { "11", "21", "31" } });
            sourceList.Add(new SourceList { FirstName = "Siba", LastName = "Dalai", Id = "3", DepId = new List<string> { "21", "22", "23" } });
            sourceList.Add(new SourceList { FirstName = "Manju", LastName = "Rath", Id = "4", DepId = new List<string> { "31", "32", "33" } });

            List<TargetList> Target1 = sourceList.ConvertAll(x => new TargetList {  FirstName=x.FirstName, Id=x.Id, DepId=x.DepId}).ToList();
            Console.ReadLine();
        }
    }

}







Stars upper half program

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

namespace OverloadingConstructor
{


    class Program
    {
        static void Main(string[] args)
        {
            int num = 8;
            int x;

            for (int i = 0, c = num; i <= num; i++, c--)
            {
                x = c;
                for (int j = 1; j <= i; j++)
                {
                    while (x >= 1)
                    {
                        Console.Write(" ");
                        x--;
                    }
                    Console.Write("* ");

                }
                Console.WriteLine();
            }

            Console.ReadLine();
        }
    }

}






Star Up and Down Program

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

namespace OverloadingConstructor
{


    class Program
    {
        static void Main(string[] args)
        {
            int num = 10,x;
            StringBuilder s = new StringBuilder();
            List<string> list = new List<string>();
            for (int i = 1, c = num; i <= num; i++,c--)
            {
                x = c;
                for (int j = 1; j <= i; j++)
                {
                    while (x >= 1)
                    {
                        Console.Write(" ");
                        s.Append(" ");
                        x--;
                    }
                    Console.Write("* ");
                    s.Append("* ");
                }
                if(i!=num)
                list.Add(s.ToString());
                s.Length = 0;
                Console.WriteLine();
            }

            foreach (string stringfinal in list.OrderByDescending(x1=>x1.Length))
            {
                Console.WriteLine(stringfinal);
            }
            Console.ReadLine();
        }
    }
}


Trick 2

using System;
using System.Collections.Generic;

namespace StarUpDown
{
    class Program
    {
        static void Main(string[] args)
        {
           
            List<string> listStr = new List<string>();
            int x = 10;
            for (int i = 1; i <= x; i++)
            {
                string s = "";
                int count = x - i;
                for (int j = 1; j <=i; j++)
                {
                    while (count > 0)
                    {
                        Console.Write(" ");
                        count--;
                        s +=" ";
                    }
                    Console.Write("* ");
                    s += "* ";
                 

                }
                if (i != x)
                listStr.Add(s);
                Console.WriteLine();
            }

            listStr.Reverse();

            foreach (var item in listStr)
            {
                Console.WriteLine(item);
            }
            Console.ReadLine();
        }
    }
}





A program on Star

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

namespace OverloadingConstructor
{


    class Program
    {
        static void Main(string[] args)
        {
            string[,] array = new string[5, 11];

            for (int i = 0; i <= 4; i++)
            {
                for (int j = 0; j <= 10; j++)
                {
                    array[i, j] = "*";
                }
                Console.WriteLine();
            }

            for (int k = 0; k <= 4; k++)
            {
                for (int l = (k+1); l <=(10-(k+1)); l++)
                {
                    array[k, l] = " ";
                }
            }

            for (int i = 0; i <= 4; i++)
            {
                for (int j = 0; j <= 10; j++)
                {
                   Console.Write(array[i, j]);
                }
                Console.WriteLine();
            }

            Console.ReadLine();
        }
    }
}





Wednesday, 23 July 2014

Basics of String Literals and Verbatim String Literal


รจString Literal is a set of characters enclosed by double quotes.
“this is string literal”

    class Program
    {
        static void Main(string[] args)
        {
            string s = @"this is string1
literal";
            Console.WriteLine(s);
            Console.ReadLine();
        }

    }





รจ class Program
    {
        static void Main(string[] args)
        {
            string s = "this is string1 \nliteral";
            Console.WriteLine(s);
            Console.ReadLine();
        }
    }

รจVerbatim string literal starts with @ , which is followed by a quoted string. The contents of the quoted string are accepted without modification and can span two or more lines. So we can avoid escape sequence.

class Program
    {
        static void Main(string[] args)
        {
            string s = @"this is ""string1"" literal";
            Console.WriteLine(s);
            Console.ReadLine();
        }
    }





class Program
    {
        static void Main(string[] args)
        {
            string s = "this is \"string1\" literal";
            Console.WriteLine(s);
            Console.ReadLine();
        }
    }

รฐ Verbatim string literals are wonderful benefit for many formatting situation.

Sunday, 20 July 2014

Constructor Initialization

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

namespace OverloadingConstructor
{
    class A
    {
        public int a, b;

        public A():this(0,0)
        {
            Console.WriteLine("Inside A()");
        }

        public A(int a, int b)
        {
            this.a = 10;
            this.b = b;
            Console.WriteLine("Inside A(int a, int b)");
        }

        public A(A obj):this(obj.a,obj.b)
        {
            Console.WriteLine("Inside A(obj)");
        }
   
    }


    class Program
    {
        static void Main(string[] args)
        {
            A obj = new A(8,9);
            A objduplicate = new A(obj);
            Console.WriteLine("obj.a-->" + objduplicate.a + " " + "obj.b-->" + objduplicate.b);



            Console.ReadLine();


        }
    }

}




Extension Method how to Use it

Extension Methods

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

namespace lambda
{
    class mathclass
    {

        public int Sum(int a, int b)
        {

            return a + b;

        }
    }

}

How Extension Method is done

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using lambda;
namespace ConsoleApplication1
{
     class Program
    {
        static void Main(string[] args)
        {
            mathclass obj = new mathclass();
             Console.WriteLine( obj.substrat(12, 2));
             Console.WriteLine(obj.Divide1(12, 2));
             Console.WriteLine(obj.Sum(10, 12));
             Console.ReadLine();
        }
     }

    static class substract
     {
        public static int substrat(this mathclass obj, int num1, int num2)
         {

             return num1 - num2;

         }
        public static int Divide1(this mathclass obj, int num1, int num2)
         {

             return num1 / num2;

         }
     }

      
}








Operator Overloading simple example

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

namespace operator_overloading
{

    class Third
    {
        public int number { get; set; }

        public int num = 0;

        public static Third operator +(Third t1, Third t2)
        {

            Third t3 = new Third();

            t3.num = t1.number + t2.number;

            return t3;
        }

    }

    class Program
    {

        static void Main(string[] args)
        {


            Third t1 = new Third();
            t1.number = 50;

            Third t2 = new Third();
            t2.number = 60;
            Third t3 = new Third();


            t3 = t1 + t2;

            Console.WriteLine(t3.num);
            Console.ReadLine();




        }
    }

}


Abstract Class Example


ABSTRACT  CLASS  IN DETAIL
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace OverloadingConstructor
{
    abstract class Employee
    {
        public static int tax;
        public int EmpId;
        public string EmployeeName;
        public double SalaryPerDay;

        static Employee()
        {
            tax = 200;
        }

        public Employee()
        {

        }

        public Employee(int EmpId, string EmployeeName, double SalaryPerDay)
        {
            this.EmpId = EmpId;
            this.EmployeeName = EmployeeName;
            this.SalaryPerDay = SalaryPerDay;
        }
        public void Display()
        {
            Console.WriteLine("Employee ID:" + EmpId);
            Console.WriteLine("EmployeeName:" + EmployeeName);
            Console.WriteLine("SalaryPerDay:" + SalaryPerDay);
            Console.WriteLine("tax:" + tax);

        }
        public abstract void CalculateSalary(int NumberOfDays);

    }

    class LibraryManager : Employee
    {
        int LibId;
        public LibraryManager(int EmpId, string EmployeeName, double SalaryPerDay, int LibId)
            : base(EmpId, EmployeeName, SalaryPerDay)
        {

            this.LibId = LibId;

        }

        public new virtual void Display()
        {

            base.Display();
            Console.WriteLine("LibraryId" + LibId);

        }

        public override void CalculateSalary(int NumberOfDays)
        {

            double TotalSalaryLibraryIncharger = ((SalaryPerDay * NumberOfDays) - tax);
            Console.WriteLine("The Total Salary of TotalSalaryLibraryIncharger" + TotalSalaryLibraryIncharger);
        }


    }

    class Admin : Employee
    {

        public Admin(int EmpId, string EmployeeName, double SalaryPerDay, int LibId)
            : base(EmpId, EmployeeName, SalaryPerDay)
        {

        }

        public new virtual void Display()
        {

            base.Display();


        }

        public override void CalculateSalary(int NumberOfDays)
        {

            double TotalSalaryAdmin = ((SalaryPerDay * NumberOfDays + (SalaryPerDay * NumberOfDays) * 0.15) - tax);
            Console.WriteLine("The Total Salary of Admin" + TotalSalaryAdmin);
        }


    }

    class Program
    {
        static void Main(string[] args)
        {
            //LibraryManager objLibrary = new LibraryManager(1, "Anurag", 25.0, 100);
            //objLibrary.Display();
            //objLibrary.CalculateSalary(30);

            //Admin objAdmin = new Admin(1, "Anurag", 25.0, 100);
            //objAdmin.Display();
            //objAdmin.CalculateSalary(30);
            Employee E;
            E = new LibraryManager(2, "Abhishek", 30, 100);


           
            Console.WriteLine("=============***********Salary Structure*************===================");

            Console.WriteLine("1: Library Admin");
            Console.WriteLine("2: Admin");
            Console.WriteLine("Choose 1/2");

            int choose = int.Parse(Console.ReadLine());

            switch (choose)
            {

                case 1:
                    E = new LibraryManager(1, "Anurag", 25.0, 100);
                    break;
                case 2:
                    E = new Admin(1, "Anurag", 25.0, 100);
                    break;
                default:
                    break;
            }

            E.Display();
            E.CalculateSalary(30);

            Console.WriteLine("=============***********Thank You*************===================");
            Console.ReadLine();
        }
    }
}