Friday 15 February 2013

Importatnt Programs in C#


Demo on Class , Members and Object

using System;

namespace FreshersLike
{
    class Test
    {
        public int ns;            //Non-Static Member
        public static int s;    //Static Member
    }

    class MembersDemo
    {
        public static  void Main()
        {
            Test t1 = new Test();
            Test t2;

            t2 = new Test();
            t1.ns = 100;

            t2.ns = 1000;
            // t1.s = 500; Error

            Test.s = 500;
            Console.WriteLine("{0} , {1} , {2} ", t1.ns , t2.ns , Test.s);
        }
    }
}
................................................................................................................................


Demo on Optional Paramter & Named Parameter Passing.

using System;

namespace FreshersLike
{
    class Employee
    {

        private int Empno;
        private string Ename , Job;

        private decimal Salary, Commission;

        public void SetEmployee(int eno, string ename, decimal comm = 0, decimal salary = 10000, string job = "SALESMAN")
        {
            Empno = eno;

            Ename = ename;

            Job = job;

            Salary = salary;

            Commission = comm;
        }

        public void ShowEmployee()
        {
            Console.WriteLine("Employee # : {0} \nEmployee Name : {1} \nSalary : Rs. {2} \nCommission : Rs. {3} \nJob : {4} ", Empno , Ename , Salary , Commission , Job );
        }
    }

    class OptionalParameterDemo
    {
        public static void Main()
        {
            Employee emp = new Employee();

            //emp.SetEmployee(101, "Ravi", salary: 50000, job: "Manager");

            emp.SetEmployee(101, "Kumar", 1000);

            emp.ShowEmployee();
        }
    }
}

.........................................................................................................................................

Demo on Parsing


using System;

namespace FreshersLike
{
    class ParsingDemo
    {
        public static void Main()
        {
            int Empno;
            string Ename;
            decimal Salary;
            DateTime Hiredate;

            Console.Write("Enter Employee # : ");
            Empno = int.Parse(Console.ReadLine());

            Console.Write("Enter Employee Name : ");
            Ename = Console.ReadLine();

            Console.Write("Enter Salary : ");
            Salary = decimal.Parse(Console.ReadLine());

            Hiredate = DateTime.Now; //Assigns the current system data and time 

            Console.Clear(); //Clears the Screen

            Console.ForegroundColor = ConsoleColor.Yellow; //Sets the Fore color of text

            Console.WriteLine("Employee # : {0} \nEmployee Name : {1} \nSalary : Rs. {2} \nDate of Joining : {3} ", Empno, Ename, Salary, Hiredate);
        }
    }
}

........................................................................................................................................


//Demo on Type Casting

using System;

namespace CSIntro303
{
    class TypeCastingDemo
    {
        public static void Main()
        {
            int no1, no2;
            float result1, result2;

            Console.WriteLine("Enter Two Numbers Below : " );
            no1 = int.Parse(Console.ReadLine());
            no2 = int.Parse(Console.ReadLine());

            result1 = no1 / no2;
            result2 = (float)no1 / no2; //Type Casting

            Console.WriteLine("{0} / {1} = {2} " , no1,no2,result1);
            Console.WriteLine("{0} / {1} = {2} ", no1, no2, result2);
        }
    }
}
















1 comment: