SlideShare uma empresa Scribd logo
1 de 22
Bhushan Mulmule
bhushan.mulmule@dotnetvideotutorial.com
www.dotnetvideotutorial.com
For video visit
www.dotnetvideotutorial.com
Methods
Passing
value types
By Value
By
Reference
Passing
reference
types
By Value
By
Reference
Agenda
Features
Variable
arguments
Named
arguments
Optional
parameters
Methods
 A method is a code block containing a series of statements.
 Methods are declared within a class or struct by specifying
 Access Modifier
 Other Modifiers
 Return value
 Name of the method
 Method parameters.
public static int add(int no1, int no2)
{
}
www.dotnetvideotutorial.com
Possible Combinations
• Method without parameter and no
return value
void fun1()
• Method with parameter and no return
value
void fun2(string name)
• Method with parameter and return
value
int fun3(int no1, int no2)
• Method without parameter but with
return value
string fun4()
Method without parameter and No return value
static void Main(string[] args)
{
greet();
Console.ReadKey();
}
static void greet()
{
Console.WriteLine("Hello");
}
www.dotnetvideotutorial.com
static void Main(string[] args)
{
greet("Mr. Anderson");
Console.ReadKey();
}
static void greet(string name)
{
Console.WriteLine("Hello " + name);
}
Method with Parameters but No Return value
Mr. Anderson
name
www.dotnetvideotutorial.com
Method With Parameters and Return value
static void Main(string[] args)
{
int no1 = 10, no2 = 20;
int result = add(no1, no2);
Console.WriteLine("Result = {0}", result);
Console.ReadKey();
}
static int add(int no1, int no2)
{
int result = no1 + no2;
return result;
}
no1
10
no2
20
no1
10
no2
20
result
30
result
30
www.dotnetvideotutorial.com
Compact Format
static void Main(string[] args)
{
Console.WriteLine(add(25,50));
}
static int add(int no1, int no2)
{
return no1 + no2;
}
no1
25
no2
50
www.dotnetvideotutorial.com
Fn without Parameter but with return value
static void Main(string[] args)
{
Console.WriteLine(GetTime());
Console.ReadKey();
}
static string GetTime()
{
string time = DateTime.Now.ToShortTimeString();
return time;
}
www.dotnetvideotutorial.com
 ref and out keyword used to pass arguments by reference
 ref: to pass references of initialized variables
 out: to pass references of uninitialized variables
 value type as well as reference type can be passed by
reference
Passing References
www.dotnetvideotutorial.com
static void Main(string[] args)
{
int no1 = 25, no2 = 50;
Console.WriteLine("Before swaping: no1 = {0}, no2 = {1}", no1, no2);
Swap(ref no1, ref no2);
Console.WriteLine("After swaping: no1 = {0}, no2 = {1}", no1, no2);
Console.ReadKey();
}
static void Swap(ref int no1,ref int no2)
{
int temp = no1;
no1 = no2;
no2 = temp;
}
ref keyword
25
no1
5420
50
no2
5424
5420
no1
5424
no2
25
temp
50 25
www.dotnetvideotutorial.com
out keyword
static void Main(string[] args)
{
float radius, area, circumference;
Console.WriteLine("Enter Radius of Circle: ");
radius = Convert.ToInt32(Console.ReadLine());
Calculate(radius, out area, out circumference);
Console.WriteLine("Area = " + area);
Console.WriteLine("Circumference = " + circumference);
}
static void Calculate(float radius, out float area, out float circumference)
{
const float pi = 3.14f;
area = 2 * pi * radius;
circumference = pi * radius * radius;
}
radius
5420
5
area
5424
circum
5428
78.5
radius
5
area
5424
circum
5428
31.4
www.dotnetvideotutorial.com
Passing Reference Types
 Reference type can be passed by value as well by reference
 If passed by value - content of object reference is passed
 If passed by reference - reference of object reference is
passed
5028
2000 5028
obj
www.dotnetvideotutorial.com
Passing string by value
static void Main(string[] args)
{
string str = "Milkyway";
Console.WriteLine("str = " + str);
ChangeString(str);
Console.WriteLine("nstr = " + str);
}
static void ChangeString(string str)
{
Console.WriteLine("nstr = " + str);
str = "Andromeda";
Console.WriteLine("nstr = " + str);
}
5028 Milkyway
Andromeda
5028
5028
6252
6252
str
str
www.dotnetvideotutorial.com
Passing string by reference
static void Main(string[] args)
{
string str = "Milkyway";
Console.WriteLine("str = " + str);
ChangeString(ref str);
Console.WriteLine("nstr = " + str);
Console.ReadKey();
}
static void ChangeString(ref string str)
{
Console.WriteLine("nstr = " + str);
str = "Andromeda";
Console.WriteLine("nstr = " + str);
}
5028 Milkyway
Andromeda
5028
6252
6252
2020
2020
str
str
www.dotnetvideotutorial.com
Passing Array by value
static void Main(string[] args)
{
int[] marks = new int[] { 10, 20, 30 };
Console.WriteLine(marks[0]);
ChangeMarks(marks);
Console.WriteLine(marks[0]);
}
static void ChangeMarks(int[] marks)
{
Console.WriteLine(marks[0]);
marks[0] = 70;
Console.WriteLine(marks[0]);
}
5028
5028
10 20 30
5028
marks
marks
70
www.dotnetvideotutorial.com
Flexibility
Variable
Arguments
Named
Arguments
Optional
Parameters
Passing variable number of arguments
static void Main(string[] args)
{
Console.Write("Humans : ");
printNames("Neo", "Trinity", "Morphious", "Seroph");
Console.Write("Programs : ");
printNames("Smith", "Oracle");
}
static void printNames(params string[] names)
{
foreach (string n in names)
{
Console.Write(n + " ");
}
Console.WriteLine();
}
www.dotnetvideotutorial.com
Named Arguments
static void Main(string[] args)
{
int area1 = CalculateArea(width:10,height:15);
Console.WriteLine(area1);
int area2 = CalculateArea(height: 8, width: 4);
Console.WriteLine(area2);
}
static int CalculateArea(int width, int height)
{
return width * height;
}
www.dotnetvideotutorial.com
Optional Parameters
static void Main(string[] args)
{
Score(30, 70, 60, 80);
Score(30, 70, 60);
Score(30, 70);
}
static void Score(int m1,int m2,int m3 = 50,int m4 = 30)
{
Console.WriteLine(m1 + m2 + m3 + m4);
}
www.dotnetvideotutorial.com
Bhushan Mulmule
bhushan.mulmule@dotnetvideotutorial.com
www.dotnetvideotutorial.com

Mais conteúdo relacionado

Mais procurados

02. Primitive Data Types and Variables
02. Primitive Data Types and Variables02. Primitive Data Types and Variables
02. Primitive Data Types and VariablesIntro C# Book
 
Syntax Comparison of Golang with C and Java - Mindbowser
Syntax Comparison of Golang with C and Java - MindbowserSyntax Comparison of Golang with C and Java - Mindbowser
Syntax Comparison of Golang with C and Java - MindbowserMindbowser Inc
 
Templates in C++
Templates in C++Templates in C++
Templates in C++Tech_MX
 
Python programming- Part IV(Functions)
Python programming- Part IV(Functions)Python programming- Part IV(Functions)
Python programming- Part IV(Functions)Megha V
 
Inverted Index Based Multi-Keyword Public-key Searchable Encryption with Stro...
Inverted Index Based Multi-Keyword Public-key Searchable Encryption with Stro...Inverted Index Based Multi-Keyword Public-key Searchable Encryption with Stro...
Inverted Index Based Multi-Keyword Public-key Searchable Encryption with Stro...Mateus S. H. Cruz
 
Python programming –part 3
Python programming –part 3Python programming –part 3
Python programming –part 3Megha V
 
40+ examples of user defined methods in java with explanation
40+ examples of user defined methods in java with explanation40+ examples of user defined methods in java with explanation
40+ examples of user defined methods in java with explanationHarish Gyanani
 
CSEG1001Unit 3 Arrays and Strings
CSEG1001Unit 3 Arrays and StringsCSEG1001Unit 3 Arrays and Strings
CSEG1001Unit 3 Arrays and StringsDhiviya Rose
 
Dynamic Objects,Pointer to function,Array & Pointer,Character String Processing
Dynamic Objects,Pointer to function,Array & Pointer,Character String ProcessingDynamic Objects,Pointer to function,Array & Pointer,Character String Processing
Dynamic Objects,Pointer to function,Array & Pointer,Character String ProcessingMeghaj Mallick
 
Static-talk
Static-talkStatic-talk
Static-talkgiunti
 
Chapter 1 Basic Programming (Python Programming Lecture)
Chapter 1 Basic Programming (Python Programming Lecture)Chapter 1 Basic Programming (Python Programming Lecture)
Chapter 1 Basic Programming (Python Programming Lecture)IoT Code Lab
 
Stacks queues lists
Stacks queues listsStacks queues lists
Stacks queues listsHarry Potter
 

Mais procurados (20)

Overview of MONOMI
Overview of MONOMIOverview of MONOMI
Overview of MONOMI
 
02. Primitive Data Types and Variables
02. Primitive Data Types and Variables02. Primitive Data Types and Variables
02. Primitive Data Types and Variables
 
Syntax Comparison of Golang with C and Java - Mindbowser
Syntax Comparison of Golang with C and Java - MindbowserSyntax Comparison of Golang with C and Java - Mindbowser
Syntax Comparison of Golang with C and Java - Mindbowser
 
Templates in c++
Templates in c++Templates in c++
Templates in c++
 
Templates in C++
Templates in C++Templates in C++
Templates in C++
 
Python programming- Part IV(Functions)
Python programming- Part IV(Functions)Python programming- Part IV(Functions)
Python programming- Part IV(Functions)
 
Computer Programming- Lecture 5
Computer Programming- Lecture 5 Computer Programming- Lecture 5
Computer Programming- Lecture 5
 
Advanced C - Part 2
Advanced C - Part 2Advanced C - Part 2
Advanced C - Part 2
 
Inverted Index Based Multi-Keyword Public-key Searchable Encryption with Stro...
Inverted Index Based Multi-Keyword Public-key Searchable Encryption with Stro...Inverted Index Based Multi-Keyword Public-key Searchable Encryption with Stro...
Inverted Index Based Multi-Keyword Public-key Searchable Encryption with Stro...
 
Python programming –part 3
Python programming –part 3Python programming –part 3
Python programming –part 3
 
Algorithmic Notations
Algorithmic NotationsAlgorithmic Notations
Algorithmic Notations
 
40+ examples of user defined methods in java with explanation
40+ examples of user defined methods in java with explanation40+ examples of user defined methods in java with explanation
40+ examples of user defined methods in java with explanation
 
CSEG1001Unit 3 Arrays and Strings
CSEG1001Unit 3 Arrays and StringsCSEG1001Unit 3 Arrays and Strings
CSEG1001Unit 3 Arrays and Strings
 
Dynamic Objects,Pointer to function,Array & Pointer,Character String Processing
Dynamic Objects,Pointer to function,Array & Pointer,Character String ProcessingDynamic Objects,Pointer to function,Array & Pointer,Character String Processing
Dynamic Objects,Pointer to function,Array & Pointer,Character String Processing
 
Chapter 2 Method in Java OOP
Chapter 2   Method in Java OOPChapter 2   Method in Java OOP
Chapter 2 Method in Java OOP
 
String functions in C
String functions in CString functions in C
String functions in C
 
Static-talk
Static-talkStatic-talk
Static-talk
 
Unit4 Slides
Unit4 SlidesUnit4 Slides
Unit4 Slides
 
Chapter 1 Basic Programming (Python Programming Lecture)
Chapter 1 Basic Programming (Python Programming Lecture)Chapter 1 Basic Programming (Python Programming Lecture)
Chapter 1 Basic Programming (Python Programming Lecture)
 
Stacks queues lists
Stacks queues listsStacks queues lists
Stacks queues lists
 

Destaque

L21 io streams
L21 io streamsL21 io streams
L21 io streamsteach4uin
 
Java rmi example program with code
Java rmi example program with codeJava rmi example program with code
Java rmi example program with codekamal kotecha
 
Database Design Process
Database Design ProcessDatabase Design Process
Database Design Processmussawir20
 
Value Engineering And Value Analysis
Value Engineering And Value AnalysisValue Engineering And Value Analysis
Value Engineering And Value Analysisthombremahesh
 

Destaque (7)

L21 io streams
L21 io streamsL21 io streams
L21 io streams
 
Sql
SqlSql
Sql
 
Java rmi example program with code
Java rmi example program with codeJava rmi example program with code
Java rmi example program with code
 
Database design
Database designDatabase design
Database design
 
Database Design Process
Database Design ProcessDatabase Design Process
Database Design Process
 
Java SE 8 best practices
Java SE 8 best practicesJava SE 8 best practices
Java SE 8 best practices
 
Value Engineering And Value Analysis
Value Engineering And Value AnalysisValue Engineering And Value Analysis
Value Engineering And Value Analysis
 

Semelhante a Methods in C# - Passing Parameters, Variable Arguments, Named Arguments and Optional Parameters

ECSE 221 - Introduction to Computer Engineering - Tutorial 1 - Muhammad Ehtas...
ECSE 221 - Introduction to Computer Engineering - Tutorial 1 - Muhammad Ehtas...ECSE 221 - Introduction to Computer Engineering - Tutorial 1 - Muhammad Ehtas...
ECSE 221 - Introduction to Computer Engineering - Tutorial 1 - Muhammad Ehtas...Muhammad Ulhaque
 
Александр Зимин (Alexander Zimin) — Магия Swift
Александр Зимин (Alexander Zimin) — Магия SwiftАлександр Зимин (Alexander Zimin) — Магия Swift
Александр Зимин (Alexander Zimin) — Магия SwiftCocoaHeads
 
Cocoaheads Meetup / Alex Zimin / Swift magic
Cocoaheads Meetup / Alex Zimin / Swift magicCocoaheads Meetup / Alex Zimin / Swift magic
Cocoaheads Meetup / Alex Zimin / Swift magicBadoo Development
 
Java 5 New Feature
Java 5 New FeatureJava 5 New Feature
Java 5 New Featurexcoda
 
Visula C# Programming Lecture 6
Visula C# Programming Lecture 6Visula C# Programming Lecture 6
Visula C# Programming Lecture 6Abou Bakr Ashraf
 
Constructor in c++
Constructor in c++Constructor in c++
Constructor in c++Jay Patel
 
Java Foundations: Methods
Java Foundations: MethodsJava Foundations: Methods
Java Foundations: MethodsSvetlin Nakov
 
Module 4 : methods & parameters
Module 4 : methods & parametersModule 4 : methods & parameters
Module 4 : methods & parametersPrem Kumar Badri
 
Chapter 6.6
Chapter 6.6Chapter 6.6
Chapter 6.6sotlsoc
 
Arrays, Structures And Enums
Arrays, Structures And EnumsArrays, Structures And Enums
Arrays, Structures And EnumsBhushan Mulmule
 
C# 7.x What's new and what's coming with C# 8
C# 7.x What's new and what's coming with C# 8C# 7.x What's new and what's coming with C# 8
C# 7.x What's new and what's coming with C# 8Christian Nagel
 
45 aop-programming
45 aop-programming45 aop-programming
45 aop-programmingdaotuan85
 
Pointers, virtual function and polymorphism
Pointers, virtual function and polymorphismPointers, virtual function and polymorphism
Pointers, virtual function and polymorphismlalithambiga kamaraj
 
Class & Object - User Defined Method
Class & Object - User Defined MethodClass & Object - User Defined Method
Class & Object - User Defined MethodPRN USM
 
14 initialization & cleanup
14   initialization & cleanup14   initialization & cleanup
14 initialization & cleanupdhrubo kayal
 
Reflection in Go
Reflection in GoReflection in Go
Reflection in Gostrikr .
 
Ifi7184.DT lesson 2
Ifi7184.DT lesson 2Ifi7184.DT lesson 2
Ifi7184.DT lesson 2Sónia
 
Refactoring and code smells
Refactoring and code smellsRefactoring and code smells
Refactoring and code smellsPaul Nguyen
 

Semelhante a Methods in C# - Passing Parameters, Variable Arguments, Named Arguments and Optional Parameters (20)

ECSE 221 - Introduction to Computer Engineering - Tutorial 1 - Muhammad Ehtas...
ECSE 221 - Introduction to Computer Engineering - Tutorial 1 - Muhammad Ehtas...ECSE 221 - Introduction to Computer Engineering - Tutorial 1 - Muhammad Ehtas...
ECSE 221 - Introduction to Computer Engineering - Tutorial 1 - Muhammad Ehtas...
 
Александр Зимин (Alexander Zimin) — Магия Swift
Александр Зимин (Alexander Zimin) — Магия SwiftАлександр Зимин (Alexander Zimin) — Магия Swift
Александр Зимин (Alexander Zimin) — Магия Swift
 
Cocoaheads Meetup / Alex Zimin / Swift magic
Cocoaheads Meetup / Alex Zimin / Swift magicCocoaheads Meetup / Alex Zimin / Swift magic
Cocoaheads Meetup / Alex Zimin / Swift magic
 
Java 5 New Feature
Java 5 New FeatureJava 5 New Feature
Java 5 New Feature
 
Visula C# Programming Lecture 6
Visula C# Programming Lecture 6Visula C# Programming Lecture 6
Visula C# Programming Lecture 6
 
Constructor in c++
Constructor in c++Constructor in c++
Constructor in c++
 
Java Foundations: Methods
Java Foundations: MethodsJava Foundations: Methods
Java Foundations: Methods
 
Module 4 : methods & parameters
Module 4 : methods & parametersModule 4 : methods & parameters
Module 4 : methods & parameters
 
Chapter 6.6
Chapter 6.6Chapter 6.6
Chapter 6.6
 
Arrays, Structures And Enums
Arrays, Structures And EnumsArrays, Structures And Enums
Arrays, Structures And Enums
 
C# 7.x What's new and what's coming with C# 8
C# 7.x What's new and what's coming with C# 8C# 7.x What's new and what's coming with C# 8
C# 7.x What's new and what's coming with C# 8
 
Codejunk Ignitesd
Codejunk IgnitesdCodejunk Ignitesd
Codejunk Ignitesd
 
45 aop-programming
45 aop-programming45 aop-programming
45 aop-programming
 
Pointers, virtual function and polymorphism
Pointers, virtual function and polymorphismPointers, virtual function and polymorphism
Pointers, virtual function and polymorphism
 
Class & Object - User Defined Method
Class & Object - User Defined MethodClass & Object - User Defined Method
Class & Object - User Defined Method
 
14 initialization & cleanup
14   initialization & cleanup14   initialization & cleanup
14 initialization & cleanup
 
Reflection in Go
Reflection in GoReflection in Go
Reflection in Go
 
Pointers.pdf
Pointers.pdfPointers.pdf
Pointers.pdf
 
Ifi7184.DT lesson 2
Ifi7184.DT lesson 2Ifi7184.DT lesson 2
Ifi7184.DT lesson 2
 
Refactoring and code smells
Refactoring and code smellsRefactoring and code smells
Refactoring and code smells
 

Mais de Bhushan Mulmule

Implementing auto complete using JQuery
Implementing auto complete using JQueryImplementing auto complete using JQuery
Implementing auto complete using JQueryBhushan Mulmule
 
Windows Forms For Beginners Part 5
Windows Forms For Beginners Part 5Windows Forms For Beginners Part 5
Windows Forms For Beginners Part 5Bhushan Mulmule
 
Windows Forms For Beginners Part - 4
Windows Forms For Beginners Part - 4Windows Forms For Beginners Part - 4
Windows Forms For Beginners Part - 4Bhushan Mulmule
 
Windows Forms For Beginners Part - 3
Windows Forms For Beginners Part - 3Windows Forms For Beginners Part - 3
Windows Forms For Beginners Part - 3Bhushan Mulmule
 
Windows Forms For Beginners Part - 2
Windows Forms For Beginners Part - 2Windows Forms For Beginners Part - 2
Windows Forms For Beginners Part - 2Bhushan Mulmule
 
Windows Forms For Beginners Part - 1
Windows Forms For Beginners Part - 1Windows Forms For Beginners Part - 1
Windows Forms For Beginners Part - 1Bhushan Mulmule
 
Dependency injection for beginners
Dependency injection for beginnersDependency injection for beginners
Dependency injection for beginnersBhushan Mulmule
 
Understanding Interfaces
Understanding InterfacesUnderstanding Interfaces
Understanding InterfacesBhushan Mulmule
 
Getting started with C# Programming
Getting started with C# ProgrammingGetting started with C# Programming
Getting started with C# ProgrammingBhushan Mulmule
 
Overview of .Net Framework 4.5
Overview of .Net Framework 4.5Overview of .Net Framework 4.5
Overview of .Net Framework 4.5Bhushan Mulmule
 

Mais de Bhushan Mulmule (15)

Implementing auto complete using JQuery
Implementing auto complete using JQueryImplementing auto complete using JQuery
Implementing auto complete using JQuery
 
Windows Forms For Beginners Part 5
Windows Forms For Beginners Part 5Windows Forms For Beginners Part 5
Windows Forms For Beginners Part 5
 
Windows Forms For Beginners Part - 4
Windows Forms For Beginners Part - 4Windows Forms For Beginners Part - 4
Windows Forms For Beginners Part - 4
 
Windows Forms For Beginners Part - 3
Windows Forms For Beginners Part - 3Windows Forms For Beginners Part - 3
Windows Forms For Beginners Part - 3
 
Windows Forms For Beginners Part - 2
Windows Forms For Beginners Part - 2Windows Forms For Beginners Part - 2
Windows Forms For Beginners Part - 2
 
Windows Forms For Beginners Part - 1
Windows Forms For Beginners Part - 1Windows Forms For Beginners Part - 1
Windows Forms For Beginners Part - 1
 
NInject - DI Container
NInject - DI ContainerNInject - DI Container
NInject - DI Container
 
Dependency injection for beginners
Dependency injection for beginnersDependency injection for beginners
Dependency injection for beginners
 
Understanding Interfaces
Understanding InterfacesUnderstanding Interfaces
Understanding Interfaces
 
Polymorphism
PolymorphismPolymorphism
Polymorphism
 
Inheritance
InheritanceInheritance
Inheritance
 
Classes and objects
Classes and objectsClasses and objects
Classes and objects
 
Flow Control (C#)
Flow Control (C#)Flow Control (C#)
Flow Control (C#)
 
Getting started with C# Programming
Getting started with C# ProgrammingGetting started with C# Programming
Getting started with C# Programming
 
Overview of .Net Framework 4.5
Overview of .Net Framework 4.5Overview of .Net Framework 4.5
Overview of .Net Framework 4.5
 

Último

Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupFlorian Wilhelm
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii SoldatenkoFwdays
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Scott Keck-Warren
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationSlibray Presentation
 
Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Manik S Magar
 
Vertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsVertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsMiki Katsuragi
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 3652toLead Limited
 
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Wonjun Hwang
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsMemoori
 
Powerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time ClashPowerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time Clashcharlottematthew16
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr BaganFwdays
 
My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024The Digital Insurer
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsMark Billinghurst
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brandgvaughan
 
Search Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdfSearch Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdfRankYa
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):comworks
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024Lorenzo Miniero
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfAddepto
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsSergiu Bodiu
 

Último (20)

Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project Setup
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck Presentation
 
Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!
 
Vertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsVertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering Tips
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365
 
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial Buildings
 
Powerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time ClashPowerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time Clash
 
DMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special EditionDMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special Edition
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan
 
My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR Systems
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brand
 
Search Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdfSearch Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdf
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdf
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platforms
 

Methods in C# - Passing Parameters, Variable Arguments, Named Arguments and Optional Parameters

  • 3. Methods Passing value types By Value By Reference Passing reference types By Value By Reference Agenda Features Variable arguments Named arguments Optional parameters
  • 4. Methods  A method is a code block containing a series of statements.  Methods are declared within a class or struct by specifying  Access Modifier  Other Modifiers  Return value  Name of the method  Method parameters. public static int add(int no1, int no2) { } www.dotnetvideotutorial.com
  • 5. Possible Combinations • Method without parameter and no return value void fun1() • Method with parameter and no return value void fun2(string name) • Method with parameter and return value int fun3(int no1, int no2) • Method without parameter but with return value string fun4()
  • 6. Method without parameter and No return value static void Main(string[] args) { greet(); Console.ReadKey(); } static void greet() { Console.WriteLine("Hello"); } www.dotnetvideotutorial.com
  • 7. static void Main(string[] args) { greet("Mr. Anderson"); Console.ReadKey(); } static void greet(string name) { Console.WriteLine("Hello " + name); } Method with Parameters but No Return value Mr. Anderson name www.dotnetvideotutorial.com
  • 8. Method With Parameters and Return value static void Main(string[] args) { int no1 = 10, no2 = 20; int result = add(no1, no2); Console.WriteLine("Result = {0}", result); Console.ReadKey(); } static int add(int no1, int no2) { int result = no1 + no2; return result; } no1 10 no2 20 no1 10 no2 20 result 30 result 30 www.dotnetvideotutorial.com
  • 9. Compact Format static void Main(string[] args) { Console.WriteLine(add(25,50)); } static int add(int no1, int no2) { return no1 + no2; } no1 25 no2 50 www.dotnetvideotutorial.com
  • 10. Fn without Parameter but with return value static void Main(string[] args) { Console.WriteLine(GetTime()); Console.ReadKey(); } static string GetTime() { string time = DateTime.Now.ToShortTimeString(); return time; } www.dotnetvideotutorial.com
  • 11.  ref and out keyword used to pass arguments by reference  ref: to pass references of initialized variables  out: to pass references of uninitialized variables  value type as well as reference type can be passed by reference Passing References www.dotnetvideotutorial.com
  • 12. static void Main(string[] args) { int no1 = 25, no2 = 50; Console.WriteLine("Before swaping: no1 = {0}, no2 = {1}", no1, no2); Swap(ref no1, ref no2); Console.WriteLine("After swaping: no1 = {0}, no2 = {1}", no1, no2); Console.ReadKey(); } static void Swap(ref int no1,ref int no2) { int temp = no1; no1 = no2; no2 = temp; } ref keyword 25 no1 5420 50 no2 5424 5420 no1 5424 no2 25 temp 50 25 www.dotnetvideotutorial.com
  • 13. out keyword static void Main(string[] args) { float radius, area, circumference; Console.WriteLine("Enter Radius of Circle: "); radius = Convert.ToInt32(Console.ReadLine()); Calculate(radius, out area, out circumference); Console.WriteLine("Area = " + area); Console.WriteLine("Circumference = " + circumference); } static void Calculate(float radius, out float area, out float circumference) { const float pi = 3.14f; area = 2 * pi * radius; circumference = pi * radius * radius; } radius 5420 5 area 5424 circum 5428 78.5 radius 5 area 5424 circum 5428 31.4 www.dotnetvideotutorial.com
  • 14. Passing Reference Types  Reference type can be passed by value as well by reference  If passed by value - content of object reference is passed  If passed by reference - reference of object reference is passed 5028 2000 5028 obj www.dotnetvideotutorial.com
  • 15. Passing string by value static void Main(string[] args) { string str = "Milkyway"; Console.WriteLine("str = " + str); ChangeString(str); Console.WriteLine("nstr = " + str); } static void ChangeString(string str) { Console.WriteLine("nstr = " + str); str = "Andromeda"; Console.WriteLine("nstr = " + str); } 5028 Milkyway Andromeda 5028 5028 6252 6252 str str www.dotnetvideotutorial.com
  • 16. Passing string by reference static void Main(string[] args) { string str = "Milkyway"; Console.WriteLine("str = " + str); ChangeString(ref str); Console.WriteLine("nstr = " + str); Console.ReadKey(); } static void ChangeString(ref string str) { Console.WriteLine("nstr = " + str); str = "Andromeda"; Console.WriteLine("nstr = " + str); } 5028 Milkyway Andromeda 5028 6252 6252 2020 2020 str str www.dotnetvideotutorial.com
  • 17. Passing Array by value static void Main(string[] args) { int[] marks = new int[] { 10, 20, 30 }; Console.WriteLine(marks[0]); ChangeMarks(marks); Console.WriteLine(marks[0]); } static void ChangeMarks(int[] marks) { Console.WriteLine(marks[0]); marks[0] = 70; Console.WriteLine(marks[0]); } 5028 5028 10 20 30 5028 marks marks 70 www.dotnetvideotutorial.com
  • 19. Passing variable number of arguments static void Main(string[] args) { Console.Write("Humans : "); printNames("Neo", "Trinity", "Morphious", "Seroph"); Console.Write("Programs : "); printNames("Smith", "Oracle"); } static void printNames(params string[] names) { foreach (string n in names) { Console.Write(n + " "); } Console.WriteLine(); } www.dotnetvideotutorial.com
  • 20. Named Arguments static void Main(string[] args) { int area1 = CalculateArea(width:10,height:15); Console.WriteLine(area1); int area2 = CalculateArea(height: 8, width: 4); Console.WriteLine(area2); } static int CalculateArea(int width, int height) { return width * height; } www.dotnetvideotutorial.com
  • 21. Optional Parameters static void Main(string[] args) { Score(30, 70, 60, 80); Score(30, 70, 60); Score(30, 70); } static void Score(int m1,int m2,int m3 = 50,int m4 = 30) { Console.WriteLine(m1 + m2 + m3 + m4); } www.dotnetvideotutorial.com