KEMBAR78
C# Basics and OOP | PDF | Method (Computer Programming) | Parameter (Computer Programming)
0% found this document useful (0 votes)
5 views34 pages

C# Basics and OOP

Uploaded by

Shimaa Gamal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views34 pages

C# Basics and OOP

Uploaded by

Shimaa Gamal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 34

C# BASICS

CONT.,
METHODS

Eng: Shimaa Gamal


AGENDA
C# Math
C# Strings
C# Break, Continue
C# Methods
C# Methods Parameters
C# Methods overloading
Introduction to C# Class

2/7/20XX 2
C# MATH
The C# Math class has many methods that allows you to
perform mathematical tasks on numbers.
Math.Max(x,y): can be used to find the highest value
of x and y
Math.Min(x,y): can be used to find the lowest value of
of x and y
Math.Sqrt(x): returns the square root of x
Math.Abs(x): returns the absolute (positive) value of x
Math.Round(): rounds a number to the nearest whole
number

3
C# STRINGS Strings are used for storing text
A string variable contains a
collection of characters
surrounded by double
quotes
string greeting = "Hello";
A string variable can contain
many words, if you want
string greeting2 = "Nice to
meet you!";

4
C# STRINGS
String Length: A string in C# is actually an object,
which contain properties and methods that
can perform certain operations on strings. For
example, the length of a string can be found
with the Length property

5
C# STRINGS
Other Methods
There are many string methods available,
for example ToUpper() and ToLower(), which
returns a copy of the string converted to
uppercase or lowercase:

6
C# STRING
CONCATENATION
The + operator can be used between
strings to combine them. This is called
concatenation

7
C# STRING
CONCATENATION
You can also use
the string.Concat() m
ethod to concatenate
two strings

8
ADDING NUMBERS
AND STRINGS If you add two numbers, the result will be a number
int x = 10;
int y = 20;
int z = x + y;
Console.WriteLine(z); // 10+20 = 30
If you add two strings, the result will be a string concatenation
string x = "10";
string y = "20";
string z = x + y;
Console.WriteLine(z); // 1020

9
C# STRING
INTERPOLATION Another option of string
concatenation, is string
interpolation, which
substitutes values of variables
into placeholders in a string.

10
C# ACCESS You can access the characters in a
STRINGS string by referring to its index
number inside square brackets []
string myString = "Hello";
Console.WriteLine(myString[0]); // H
You can also find the index position of a
specific character in a string, by using
the IndexOf() method
string myString = "Hello";
Console.WriteLine(myString.IndexOf("e")); // 1
11
C# ACCESS Another useful method is Substring(), which
extracts the characters from a string, starting
STRINGS from the specified character position/index, and
returns a new string. This method is often used
together with IndexOf() to get the specific
character position

12
C# SPECIAL
Because strings must be written within quotes, C# will
CHARACTERS misunderstand this string, and generate an error
The solution to avoid this problem, is to use the backslash
escape character.
The backslash (\) escape character turns special characters
into string characters
The sequence \" inserts a double quote in
a string
The sequence \' inserts a single quote in
a string
The sequence \\ inserts a single
backslash in a string

13
C# SORT ARRAYS There are many array methods available,
for example Sort(), which sorts an array
alphabetically or in an ascending order

14
C# SORT ARRAYS
System.Linq Namespace
Other useful array methods, such as Min, Max, and Sum,
can be found in the System.Linq namespace:

15
C# METHODS
A method is a block of code which only runs when it is called.
You can pass data, known as parameters, into a method.
Methods are used to perform certain actions, and they are also known
as functions.
Why use methods? To reuse code: define the code once, and use it
many times.

16
Create a Method
C# A method is defined with the name of the method,
METHODS followed by parentheses (). C# provides some pre-
defined methods, which you already are familiar with,
such as Main(), but you can also create your own
methods to perform certain actions
class Program
{
Static void MyMethod()
{
// code to be executed
}
}
17
static means that the method belongs to
the Program class and not an object of
C# METHODS the Program class.
void means that this method does not
have a return value.
Call a Method:
To call (execute) a method, write the method's name
followed by two parentheses () and a semicolon;
In the following example, MyMethod() is
used to print a text (the action), when
it is called
A method can be called one or multiple times:

18
CALL A METHOD

19
C# METHOD
PARAMETERS Information can be passed to methods as parameter. Parameters act as
variables inside the method.
They are specified after the method name, inside the parentheses. You
can add as many parameters as you want, just separate them with a
comma.
The following example has a method that takes
a string called fname as parameter. When the method is
called, we pass along a first name, which is used inside the
method to print the full name

20
C# METHOD PARAMETERS

21
C# METHOD •When a parameter is passed to the method, it is
PARAMETERS above:
called an argument. So, from the example
fname is a parameter,
while Liam, Jenny and Anja are arguments.
•Multiple Parameters: You can have as many parameters as you like, just
separate them with commas

22
You can also use a default parameter
value, by using the equals sign (=)
C# DEFAULT If we call the method without an argument, it uses the
default value (“Egypt"):
PARAMETER VALUE A parameter with a default value, is often known as an
"optional parameter"
static void MyMethod(string country = "Egypt")
{
Console.WriteLine(country);
}
static void Main(string[] args)
{
MyMethod(“England"); // England
MyMethod("India"); // India
MyMethod(); // Egypt
MyMethod("USA"); // USA 23
C# RETURN •If you want the method to return a value, you can
use a primitive data type (such as int or double)
VALUES instead of void, and use the return keyword inside
the method
static int MyMethod(int x)
{
return 5 + x;
}

static void Main(string[] args)


{
Console.WriteLine(MyMethod(3)); //8
}

24
C# NAMED It is also possible to send arguments with
ARGUMENTS the key: value syntax
That way, the order of the arguments does not matter

25
With method overloading, multiple methods can have the same name
C# METHOD with different parameters
OVERLOADING Consider the following example, which have two methods that add
numbers of different type:

26
C# METHOD Instead of defining two methods that should do the same thing, it is
OVERLOADIN better to overload one

G In the example below, we overload the PlusMethod method to


work for both int and doubl e

27
C# OOP OOP stands for Object-Oriented Programming
Procedural programming is about writing procedures or methods that perform
operations on the data, while object-oriented programming is about creating
objects that contain both data and methods.
Object-oriented programming has several advantages over procedural programming:
• OOP is faster and easier to execute
• OOP provides a clear structure for the programs
• OOP helps to keep the C# code DRY "Don't Repeat Yourself", and makes the code
easier to maintain, modify and debug
• OOP makes it possible to create full reusable applications with less code and
shorter development time

28
What are Classes and Objects?
C# OOP Classes and objects are the two main aspects of object-oriented
programming.
Look at the following illustration to see the difference between
class and objects:

29
C# OOP

So, a class is a template for objects, and an object is an instance of


a class.
When the individual objects are created, they inherit all the
variables and methods from the class.

30
ASSIGNMENT 4 :
NAME LIST
Create an array of 5 names (strings),
then print all names using foreach loop

31
ASSIGNMENT 5 :
STUDENT MARKS
•Ask the user to enter 5 student
marks (store in an array)
•Then calculate and print:
•Total
•Average
•Highest mark

32
ASSIGNMENT 6 :
COUNT EVEN AND
ODD
•Ask the user to enter 10 numbers
•Store them in an array
•Then count how many are even and how many are odd,
and print the result

33
THANK YOU

34

You might also like