4. What we will see…
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
6. A Random Review
Dr.AtifShahzad
6
DataTypes DefaultValue MinimumValue MaximumValue
sbyte 0 -128 127
byte 0 0 255
short 0 -32768 32767
ushort 0 0 65535
int 0 -2147483648 2147483647
uint 0u 0 4294967295
long 0L -9223372036854775808 9223372036854775807
ulong 0u 0 18446744073709551615
float 0.0f ±1.5×10-45 ±3.4×1038
double 0.0d ±5.0×10-324 ±1.7×10308
decimal 0.0m ±1.0×10-28 ±7.9×1028
bool false Two possible values: true and false
char 'u0000' 'u0000' 'uffff'
object null - -
string null - -
7. A Random Review: Integer types
Dr.AtifShahzad
7
// Declare some variables
byte centuries = 20;
ushort years = 2000;
uint days = 730480;
ulong hours = 17531520;
// Print the result on the console
Console.WriteLine(centuries + " centuries are " + years +
" years, or " + days + " days, or " + hours + " hours.
");
// Console output:
// 20 centuries are 2000 years, or 730480 days, or 17531520
// hours.
ulong maxIntValue = UInt64.MaxValue;
Console.WriteLine(maxIntValue); // 18446744073709551615
8. A Random Review: Real types
Dr.AtifShahzad
8
// Declare some variables
float floatPI = 3.141592653589793238f;
double doublePI = 3.141592653589793238;
// Print the results on the console
Console.WriteLine("Float PI is: " + floatPI);
Console.WriteLine("Double PI is: " + doublePI);
// Console output:
// Float PI is: 3.141593
// Double PI is: 3.14159265358979
10. A quick Question
int age = 8;
if ( !(age >= 16) ) {
Console.Write("Your age is less than 16");
}
// Outputs "Your age is less than 16"
9/27/2018
Dr.AtifShahzad
10
12. Classes
Classes act as templatesfrom which
an instance of an object is created at run time.
9/27/2018
Dr.AtifShahzad
12
13. Classes
Classes act as templates
from which
an instance of an object is created
at run time.
Classes define the properties of the object
and the methods used to control the
object's behavior.
9/27/2018
Dr.AtifShahzad
13
14. Class vs Object: Example 1
Dr.AtifShahzad
14
Drawing of House
House
15. Class vs Object: Example 2
Dr.AtifShahzad
15
Engineering Drawing of a car
Car
16. Class vs Object: Example 3
Dr.AtifShahzad
16
BankAccountclass
Bank Account of a customer
17. Class vs Object: Example 4
Dr.AtifShahzad
17
Student class
A particular student
18. Class vs Object:You examples?
Dr.AtifShahzad
18
Class:
Object:
Attributes:
Methods:
19. Class vs Object:
Dr.AtifShahzad
19
Class:
Object:
Attributes or States or Properties
these are the characteristics of the object which define it in a
way and describe it in general or in a specific moment
Methods or Behaviors
these are the specific distinctive actions, which can be done by
the object.
Methods:
23. Adding a Method to the Class
Dr.AtifShahzad
23
namespace CarApp
{
class Car
{
public void Start()
{ // A method
Console.WriteLine("Car has been
started");
}
} // end class Car
}//end namespace CarApp
24. Instantiating an object of a class
Dr.AtifShahzad
24
In the Main method of the Program:
Car MyCar = new Car();
25. Calling a Method of an object
Dr.AtifShahzad
25
Access or dot (.) operator
MyCar.Start();
29. Files of the project inWindows
explorer
9/27/2018
Dr.AtifShahzad
29
30. Example2:
Dr.AtifShahzad
30
public class Cat
{
// Field name
private string name;
// Field color
private string color;
public string Name
{
// Getter of the property "Name"
get
{
return this.name;
}
// Setter of the property "Name"
set
{
this.name = value;
}
}
public string Color
{
// Getter of the property "Color"
get
{
return this.color;
}
// Setter of the property "Color"
set
{
this.color = value;
}
}
// Default constructor
public Cat()
{
this.name = "Unnamed";
this.color = "gray";
}
// Constructor with parameters
public Cat(string name, string color)
{
this.name = name;
this.color = color;
}
// Method SayMiau
public void SayMiau()
{
Console.WriteLine("Cat {0} said: Miauuuuuu!", name);
}
}
public class Cat
{
// Field name
private string name;
// Field color
private string color;
public string Name
{
// Getter of the property "Name"
get
{
return this.name;
}
// Setter of the property "Name"
set
{
this.name = value;
}
}
public string Color
{
// Getter of the property "Color"
get
{
return this.color;
}
// Setter of the property "Color"
set
{
this.color = value;
}
}
// Default constructor
public Cat()
{
this.name = "Unnamed";
this.color = "gray";
}
// Constructor with parameters
public Cat(string name, string color)
{
this.name = name;
this.color = color;
}
// Method SayMiau
public void SayMiau()
{
Console.WriteLine("Cat {0} said: Miauuuuuu!
", name);
}
}
34. Look at this example
int age = 42;
string msg;
if(age >= 18)
msg = "Welcome";
else
msg = "Sorry";
Console.WriteLine(msg);
9/27/2018
Dr.AtifShahzad
34
38. What is aVisual Studio Project?
When you create an
app, website, plug-in,
etc. inVisual Studio,
you start with a project.
9/27/2018
Dr.AtifShahzad
38
39. What is aVisual Studio Project?
When you create an app, website, plug-in, etc.
inVisual Studio, you start with a project.
In a logical sense, a project contains all the
source code files, icons, images, data files, etc.
that are compiled into an executable, library, or
website.
A project also contains compiler settings and
other configuration files that might be needed
by various services or components that your
program communicates with.9/27/2018
Dr.AtifShahzad
39
40. What is aVisual Studio Project?
A project is defined in an XML file with an
extension such as .vbproj, .csproj, or
.vcxproj.
This file contains a virtual folder hierarchy,
and paths to all the items in the project. It
also contains the build settings.
9/27/2018
Dr.AtifShahzad
40
41. What is aVisual Studio Project?
In Visual Studio, the project file is used by
Solution Explorer to display the project
contents and settings.
When you compile your project, the
MSBuild engine consumes the project file
to create the executable.You can also
customize projects to produce other kinds
of output.
9/27/2018
Dr.AtifShahzad
41
42. What is aVisual Studio Solution?
A project is contained within a solution. A
solution contains one or more related projects,
along with build information, Visual Studio
window settings, and any miscellaneous files
that aren't associated with a particular project.
A solution is described by a text file (extension
.sln) with its own unique format; it is generally
not intended to be edited by hand.
9/27/2018
Dr.AtifShahzad
42
43. What is aVisual Studio Solution?
A solution has an associated .suo file that stores
settings, preferences and configuration
information for each user that has worked on
the project.
9/27/2018
Dr.AtifShahzad
43