5. static void Main(string[] args)
{
int empNo;
string name;
float basicSalary, hra, da, pf, grossSalary;
...
...
}
Real world Digital world
empNo Name basicSalary
hra da pf grossSalary
www.dotnetvideotutorial.com
6. Real world Digital world
struct Employee
{
int empNo;
string name;
float basicSalary, hra, da, pf, grossSalary;
}
static void Main()
{
Employee emp;
}
empNo name basic
Salary
hra da pf gross
Salary
emp
www.dotnetvideotutorial.com
7. Real world Digital world
class Employee
{
public int empNo;
public string name;
public float basicSalary, hra, da, pf, grossSalary;
public void CalculateSalary()
{
hra = 15 * basicSalary / 100;
da = 10 * basicSalary / 100;
pf = 5 * basicSalary / 100;
grossSalary = basicSalary + hra + da - pf;
}
}
class Program
{
static void Main()
{
Employee emp = new Employee();
}
}
empNo name basic
Salary
hra da pf gross
Salary
emp
www.dotnetvideotutorial.com
10. Class
A class is a construct that enables you to create your own custom types by
grouping together variables of other types, methods and events.
A class is like a blueprint. It defines the data and behavior of a type.
Client code can use it by creating objects or instances which are assigned to a
variable.
www.dotnetvideotutorial.com
11. Object
An object is a block of memory that has been allocated and configured
according to the blueprint (typically class).
A program may create many objects of the same class.
Objects can be stored in either a named variable or in an array or collection.
Client code is the code that uses these variables to call the methods and access
the public properties of the object.
In an object-oriented language such as C#, a typical program consists of multiple
objects interacting dynamically.
www.dotnetvideotutorial.com
13. Constructor
Constructors are special methods with the same name as of class
Typically used to initialize the data members of the new object.
Invoked by the new operator immediately after memory is allocated for the new
object.
Class can have multiple constructors that take different arguments (overloading)
Constructor that takes no parameters is called a default constructor.
If no constructor is provided, C# creates one by default.
www.dotnetvideotutorial.com
17. Properties
A property is a member that provides a flexible mechanism to read, write, or
compute the value of a private field.
Properties can be used as if they are public data members, but they are actually
special methods called accessors.
This enables data to be accessed easily and still helps promote the safety and
flexibility of methods.
A get property accessor is used to return the property value, and a set accessor
is used to assign a new value. These accessors can have different access levels.
Properties that do not implement a set accessor are read only.
www.dotnetvideotutorial.com
20. Automatic Properties
Improves readability
Reduces coding efforts
Are useful when validation and calculations are not required in properties.
www.dotnetvideotutorial.com
public int EmpNo { get; set; }
private int _EmpNo;
public int EmpNo
{
get
{ return this._EmpNo; }
set
{ this._EmpNo = value; }
}
Compiler converts into
23. Destructor
Destructor is special method with same name as of class name prefixed by tilde (~)
A class can only have one destructor.
Destructors cannot be inherited or overloaded.
Destructors cannot be called. They are invoked automatically by garbage collector just
before destroying the object (if provided).
A destructor does not take modifiers or have parameters.
Destructors are also called when the program exits.
Can be used to write clean up code such as releasing unmanaged resources (if used in
class)
www.dotnetvideotutorial.com