SlideShare a Scribd company logo
Renas R. Rekany Object-Oriented-Programming
1
Programming Language with
C_Sharp
Object-Oriented-Programming
Renas R. Rekany
Renas R. Rekany Object-Oriented-Programming
2
Build_in and User_define C# Methods
1- Method (= Function)
A method is a group of statements that together perform a task. It combines related code together and
makes program easier. Every C# program has at least one class with a method named Main.
2- Method Types:
A. Built in: the methods that the language provides. Such as Math class provide the following
methods:
Abs (x) exp (x) tan (x)
Cos (x) pow (x,y) log (x)
Sin (x) max (x,y) Sqrt (x)
Log10, max, min, round, sqrt.
Example:
Math.abs(x);
Textbox1.text=Convert.ToString(math.abs(-10));
Math.cos(x);
B. User defines: the methods that the user defines and call.
Example:
int sum (int x , int y)
{int s=x+y;
Return (s);}
3- Defining Methods in C# and calling techniques:
When you define a method, you basically declare the elements of its structure. The syntax for
defining a method in C# is as follows:
Following are the various elements of a method:
 Access Specifier: This determines the visibility of a variable or a method from
another class.
 Return type: A method may return a value. The return type is the data type of the
value the method returns. If the method is not returning any values, then the return
type is void.
< Access Specifier> < Return Type> < Method Name> (Parameter list)
{
Body
}
Renas R. Rekany Object-Oriented-Programming
3
 Method name: Method name is a unique identifier and it is case sensitive. It
cannot be same as any other identifier declared in the class.
 Parameter list: Enclosed between parentheses, the parameters are used to pass
and receive data from a method. The parameter list refers to the type, order, and
number of the parameters of a method. Parameters are optional; that is, a method
may contain no parameters.
 Method body: This contains the set of instructions needed to complete the
required activity.
Example:
public void add()
{
Body
}
In the preceding example, the public is an access specifier, void is a return data type
that return nothing and add() is a method name. There is no parameter define in
add() method.
If the function returns interger value, then you can define function as follow:
public int add()
{
Body
}
If the function is returning string value, then you can define function as follow:
public string printname()
{
Body
}
You must remember, whenever use return data type with method, must return value
using return keyword from body. If you don’t want to return any value, then you can
use void data type.
Renas R. Rekany Object-Oriented-Programming
4
Ex:
Class program
{
public int sqr(int a)
{
return (a * a);
}
private void button1_Click(object sender, EventArgs e)
{
int b;
b =Convert.ToInt32( textBox1.Text);
textBox2.Text = Convert.ToString(sqr(b));
}
}
4- Passing arguments:
pass by value Pass by reference
static int sqr(int x)
{
return ( x * x);
} ----------
Output
- Before call x is 5
- After call x is 5
static int sqr2(ref int x)
{
return x * x;
}
----------
Output
- Before call x is 5
- After call x is 25
Renas R. Rekany Object-Oriented-Programming
5
5- Method overload:
A. C# enable several methods of same name to be defined in same class as long as
these method have different sets of parameters (number of parameter , type of
parameter , order of parameter ) this is called method overloaded .
B. Method overloading commonly is used to create several methods with same name
that perform similar tasks, but on different data types.
Ex:
Static int squire (int x)
{
return (x*x);
}
Static double squire (double x)
{
return (x*x);
}
static void main ( )
{
int y = 2;
double z=12.1;
textBox1.Text = Convert.ToString(squire(y));
textBox1.Text = Convert.ToString(squire(z));
}
Output
- The squire of int 2 is 4.
- The squire of double 2.1 is 4.41.
Note: This is called overload method.

More Related Content

What's hot

Function overloading
Function overloadingFunction overloading
Function overloading
Ashish Kelwa
 
Classes function overloading
Classes function overloadingClasses function overloading
Classes function overloadingankush_kumar
 
Templates
TemplatesTemplates
Templates
Nilesh Dalvi
 
Introduction to C++
Introduction to C++Introduction to C++
Introduction to C++
Pranali Chaudhari
 
Template C++ OOP
Template C++ OOPTemplate C++ OOP
Template C++ OOP
Muhammad khan
 
parameter passing in c#
parameter passing in c#parameter passing in c#
parameter passing in c#
khush_boo31
 
Functions in c language
Functions in c language Functions in c language
Functions in c language
tanmaymodi4
 
Functional Programming With Scala
Functional Programming With ScalaFunctional Programming With Scala
Functional Programming With Scala
Knoldus Inc.
 
Python programming –part 3
Python programming –part 3Python programming –part 3
Python programming –part 3
Megha V
 
Scala functions
Scala functionsScala functions
Scala functions
Knoldus Inc.
 
C++ Template
C++ TemplateC++ Template
C++ Template
Saket Pathak
 
Templates
TemplatesTemplates
Let Us Learn Lambda Using C# 3.0
Let Us Learn Lambda Using C# 3.0Let Us Learn Lambda Using C# 3.0
Let Us Learn Lambda Using C# 3.0
Sheik Uduman Ali
 
Intake 37 linq1
Intake 37 linq1Intake 37 linq1
Intake 37 linq1
Mahmoud Ouf
 
14method in c#
14method in c#14method in c#
14method in c#
Sireesh K
 
Templates2
Templates2Templates2
Templates2
zindadili
 
Chapter 6.6
Chapter 6.6Chapter 6.6
Chapter 6.6sotlsoc
 
Functions
FunctionsFunctions
Functions
Pragnavi Erva
 
Templates presentation
Templates presentationTemplates presentation
Templates presentation
malaybpramanik
 

What's hot (20)

Function overloading
Function overloadingFunction overloading
Function overloading
 
Classes function overloading
Classes function overloadingClasses function overloading
Classes function overloading
 
Templates
TemplatesTemplates
Templates
 
Csharp generics
Csharp genericsCsharp generics
Csharp generics
 
Introduction to C++
Introduction to C++Introduction to C++
Introduction to C++
 
Template C++ OOP
Template C++ OOPTemplate C++ OOP
Template C++ OOP
 
parameter passing in c#
parameter passing in c#parameter passing in c#
parameter passing in c#
 
Functions in c language
Functions in c language Functions in c language
Functions in c language
 
Functional Programming With Scala
Functional Programming With ScalaFunctional Programming With Scala
Functional Programming With Scala
 
Python programming –part 3
Python programming –part 3Python programming –part 3
Python programming –part 3
 
Scala functions
Scala functionsScala functions
Scala functions
 
C++ Template
C++ TemplateC++ Template
C++ Template
 
Templates
TemplatesTemplates
Templates
 
Let Us Learn Lambda Using C# 3.0
Let Us Learn Lambda Using C# 3.0Let Us Learn Lambda Using C# 3.0
Let Us Learn Lambda Using C# 3.0
 
Intake 37 linq1
Intake 37 linq1Intake 37 linq1
Intake 37 linq1
 
14method in c#
14method in c#14method in c#
14method in c#
 
Templates2
Templates2Templates2
Templates2
 
Chapter 6.6
Chapter 6.6Chapter 6.6
Chapter 6.6
 
Functions
FunctionsFunctions
Functions
 
Templates presentation
Templates presentationTemplates presentation
Templates presentation
 

Similar to C# p8

Class 10
Class 10Class 10
3 functions and class
3   functions and class3   functions and class
3 functions and classtrixiacruz
 
Class & Object - User Defined Method
Class & Object - User Defined MethodClass & Object - User Defined Method
Class & Object - User Defined Method
PRN USM
 
Visula C# Programming Lecture 6
Visula C# Programming Lecture 6Visula C# Programming Lecture 6
Visula C# Programming Lecture 6
Abou Bakr Ashraf
 
EContent_11_2023_04_09_11_30_38_Unit_3_Objects_and_Classespptx__2023_03_20_12...
EContent_11_2023_04_09_11_30_38_Unit_3_Objects_and_Classespptx__2023_03_20_12...EContent_11_2023_04_09_11_30_38_Unit_3_Objects_and_Classespptx__2023_03_20_12...
EContent_11_2023_04_09_11_30_38_Unit_3_Objects_and_Classespptx__2023_03_20_12...
vekariyakashyap
 
Intro to C# - part 2.pptx emerging technology
Intro to C# - part 2.pptx emerging technologyIntro to C# - part 2.pptx emerging technology
Intro to C# - part 2.pptx emerging technology
worldchannel
 
Lecture 5: Functional Programming
Lecture 5: Functional ProgrammingLecture 5: Functional Programming
Lecture 5: Functional ProgrammingEelco Visser
 
Advanced Web Technology ass.pdf
Advanced Web Technology ass.pdfAdvanced Web Technology ass.pdf
Advanced Web Technology ass.pdf
simenehanmut
 
Java class
Java classJava class
Java class
Arati Gadgil
 
functions.pptx
functions.pptxfunctions.pptx
functions.pptx
KavithaChekuri3
 
08 class and object
08   class and object08   class and object
08 class and objectdhrubo kayal
 
Linq and lambda
Linq and lambdaLinq and lambda
Linq and lambda
John Walsh
 
Lecture 2
Lecture 2Lecture 2
Lecture 2
Avinash Kapse
 
Getting Started - Console Program and Problem Solving
Getting Started - Console Program and Problem SolvingGetting Started - Console Program and Problem Solving
Getting Started - Console Program and Problem Solving
Hock Leng PUAH
 
SPF Getting Started - Console Program
SPF Getting Started - Console ProgramSPF Getting Started - Console Program
SPF Getting Started - Console Program
Hock Leng PUAH
 
Chapter 7 - Defining Your Own Classes - Part II
Chapter 7 - Defining Your Own Classes - Part IIChapter 7 - Defining Your Own Classes - Part II
Chapter 7 - Defining Your Own Classes - Part II
Eduardo Bergavera
 
Object and class
Object and classObject and class
Object and class
mohit tripathi
 

Similar to C# p8 (20)

Class 10
Class 10Class 10
Class 10
 
3 functions and class
3   functions and class3   functions and class
3 functions and class
 
Class & Object - User Defined Method
Class & Object - User Defined MethodClass & Object - User Defined Method
Class & Object - User Defined Method
 
Visula C# Programming Lecture 6
Visula C# Programming Lecture 6Visula C# Programming Lecture 6
Visula C# Programming Lecture 6
 
EContent_11_2023_04_09_11_30_38_Unit_3_Objects_and_Classespptx__2023_03_20_12...
EContent_11_2023_04_09_11_30_38_Unit_3_Objects_and_Classespptx__2023_03_20_12...EContent_11_2023_04_09_11_30_38_Unit_3_Objects_and_Classespptx__2023_03_20_12...
EContent_11_2023_04_09_11_30_38_Unit_3_Objects_and_Classespptx__2023_03_20_12...
 
Java execise
Java execiseJava execise
Java execise
 
Intro to C# - part 2.pptx emerging technology
Intro to C# - part 2.pptx emerging technologyIntro to C# - part 2.pptx emerging technology
Intro to C# - part 2.pptx emerging technology
 
Lecture 5: Functional Programming
Lecture 5: Functional ProgrammingLecture 5: Functional Programming
Lecture 5: Functional Programming
 
Advanced Web Technology ass.pdf
Advanced Web Technology ass.pdfAdvanced Web Technology ass.pdf
Advanced Web Technology ass.pdf
 
Java class
Java classJava class
Java class
 
functions.pptx
functions.pptxfunctions.pptx
functions.pptx
 
08 class and object
08   class and object08   class and object
08 class and object
 
Linq and lambda
Linq and lambdaLinq and lambda
Linq and lambda
 
Ds lab handouts
Ds lab handoutsDs lab handouts
Ds lab handouts
 
Bc0037
Bc0037Bc0037
Bc0037
 
Lecture 2
Lecture 2Lecture 2
Lecture 2
 
Getting Started - Console Program and Problem Solving
Getting Started - Console Program and Problem SolvingGetting Started - Console Program and Problem Solving
Getting Started - Console Program and Problem Solving
 
SPF Getting Started - Console Program
SPF Getting Started - Console ProgramSPF Getting Started - Console Program
SPF Getting Started - Console Program
 
Chapter 7 - Defining Your Own Classes - Part II
Chapter 7 - Defining Your Own Classes - Part IIChapter 7 - Defining Your Own Classes - Part II
Chapter 7 - Defining Your Own Classes - Part II
 
Object and class
Object and classObject and class
Object and class
 

More from Renas Rekany

decision making
decision makingdecision making
decision making
Renas Rekany
 
Artificial Neural Network
Artificial Neural NetworkArtificial Neural Network
Artificial Neural Network
Renas Rekany
 
AI heuristic search
AI heuristic searchAI heuristic search
AI heuristic search
Renas Rekany
 
AI local search
AI local searchAI local search
AI local search
Renas Rekany
 
AI simple search strategies
AI simple search strategiesAI simple search strategies
AI simple search strategies
Renas Rekany
 
C# p9
C# p9C# p9
C# p7
C# p7C# p7
C# p6
C# p6C# p6
C# p5
C# p5C# p5
C# p4
C# p4C# p4
C# p3
C# p3C# p3
C# p2
C# p2C# p2
C# p1
C# p1C# p1
C# with Renas
C# with RenasC# with Renas
C# with Renas
Renas Rekany
 
Object oriented programming inheritance
Object oriented programming inheritanceObject oriented programming inheritance
Object oriented programming inheritance
Renas Rekany
 
Object oriented programming
Object oriented programmingObject oriented programming
Object oriented programming
Renas Rekany
 
Renas Rajab Asaad
Renas Rajab Asaad Renas Rajab Asaad
Renas Rajab Asaad
Renas Rekany
 
Renas Rajab Asaad
Renas Rajab AsaadRenas Rajab Asaad
Renas Rajab Asaad
Renas Rekany
 
Renas Rajab Asaad
Renas Rajab Asaad Renas Rajab Asaad
Renas Rajab Asaad
Renas Rekany
 
Renas Rajab Asaad
Renas Rajab Asaad Renas Rajab Asaad
Renas Rajab Asaad
Renas Rekany
 

More from Renas Rekany (20)

decision making
decision makingdecision making
decision making
 
Artificial Neural Network
Artificial Neural NetworkArtificial Neural Network
Artificial Neural Network
 
AI heuristic search
AI heuristic searchAI heuristic search
AI heuristic search
 
AI local search
AI local searchAI local search
AI local search
 
AI simple search strategies
AI simple search strategiesAI simple search strategies
AI simple search strategies
 
C# p9
C# p9C# p9
C# p9
 
C# p7
C# p7C# p7
C# p7
 
C# p6
C# p6C# p6
C# p6
 
C# p5
C# p5C# p5
C# p5
 
C# p4
C# p4C# p4
C# p4
 
C# p3
C# p3C# p3
C# p3
 
C# p2
C# p2C# p2
C# p2
 
C# p1
C# p1C# p1
C# p1
 
C# with Renas
C# with RenasC# with Renas
C# with Renas
 
Object oriented programming inheritance
Object oriented programming inheritanceObject oriented programming inheritance
Object oriented programming inheritance
 
Object oriented programming
Object oriented programmingObject oriented programming
Object oriented programming
 
Renas Rajab Asaad
Renas Rajab Asaad Renas Rajab Asaad
Renas Rajab Asaad
 
Renas Rajab Asaad
Renas Rajab AsaadRenas Rajab Asaad
Renas Rajab Asaad
 
Renas Rajab Asaad
Renas Rajab Asaad Renas Rajab Asaad
Renas Rajab Asaad
 
Renas Rajab Asaad
Renas Rajab Asaad Renas Rajab Asaad
Renas Rajab Asaad
 

Recently uploaded

Instructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptxInstructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptx
Jheel Barad
 
Polish students' mobility in the Czech Republic
Polish students' mobility in the Czech RepublicPolish students' mobility in the Czech Republic
Polish students' mobility in the Czech Republic
Anna Sz.
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
siemaillard
 
Digital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and ResearchDigital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and Research
Vikramjit Singh
 
Home assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfHome assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdf
Tamralipta Mahavidyalaya
 
The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official Publication
Delapenabediema
 
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
MysoreMuleSoftMeetup
 
CACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdfCACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdf
camakaiclarkmusic
 
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdfAdversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Po-Chuan Chen
 
A Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in EducationA Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in Education
Peter Windle
 
Language Across the Curriculm LAC B.Ed.
Language Across the  Curriculm LAC B.Ed.Language Across the  Curriculm LAC B.Ed.
Language Across the Curriculm LAC B.Ed.
Atul Kumar Singh
 
Operation Blue Star - Saka Neela Tara
Operation Blue Star   -  Saka Neela TaraOperation Blue Star   -  Saka Neela Tara
Operation Blue Star - Saka Neela Tara
Balvir Singh
 
special B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdfspecial B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdf
Special education needs
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
Celine George
 
Palestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptxPalestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptx
RaedMohamed3
 
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
Nguyen Thanh Tu Collection
 
The French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free downloadThe French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free download
Vivekanand Anglo Vedic Academy
 
1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx
JosvitaDsouza2
 
Chapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptxChapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptx
Mohd Adib Abd Muin, Senior Lecturer at Universiti Utara Malaysia
 
678020731-Sumas-y-Restas-Para-Colorear.pdf
678020731-Sumas-y-Restas-Para-Colorear.pdf678020731-Sumas-y-Restas-Para-Colorear.pdf
678020731-Sumas-y-Restas-Para-Colorear.pdf
CarlosHernanMontoyab2
 

Recently uploaded (20)

Instructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptxInstructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptx
 
Polish students' mobility in the Czech Republic
Polish students' mobility in the Czech RepublicPolish students' mobility in the Czech Republic
Polish students' mobility in the Czech Republic
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
 
Digital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and ResearchDigital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and Research
 
Home assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfHome assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdf
 
The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official Publication
 
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
 
CACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdfCACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdf
 
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdfAdversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
 
A Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in EducationA Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in Education
 
Language Across the Curriculm LAC B.Ed.
Language Across the  Curriculm LAC B.Ed.Language Across the  Curriculm LAC B.Ed.
Language Across the Curriculm LAC B.Ed.
 
Operation Blue Star - Saka Neela Tara
Operation Blue Star   -  Saka Neela TaraOperation Blue Star   -  Saka Neela Tara
Operation Blue Star - Saka Neela Tara
 
special B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdfspecial B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdf
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
 
Palestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptxPalestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptx
 
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
 
The French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free downloadThe French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free download
 
1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx
 
Chapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptxChapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptx
 
678020731-Sumas-y-Restas-Para-Colorear.pdf
678020731-Sumas-y-Restas-Para-Colorear.pdf678020731-Sumas-y-Restas-Para-Colorear.pdf
678020731-Sumas-y-Restas-Para-Colorear.pdf
 

C# p8

  • 1. Renas R. Rekany Object-Oriented-Programming 1 Programming Language with C_Sharp Object-Oriented-Programming Renas R. Rekany
  • 2. Renas R. Rekany Object-Oriented-Programming 2 Build_in and User_define C# Methods 1- Method (= Function) A method is a group of statements that together perform a task. It combines related code together and makes program easier. Every C# program has at least one class with a method named Main. 2- Method Types: A. Built in: the methods that the language provides. Such as Math class provide the following methods: Abs (x) exp (x) tan (x) Cos (x) pow (x,y) log (x) Sin (x) max (x,y) Sqrt (x) Log10, max, min, round, sqrt. Example: Math.abs(x); Textbox1.text=Convert.ToString(math.abs(-10)); Math.cos(x); B. User defines: the methods that the user defines and call. Example: int sum (int x , int y) {int s=x+y; Return (s);} 3- Defining Methods in C# and calling techniques: When you define a method, you basically declare the elements of its structure. The syntax for defining a method in C# is as follows: Following are the various elements of a method:  Access Specifier: This determines the visibility of a variable or a method from another class.  Return type: A method may return a value. The return type is the data type of the value the method returns. If the method is not returning any values, then the return type is void. < Access Specifier> < Return Type> < Method Name> (Parameter list) { Body }
  • 3. Renas R. Rekany Object-Oriented-Programming 3  Method name: Method name is a unique identifier and it is case sensitive. It cannot be same as any other identifier declared in the class.  Parameter list: Enclosed between parentheses, the parameters are used to pass and receive data from a method. The parameter list refers to the type, order, and number of the parameters of a method. Parameters are optional; that is, a method may contain no parameters.  Method body: This contains the set of instructions needed to complete the required activity. Example: public void add() { Body } In the preceding example, the public is an access specifier, void is a return data type that return nothing and add() is a method name. There is no parameter define in add() method. If the function returns interger value, then you can define function as follow: public int add() { Body } If the function is returning string value, then you can define function as follow: public string printname() { Body } You must remember, whenever use return data type with method, must return value using return keyword from body. If you don’t want to return any value, then you can use void data type.
  • 4. Renas R. Rekany Object-Oriented-Programming 4 Ex: Class program { public int sqr(int a) { return (a * a); } private void button1_Click(object sender, EventArgs e) { int b; b =Convert.ToInt32( textBox1.Text); textBox2.Text = Convert.ToString(sqr(b)); } } 4- Passing arguments: pass by value Pass by reference static int sqr(int x) { return ( x * x); } ---------- Output - Before call x is 5 - After call x is 5 static int sqr2(ref int x) { return x * x; } ---------- Output - Before call x is 5 - After call x is 25
  • 5. Renas R. Rekany Object-Oriented-Programming 5 5- Method overload: A. C# enable several methods of same name to be defined in same class as long as these method have different sets of parameters (number of parameter , type of parameter , order of parameter ) this is called method overloaded . B. Method overloading commonly is used to create several methods with same name that perform similar tasks, but on different data types. Ex: Static int squire (int x) { return (x*x); } Static double squire (double x) { return (x*x); } static void main ( ) { int y = 2; double z=12.1; textBox1.Text = Convert.ToString(squire(y)); textBox1.Text = Convert.ToString(squire(z)); } Output - The squire of int 2 is 4. - The squire of double 2.1 is 4.41. Note: This is called overload method.