Constructors,Members Object,object initializer
Demo on Constructors
using System;
namespace FreshersLike.Constructors
{
class Test
{
public int ns; //Non-Static variable
public static int s; //Static variable
static Test() //Static Constructor
{
s = 100;
//ns = 100; Error : Static constructors cant initialize Non-Static variables
}
public Test() //Non-Static Constructor
{
ns = 1000;
s = 1000;
}
}
class ConstructorsDemo
{
public static void Main()
{
Console.WriteLine("Value of S before object creation : {0} ", Test.s);
Test t = new Test();
Console.WriteLine("Values of NS and S : {0} , {1} " , t.ns , Test.s );
}
}
}
.........................................................................................................................................
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 Object initializer of C# 3.0
using System;
namespace FreshersLike
{
class Department
{
public int Deptno;
public string Dname, Loc;
public static int TotalDepartments;
static Department()
{
TotalDepartments = 0;
}
public Department()
{
TotalDepartments += 1;
}
}
class ObjectIntializerDemo
{
public static void Main()
{
Department d1 = new Department()
{
Deptno = 10,
Dname = "SALES",
Loc = "HYD"
};
Department d2 = new Department()
{
Deptno = 20,
Loc = "Bang"
};
Department d3 = new Department() { Deptno = 30 };
Console.WriteLine("Total Departments : {0} " , Department.TotalDepartments );
Console.WriteLine("Department # : {0} \nDepartment Name : {1} \nLocation : {2} ", d1.Deptno , d1.Dname , d1.Loc );
Console.WriteLine("Department # : {0} \nDepartment Name : {1} \nLocation : {2} ", d2.Deptno, d2.Dname, d2.Loc);
Console.WriteLine("Department # : {0} \nDepartment Name : {1} \nLocation : {2} ", d3.Deptno, d3.Dname, d3.Loc);
}
}
}
.....................................................................................................................................
Demo on Optional Parameter & 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, "Ramesh", salary: 50000, job: "Manager");
emp.SetEmployee(101, "Kumar", 1000);
emp.ShowEmployee();
}
}
}
0 comments:
Post a Comment