(LECTURE 2 (B) ) Introduction To Classes, Objects MethodsPart1
(LECTURE 2 (B) ) Introduction To Classes, Objects MethodsPart1
Methods
C# Object
1. class Student
2. {
3. int id;//field or data member
4. String name;//field or data member
5. }
C# Object and Class Example
namespace ConsoleApplication36
{
class student
{
int id;
string name;
}
}
}
C# Class Example 2: Having Main() in another class
namespace ConsoleApplication36
{
class Employee
{
int id;
string name;
int salary;
int tax_am;
public void insertvalue(int i, string n, int sal)
{
id = i;
name = n;
salary = sal;
}
public void tax()
{
tax_am = salary / 100 * 25;
}
public void display()
{
Console.WriteLine("ID : "+id+ " Name : "+name+" Salary : "+salary+" Tax : "+tax_am);
}
}
class TestEMPLOYEE
{
• Public
The public modifier sets no restriction on the access of members.
• Protected
Access limited to the derived class or class definition.
• Internal
The Internal access modifier access within the program that has its
declaration.
• Protected internal
It has both the access specifiers provided by protected and internal
access modifiers.
• Private
Limited only inside the class in which it is declared. The members
specified as private cannot be accessed outside the class.
Access Modifiers in C#
C# Properties (Get and Set)
Properties and Encapsulation
namespace ConsoleApplication41
{
class Employee
{
private int salary = 2000;
public int salaryaccess //read & write property
{
get { return salary; }
set { salary = value; }
}
}
class Program
{
static void Main(string[] args)
{
int bonus;
Employee emp = new Employee();
emp.salaryaccess = 1800;
bonus = emp.salaryaccess / 100 * 30;
Console.WriteLine(bonus);
Console.ReadKey();
}
}}
Types Of Properties
• Read and Write Properties: When property
contains both get and set methods.