OOPS
OOPS
OOPS
C# Programming(CSU1531)
Section A
C# Language Basics and Advanced Features
Classes and Objects
Constructors
The car has attributes, such as weight and color, and methods,
such as drive and brake.
Object is an entity that has state and behavior. Here, state means data and
behavior means functionality.
// Outputs "Mustang"
Constructor Example
// Outputs "Mustang"
Access Modifiers
They are used to set the access level/visibility for classes, fields,
methods and properties.
Modifier Description
class Program
class Car
{
{
static void Main(string[] args)
private string model = "Mustang";
{
Car myObj = new Car();
static void Main(string[] args)
Console.WriteLine(myObj.model);
{
}
Car myObj = new Car();
}
Console.WriteLine(myObj.model);
}
}
By default, all members of a class are private if you don't specify an access
modifier
Example
class Car
{
string model; // private
string year; // private
}
Public Modifier
If you declare a field with a public access modifier, it is accessible for all classes:
Example
class Car
{
public string model = "Mustang";
}
class Program
{
static void Main(string[] args)
{
Car myObj = new Car();
Console.WriteLine(myObj.model);
}
}
To control the visibility of class members (the security level of each individual
class and class member).
By declaring the variables as private (to restrict their direct access from
outside the class)
By defining one pair of public setter and getter methods or properties to
access private variables from outside the class.
//Public Setter Method
Example //This method is used to store the data in the balance variable
public double GetBalance() //You can access the private variable via public setter and getter methods
{ bank.SetBalance(500);
} }