Wednesday, 16 May 2018

Static Constructor in C#





STUDENT CLASS:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;



namespace ConsoleApplication18.Model

{

    class student

    {



        int id;

        string name;

        static string collegename;

        public student(int id,string name)

        {

            this.id = id;

            this.name = name;

        }

        static student()

        {

            collegename = "Govt Dehli College";

        }



        public void display()

        {

           

            Console.ForegroundColor = System.ConsoleColor.Green;

            Console.WriteLine("************************************");

            Console.ForegroundColor = System.ConsoleColor.Magenta;

            Console.WriteLine("EnrolmentID: "+id);

            Console.WriteLine("Name : "+name);

            Console.WriteLine("College: "+collegename);



            Console.ForegroundColor = System.ConsoleColor.Green;

            Console.WriteLine("************************************");



        }

    }

}

MAIN CLASS:
using ConsoleApplication18.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication18
{
    class Program
    {
        static void Main(string[] args)
        {
            student[] s = new student[]
            {
                new student(123,"salman"),
                 new student(124,"ali"),
                  new student(125,"sana"), 
                  new student(126,"kashif"),
            };

            foreach (student item in s)
            {
                item.display();
            }


        }
    }
}


Tuesday, 15 May 2018

Static Class and Method in C#



Mathematics Class: 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;



namespace ConsoleApplication16.Model

{

   static class Mathematics

    {

       public static float pi = 3.124f;

   public static double areaofcircle(float r)

   {

       return pi * Math.Pow(r, 2);

       



   }





   public static double circumferencecircle(float r)

   {

       return 2 * pi * r;





   }







   }



}

Main class:
using ConsoleApplication16.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication16
{
    class Program
    {
        static void Main(string[] args)
        {

            float r;
            l1:
            Console.WriteLine("Enter the radius of circle:");
            r = float.Parse(Console.ReadLine());
            if (r<0)
            {
                Console.ForegroundColor = System.ConsoleColor.Red;
                Console.WriteLine("Radius cannot be negative.....");
             Console.ForegroundColor=System.ConsoleColor.White;
            }
            else
            {
                Console.ForegroundColor = System.ConsoleColor.Green;
                Console.WriteLine("Area of Circle is " + Mathematics.areaofcircle(r));
                Console.ForegroundColor = System.ConsoleColor.Magenta;
                Console.WriteLine("Circumference  of Circle is " + Mathematics.circumferencecircle(r));
                Console.ForegroundColor = System.ConsoleColor.White;
            }
            Console.WriteLine("Press any key to continue....");
            Console.ReadKey();
            Console.Clear();
            goto l1;
        }
    }
}


Monday, 14 May 2018

static keyword and static field in C#





MODEL CODE:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;



namespace ConsoleApplication15.Model

{

    class student

    {

        int id;

        string name;

        public static string batchcode="1708C1";



        public student(int id,string name)

        {

            this.id = id;

            this.name = name;

        }



        public void displayrecord()

        {

            Console.ForegroundColor = System.ConsoleColor.Red;



            Console.WriteLine("----------------------------------");

            Console.ForegroundColor = System.ConsoleColor.Green;



            Console.WriteLine("ID= " + id);

            Console.WriteLine("Name = "+name);

            Console.ForegroundColor = System.ConsoleColor.Cyan;



            Console.WriteLine("Batch Code: "+batchcode);

            Console.ForegroundColor = System.ConsoleColor.Red;

            Console.WriteLine("----------------------------------");



        }



    }

}

MAIN CLASS CODE:
 class Program
    {
        static void Main(string[] args)
        {
            student[] arr = new student[4];

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

             Console.WriteLine("ENTER THE NAME OF STUDENT: ");
             string n = Console.ReadLine();
             arr[i] = new student((i + 234), n);
               
            }


            foreach (student item in arr)
            {
                item.displayrecord();
            }

            Console.ReadLine();
        }
    }

This keyword in C#





 class Program

    {

        static void Main(string[] args)

        {

            employee EMP = new employee(12, 46000, "SALMAN");

            Console.ReadLine();

        }

    }



Model code:

    class employee

    {

        int id, salary;

        string name;

       public employee(int id,int salary,string name)

        {

            this.id = id;

            this.salary = salary;

            this.name = name;

            Console.WriteLine("ID: "+id);

            Console.WriteLine("NAME: "+name);

            Console.WriteLine("Salary: "+salary); 

       }

        

    }


This keyword in C#





 class Program

    {

        static void Main(string[] args)

        {

            employee EMP = new employee(12, 46000, "SALMAN");

            Console.ReadLine();

        }

    }



Model code:

    class employee

    {

        int id, salary;

        string name;

       public employee(int id,int salary,string name)

        {

            this.id = id;

            this.salary = salary;

            this.name = name;

            Console.WriteLine("ID: "+id);

            Console.WriteLine("NAME: "+name);

            Console.WriteLine("Salary: "+salary); 

       }

        

    }


Sunday, 13 May 2018

Enumeration in C# and its practical use





using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;



namespace ConsoleApplication13

{

    class Program

    {

        public enum gender {Male,Female }

        static void Main(string[] args)

        {l1:

            Console.WriteLine("Press 0 for Male");

            Console.WriteLine("Press 1 for Female");

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

            if (v==0)

            {

                Console.WriteLine("you have seleced "+gender.Male);

            }

            else if (v==1)

            {

                   Console.WriteLine("you have seleced "+gender.Female);

            }

            else

            {

                Console.ForegroundColor = System.ConsoleColor.Red;

                Console.WriteLine("Wrong selection .....");

                Console.ForegroundColor = System.ConsoleColor.White;



            }

            goto l1;

            Console.ReadLine();



        }

    }

}


Wednesday, 9 May 2018

Default and parameterized Constructor in C#





main class :

using ConsoleApplication11.Model;

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;



namespace ConsoleApplication11

{

    class Program

    {

        static void Main(string[] args)

        {

            Employee emp = new Employee();

            emp.id = 1;

            emp.name = "ali";

            emp.salary = 50000;

            emp.showdata();

            Employee emp1 = new Employee(2, "ahmed", 60000);



            emp1.showdata();

            

            





        }

    }



}


Employee class: 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication11.Model
{
    class Employee
    {
        public int id;
        public string name;
        public float salary;
        public Employee() //default constructor..............
        {
            Console.WriteLine("Default Constructor is called");
        }

        public Employee(int id,string name,float salary) //default constructor..............
        {
            this.id = id;
            this.name = name;
            this.salary = salary;
            Console.WriteLine("Parametrized Constructor is called");
        }


        public void showdata()
        {
            Console.ForegroundColor = System.ConsoleColor.Cyan;
            Console.BackgroundColor = System.ConsoleColor.Red;
            Console.WriteLine("ID="+id);
            Console.WriteLine("Name = "+name );
            Console.WriteLine("Salary= "+salary);

        }

    }
}

Pass Dynamically Added Html Table Records List To Controller In Asp.net MVC

Controller Code: using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using ...