Object Oriented Programming II (OOP) II
Chapter :2 C# Classes and Objects
1.1 Copyright © 2017 Pearson Education Ltd.
First C# Program
Here, you will learn to create a simple console
application in C# and understand the basic
building blocks of a console application.
C# can be used in a window-based, web-
based, or console application. To start with,
we will create a console application to work
with C#.
Open Visual Studio (2012 ) installed on your
Create a New Project in Visual Studio 2012
local machine. Click on File -> New Project...
from the top menu, as shown below.
1.2 Copyright © 2017 Pearson Education Ltd.
First C# Program
From the New Project popup, shown below, select Visual C# in the left side panel
and select the Console App in the right-side panel.
In the name section, give any appropriate
project name, a location where you want
to create all the project files, and the name
of the project solution.
Click OK to create the console project.
1.3 Copyright © 2017 Pearson Education Ltd.
First C# Program
Program.cs will be created as default a C# file in Visual Studio where you can write your
C# code in Program class, as shown below. (The .cs is a file extension for C# file.)
C# Console Program
1.4 Copyright © 2017 Pearson Education Ltd.
First C# Program
Every console application starts from the Main() method of the Program class. The
following example displays "Hello World!!" on the console.
Application ReadKey(): This method obtains only the next key from the input stream. It is also used to hold the screen until the user presses a key.
1.5 Copyright © 2017 Pearson Education Ltd.
First C# Program
The following image illustrates the important parts of the above example.
1.6 Copyright © 2017 Pearson Education Ltd.
Let's understand the above C# structure.
1. Every .NET application takes the reference of the necessary .NET framework namespaces that it is
planning to use with the using keyword, e.g., using System.Text.
2. Declare the namespace for the current class using the namespace keyword, e.g., namespace
CSharpTutorials.FirstProgram
3. We then declared a class using the class keyword: class Program
4. The Main() is a method of Program class is the entry point of the console application.
5. String is a data type.
6. A message is a variable that holds the value of a specified data type.
7. "Hello World!!" is the value of the message variable.
Note: Every line is
8. The Console.WriteLine() orastatement in C#which
static method, mustisend with
used a semicolon
to display a text(;).
on the console.
1.7 Copyright © 2017 Pearson Education Ltd.
First C# Program
Compile and Run C# Program
To see the output of the above C# program, we have to compile it and run it by
pressing Ctrl + F5 or clicking the Run button or by clicking the "Debug" menu and
clicking "Start Without Debugging". You will see the following output in the console:
1.8 Copyright © 2017 Pearson Education Ltd.
Classes and Objects
You learned from the previous chapter that C# is an object-oriented programming
language.
Everything in C# is associated with classes and objects, along with its attributes and
methods. For example: in real life, a car is an object. The car has attributes, such as
weight and color, and methods, such as drive and brake.
A Class is like an object constructor, or a "blueprint" for creating objects.
1.9 Copyright © 2017 Pearson Education Ltd.
Create a Class
To create a class, use the class keyword:
Create a class named "Car" with a variable color:
class Car
{
string color =
"red";
}
When a variable is declared directly in a class, it is often referred to as a field (or
attribute).
1.10 Copyright © 2017 Pearson Education Ltd.
Create an Object
An object is created from a class. We have already created the class named Car, so
now we can use this to create objects. Example
class Car
To create an object of Car, specify the {
string color = "red";
class name, followed by the object
static void Main(string[]
name, and use the keyword new: args)
{
Car myObj = new Car();
Note that we use the dot syntax (.) to
access variables/fields inside a class Console.WriteLine(myObj.color);
(myObj.color). }
}
1.11 Copyright © 2017 Pearson Education Ltd.
Multiple Objects
You can create multiple objects of one class:
Example: class Car
{
Create two objects of Car: string color = "red";
static void Main(string[] args)
{
Car myObj1 = new Car();
Car myObj2 = new Car();
Console.WriteLine(myObj1.color);
Console.WriteLine(myObj2.color);
}
}
1.12 Copyright © 2017 Pearson Education Ltd.
Using Multiple Classes
You can also create an object of a class and access it in another class. This is often
used for better organization of classes (one class has all the fields and methods,
while the other class holds the Main() method (code to be executed)).
prog2.cs
prog.cs
1.13 Copyright © 2017 Pearson Education Ltd.
C# Class Members
Fields and methods inside classes are often referred to as "Class Members":
Example : Create a Car class with three class members: two fields and one method.
// The class
class MyClass
{
// Class members
string color = "red"; // field
int maxSpeed = 200; // field
public void fullThrottle() // method
{
Console.WriteLine("The car is going as fast as it can!");
}
}
1.14 Copyright © 2017 Pearson Education Ltd.
Fields
In the previous slide, you learned that variables inside a class are called fields, and
that you can access them by creating an object of the class, and by using the
dot syntax (.).
class Car
{
The following example will create an
string color = "red";
object of the Car class, with the name int maxSpeed = 200;
myObj. Then we print the value of the static void Main(string[] args)
fields color and maxSpeed: {
Car myObj = new Car();
Console.WriteLine(myObj.color);
Console.WriteLine(myObj.maxSpeed);
}
}
1.15 Copyright © 2017 Pearson Education Ltd.
class Car
{
Fields Example 2: string model;
string color;
int year;
static void Main(string[] args)
You can also leave the fields blank, and {
modify them when creating the object: Car Ford = new Car();
Ford.model = "Mustang";
Ford.color = "red";
Ford.year = 1969;
Car Opel = new Car();
Opel.model = "Astra";
Opel.color = "white";
Opel.year = 2005;
Console.WriteLine(Ford.model);
Console.WriteLine(Opel.model);
}
1.16 Copyright © 2017 Pearson Education Ltd.
}
Object Methods
You learned from the C# Methods chapter that methods are used to perform
certain actions.
Methods normally belong to a class, and they define how an object of a class
behaves.
Just like with fields, you can access methods with the dot syntax. However, note
that the method must be public. And remember that we use the name of the
method followed by two parentheses () and a semicolon ; to call (execute) the
method:
1.17 Copyright © 2017 Pearson Education Ltd.
Object Methods
Example
class Car
{
string color; // field
int maxSpeed; // field
public void fullThrottle() // method
{
Console.WriteLine("The car is going as fast as it can!");
}
static void Main(string[] args)
{
Car myObj = new Car();
myObj.fullThrottle(); // Call the method
}
1.18
} Copyright © 2017 Pearson Education Ltd.
Use Multiple Classes
we can use multiple classes for better
organization (one for fields and methods, and
another one for execution).
1.19 Copyright © 2017 Pearson Education Ltd.
OOP II
CHAPTER TWO
END!!!
1.20 Copyright © 2017 Pearson Education Ltd.