(Open in Desktop/Laptop/Big Screen for better result.)
Theory:
Syntax and Definition of Class, object, constructor, destructor, get and set methods.
• Class: A class definition starts with the keyword class followed by class name; and the class body enclosed by pair of curly braces.
• Object : objects can be created by using the new keyword by the same name of the class that the object will be based on.
Syntax:
Syntax:
class_name obj = new class_name();
• Constructor: The constructor is a special type of method in c# programming language that is called or invoked automatically when an object of the given class is created. There are basic two type of constructor default and parameterized.
Syntax:
public class user
{
// constructor public user()
{
// your custom code or body }
}
}
• Destructor: Destructors are just the opposite of constructors. It is a special method of the class that is invoked when a class object goes out of scope. Similar to a constructor, a destructor name is also exactly the same as that of the class name but with a prefix of “~” (tilde).
Syntax:
Syntax:
class Example
{
// Rest of the class
// members and methods.
// Destructor
~Example()
{
// your code or body
}
}
• Accessors: The block of “set” and “get” is known as “Accessor”. It is very essential to restrict the accessibility of property.
• get() : It specifies that the value of a field can access publicly. It return a single value and it specifies the read-only property.
• set() : It will specify the assignment of a value to a private field in a property. It return a single value and it specifies the write-only property.
Syntax for get() and set():
<access_modifier> <return_type> <property_name>
{
Get{ // body}
Set{ // body}
}
Console application code:
using System;
namespace Console_Employee
{
class Employee
{
int Emp_id;
String name;
String Email_id;
long contact_no;
String destination;
String dept;
public Employee()
{
Emp_id = 0;
name = "NULL";
Email_id = "employeexyz@gmail.com";
contact_no = 00000;
destination = "mumbai";
dept = "XYZ";
}
public Employee(int a, String n, String e, long c, String de, String dt)
{
Emp_id = a;
name = n;
Email_id = e;
contact_no = c;
destination = de;
dept = dt;
}
public void display()
{
Console.WriteLine(Emp_id+ " \t " +name+ " \t "
+Email_id+ "\t" +contact_no+ "\t" +destination+ "\t"+dept);
}
}
class Program
{
static void Main(string[] args)
{
int id; String n; String e; long c; String de; String dt;
/*taking input or employee info from user */
Console.WriteLine("Enter employee id :");
string val = Console.ReadLine();
id = Convert.ToInt32(val);
Console.WriteLine("Enter employee name:");
n = Console.ReadLine();
Console.WriteLine("Enter employee Email_id:");
e = Console.ReadLine();
Console.WriteLine("Enter employee contact NO:");
string val2 = Console.ReadLine();
c = Convert.ToInt64(val2);
Console.WriteLine("Enter employee destination:");
de = Console.ReadLine();
Console.WriteLine("Enter employee department:");
dt = Console.ReadLine();
Employee e1 = new Employee(id,n,e,c,de,dt);
e1.display();
// By parameterized constructor
Employee e2 =
new Employee
(1,"Maroof","maroof49@gmail.com",9320789456,"jogeswari","Computer");
e2.display();
// By default constructor Employee e3 = new Employee();
e3.display();
Console.ReadKey();
}
}
}
Observations/ Results:
Output :