Classes: Class Is A Data Type That Object. Blueprint. It Defines The Data and Behavior Class
Classes: Class Is A Data Type That Object. Blueprint. It Defines The Data and Behavior Class
Classes
Overview
In object-oriented programming, a class is a data type that
defines a set of variables and methods for a declared object.
class example
{
//variables, methods, etc.
}
(a class lets you define a custom data type)
Objects
Just as a built-in data type is used to declare multiple
variables, a class can be used to declare multiple objects.
Value types
C# has two ways of storing data: by reference and by value.
For example:
Hence p1 represents
the heap address.
class Person
{
int age;
string name;
public void SayHi(){
Console.WriteLine(”Hi”);
}
}
The code declares a class named Person, which has age and name
fields as well as a SayHi method that displays a greeting to the
screen.
Class usage
Key operator: new
class Dog
{
public string name;
public int age;
}
static void Main(string[] args)
{
Dog bob = new Dog();
bob.name = ”Bobby”
bob.age = 3;
Console.WriteLine(bob.age);
}
class BankAccount
{
private double balance = 0;
public void Deposit(double n)
{
balance += n;
}
public double GetBalance()
{
return balance;
}
}
Constructors
A constructor is a special member method of a class that is
executed whenever a new object of that class is created.
class BankAccount
{
private double balance = 0;
public void Deposit(double n)
{
balance += n;
}
public BankAccount()
{
Console.WriteLine(”We are accessing your Account”);
}
}
(When an object from this class will be created, mentioned phrase will be
output)
class Person
{
private int age;
private string name;
public Person(string nm)
{
name = nm;
}
public string getName()
{
return name;
}
}
(This code will assess the input into Person() value to the name member
inside of the class)
Properties
As we have seen it is a good practice to encapsulate members of
a class and provide limited access to them only through public
methods.
class Person
{
private string name; // The field
public string Name // property
{
get{return name;}
set{name = value;}
}
}
(The set accessor is used to assign a value to the name variable; get is
used to return its value)
Usage of properties
class Program
{
static void Main(string[] args)
{
Card card1 = new Card();
Console.WriteLine(card1.AccountNum);
}
}
class Card
{
private string accountNum = "0011592048120";
public string AccountNum{get {return accountNum;}}
}
From the example above we can see that using private string in
conjunction with a public string can be useful if we assign
properties to the public string.
Auto-implemented properties
When you do not need any custom logic, C# provides a fast and
effective mechanism for declaring private members through their
properties.
For example, to create a private member that can only be accessed
through the Name property's get and set accessors, use the
following syntax:
public string Name {get;set;}
We can rewrite the code from our previous example using an auto-
property:
class Person
{
public string Name{get;set;}
}
static void Main(string[] args)
{
Person p = new Person();
p.Name = ”Bob”;
Console.WriteLine(p.Name);
}