4. Recap
9/27/2018
Dr.AtifShahzad
A Random Review
A quick Question
Classes
• Class vs Object:
Examples
• Class vs Object:
Adding a Class to a C#
project
Declaring a Class
Adding a Method to the
Class
Instantiating an object
of a class
Calling a Method of an
object
Complete Example—
class Car
Solution Explorer
Files of the project in
Windows explorer
Example2:
Some built-in classes
A BonusTopic
• Look this example
• Conditional Operator
What is aVisual Studio
Project?
What is aVisual Studio
Solution?
Project & Solution
5. What we will see…
9/27/2018
Dr.AtifShahzad
Class Example Fields Encapsulation
Access
modifiers
Properties Value keyword
Auto-
implemented
properties
Constructors
10. Class Example
class Person //declares a class named Person
{
int age;
string name; // age and name fields
public void SayHi() // a SayHi method
{
Console.WriteLine("Hi");
}
}
9/27/2018
Dr.AtifShahzad
10
11. Fields
class Person //declares a class named Person
{
int age;
string name; // age and name fields
public void SayHi() // a SayHi method
{
Console.WriteLine("Hi");
}
}
9/27/2018
Dr.AtifShahzad
11
Filed is also called InstanceVariable
12. Fields
class Person //declares a class named Person
{
int age;
string name; // age and name fields
public void SayHi() // a SayHi method
{
Console.WriteLine("Hi");
}
}
9/27/2018
Dr.AtifShahzad
12
13. Instantiate an object
static void Main(string[] args)
{
Person p1 = new Person();
p1.SayHi();
}
//Outputs "Hi"
9/27/2018
Dr.AtifShahzad
13
The new operator
• instantiates an object &
• returns a reference to its location
14. Use Classes
static void Main(string[] args)
{
Person p1 = new Person();
p1.age=19;
p1.SayHi();
}
9/27/2018
Dr.AtifShahzad
14
you can use the dot operator to
make an assignment when valid.
21. Why Encapsulation?
Dr.AtifShahzad
21
Control the way data is accessed or
modified.
Code is more flexible and easy to change
with new requirements.
Change one part of code without affecting
other parts of code.
29. Properties
Dr.AtifShahzad
29
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 actually include special
methods called accessors.
The accessor of
a property contains the
executable statements that help
in getting (reading or
computing) or setting (writing)
a corresponding field.
Accessor declarations can
include a get accessor,
a set accessor, or both.
30. set & get
Dr.AtifShahzad
30
The set accessor is
used to assign a
value to the name
variable;
get is used to return
its value.
31. Because…
Dr.AtifShahzad
31
It is a good practice to
encapsulate members of a
class and provide access to
them only
through public methods.
32. Example
Dr.AtifShahzad
32
class Person
{
private int age=0;
public int Age
{
get
{
return age;
}// end get
set
{
if (value > 0)
age = value;
} // end set
} // endAge
} // end class Person
You can have any custom
logic
with get and set accessors.
The name of the property can be anyt
hing you want, but coding convention
s dictate properties have the same na
me as the private field with a
capital letter.
33. value keyword
Dr.AtifShahzad
33
value is a special keyword,
which represents the value
we assign to a property using
the set accessor.
class Person
{
private int age=0;
public int Age
{
get
{
return age;
}// end get
set
{
if (value > 0)
age = value;
} // end set
} // endAge
} // end class Person
34. Question
Dr.AtifShahzad
34
class A
{
private int x=8;
public int X
{
_______{
return x__x;
}
}
}
Fill in the blanks to
create a read-only
property X.
The return value of
the accessor should
be the square of x.
35. Question: Solved
Dr.AtifShahzad
35
class A
{
private int x=8;
public int X
{
get {
return x*x;
}
}
}
Fill in the blanks to
create a read-only
property X.
The return value of
the accessor should
be the square of x.
36. 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.
public string Name { get; set; }
9/27/2018
Dr.AtifShahzad
36
37. Auto-Implemented Properties
As you can see, you do not need to declare
the private field name separately - it is created
by property automatically.
Name is called an auto-implemented property.
Also called auto-properties, they allow for easy and
short declaration of private members.
public string Name { get; set; }
9/27/2018
Dr.AtifShahzad
37
38. Auto-Properties: Example
class Person
{
public string Name { get; set; }
}
static void Main(string[] args)
{
Person p = new Person();
p.Name = “Ali";
Console.WriteLine(p.Name);
}
// Outputs “Ali"
9/27/2018
Dr.AtifShahzad
38
40. Auto-Properties: Question Solved
class Person
{
public int Age {get; set; }
}
Fill in the blanks to create an auto-
property named Age of type int.
9/27/2018
Dr.AtifShahzad
40
42. Constructors
A class constructor is a
special member method of
a class that is executed
whenever a new object of
that class is created.
9/27/2018
Dr.AtifShahzad
42
44. Constructors: Example
//Person.cs
class Person
{
private int age;
public Person()
{
Console.WriteLine("Hi th
ere");
}
}
//Program.cs
static void Main(string[] args)
{
Person p = new Person();
}
// Outputs "Hi there"
9/27/2018
Dr.AtifShahzad
44
upon the creation of an object of typ Person,
the constructor is automatically called.
45. Constructors
Constructors can be very useful for setting initial
values for certain member variables.
A default constructor has no parameters.
However, when needed, parameters can be added
to a constructor.
9/27/2018
Dr.AtifShahzad
45
46. Constructors: Example
//Person.cs
class Person
{
private int age;
private string name;
public Person(string nm)
{
name = nm;
}
public string getName()
{
return name;
}
}
//Program.cs
static void Main(string[] args)
{
Person p = new Person(“Reda");
Console.WriteLine(p.getName());
}
//Outputs “Reda"
9/27/2018
Dr.AtifShahzad
46
Constructors can be overloaded like any method
by using different numbers of parameters.