SlideShare ist ein Scribd-Unternehmen logo
1 von 17
Downloaden Sie, um offline zu lesen
Methods
Eng Teong Cheah
Microsoft MVP Windows Development
Xamarin: the complete mobile lifecycle solution
Methods
Methods
Methods are used to define logic once and use it at many places, which make
maintenance of program easier.
Syntax of declaring methods”
[Attributes]
access-modifiers return-type methodname( parameters )
{
methodbody
}
Methods
Access modifiers can be private, public, protected, internal, protected internal.
- Return type can be any valid date type or void if nothing is returned by method.
- Method name is name of the method.
- Parameters can be any data type. Parameters are optional.
Methods are of two types:
- Static Methods
- Instance methods
Static methods
Static Methods are associated with class. Example:
class Program
{
//declaring and using static method
public static void print()
{
Console.WriteLine("printing done");
}
static void Main(string[] args)
{
print();
Console.ReadKey();
}
}
Instance methods
Instance methods are associated with instance. Example:
class Program
{
//declaring and using instance method
public void print()
{
Console.WriteLine("printing done");
}
static void Main(string[] args)
{
Program p = new Program();
p.print();
Console.ReadKey();
}
}
Methods parameters
There are four types of method parameters:
- Value parameters
Passing parameter by value
- Reference parameters
Passing parameter by reference
- Out parameters
Out parameter is used when we want multiple values output from single method and
don’t know the initialization value of input parameter.
- Parameters arrays
Passing array as parameter
Value parameters
Passing parameter by value:
class Program
{
//passing parameter by value
public void increment(int i)
{
++i;
}
static void Main(string[] args)
{
Program p = new Program();
int j = 10;
p.increment(j);
Console.WriteLine(j);
Console.ReadKey();
}
}
Result :-
10
Reference parameters
Keyword ref is use for passing the value type by reference.
Passing parameter by reference:
class Program
{
//passing parameter by reference
public void increment(ref int i)
{
++i;
}
static void Main(string[] args)
{
Program p = new Program();
int j = 10;
p.increment(ref j);
Console.WriteLine(j);
Console.ReadKey();
}
}
Out parameters
Out parameters is used when we want multiple values output from single method and
don’t know the initialization value of input parameter.
class Program
{
//passing out parameter
public int initialize(out int i)
{
i = 10;
int j = i + 1;
return j;
}
static void Main(string[] args)
{
Program p = new Program();
int j;
int increasedvalue = p.initialize(out j);
Console.WriteLine("value and incremented value is {0} {1}", j, increasedvalue);
Console.ReadKey();
}
}
Parameter arrays
Passing array as parameter:class Program
{
//passing array as parameter
public int[] initialize(int[] arr)
{
for (int i = 0; i < arr.Length - 1; i++)
{
arr[i] = i;
}
return arr;
}
static void Main(string[] args)
{
Program p = new Program();
int[] arr = new int[10];
// passing array as parameter;
p.initialize(arr);
for (int i = 0; i < arr.Length - 1; i++)
{
Console.WriteLine(arr[i]);
}
Console.ReadKey();
}
}
Optional and Named parameters
Using Optional parameters:
class Program
{
// length and breadth are optional parameter
public void print(int pages, int length = 100, int breadth = 40)
{
Console.WriteLine("length, breadth, no of pages {0} {1} {2}", length, breadth, pages);
}
static void Main(string[] args)
{
Program p = new Program();
p.print(10);
Console.ReadKey();
}
}
Optional and Named parameters
Using Named parameters:
class Program
{
public void print(int pages, int length, int breadth)
{
Console.WriteLine("length, breadth, no of pages {0} {1} {2}", length, breadth, pages);
}
static void Main(string[] args)
{
Program p = new Program();
// passing parameters by name
p.print(length: 10, breadth: 16, pages: 10);
Console.ReadKey();
}
}
Demo
Get Started Today
Xamarin.com
•Website:
• http://techoschool.com/
•Get Started:
• http://xamarin.com
Reference

Weitere ähnliche Inhalte

Was ist angesagt?

8 abstract classes and interfaces
8   abstract classes and interfaces 8   abstract classes and interfaces
8 abstract classes and interfaces
Tuan Ngo
 

Was ist angesagt? (20)

Java interface
Java interfaceJava interface
Java interface
 
Java interfaces
Java interfacesJava interfaces
Java interfaces
 
Java interfaces
Java   interfacesJava   interfaces
Java interfaces
 
Access Modifiers in C# ,Inheritance and Encapsulation
Access Modifiers in C# ,Inheritance and EncapsulationAccess Modifiers in C# ,Inheritance and Encapsulation
Access Modifiers in C# ,Inheritance and Encapsulation
 
Interface in java
Interface in javaInterface in java
Interface in java
 
Interface
InterfaceInterface
Interface
 
Java interfaces
Java interfacesJava interfaces
Java interfaces
 
06 abstract-classes
06 abstract-classes06 abstract-classes
06 abstract-classes
 
Inheritance and Polymorphism in java simple and clear
Inheritance and Polymorphism in java simple and clear Inheritance and Polymorphism in java simple and clear
Inheritance and Polymorphism in java simple and clear
 
Java abstract class & abstract methods
Java abstract class & abstract methodsJava abstract class & abstract methods
Java abstract class & abstract methods
 
java interface and packages
java interface and packagesjava interface and packages
java interface and packages
 
Interfaces and abstract classes
Interfaces and abstract classesInterfaces and abstract classes
Interfaces and abstract classes
 
Java interface
Java interfaceJava interface
Java interface
 
Interface in java ,multiple inheritance in java, interface implementation
Interface in java ,multiple inheritance in java, interface implementationInterface in java ,multiple inheritance in java, interface implementation
Interface in java ,multiple inheritance in java, interface implementation
 
8 abstract classes and interfaces
8   abstract classes and interfaces 8   abstract classes and interfaces
8 abstract classes and interfaces
 
Polymorphism presentation in java
Polymorphism presentation in javaPolymorphism presentation in java
Polymorphism presentation in java
 
Abstract Class Presentation
Abstract Class PresentationAbstract Class Presentation
Abstract Class Presentation
 
Interface
InterfaceInterface
Interface
 
Inheritance and Polymorphism Java
Inheritance and Polymorphism JavaInheritance and Polymorphism Java
Inheritance and Polymorphism Java
 
Java adapter
Java adapterJava adapter
Java adapter
 

Andere mochten auch (7)

Xamarin: Branching and Looping
Xamarin: Branching and LoopingXamarin: Branching and Looping
Xamarin: Branching and Looping
 
Introduction of C#
Introduction of C#Introduction of C#
Introduction of C#
 
C# Tutorial MSM_Murach chapter-16-slides
C# Tutorial MSM_Murach chapter-16-slidesC# Tutorial MSM_Murach chapter-16-slides
C# Tutorial MSM_Murach chapter-16-slides
 
C# Tutorial MSM_Murach chapter-06-slides
C# Tutorial MSM_Murach chapter-06-slidesC# Tutorial MSM_Murach chapter-06-slides
C# Tutorial MSM_Murach chapter-06-slides
 
Learn C# Programming - Data Types & Type Conversion
Learn C# Programming - Data Types & Type ConversionLearn C# Programming - Data Types & Type Conversion
Learn C# Programming - Data Types & Type Conversion
 
Xamarin - First Application
Xamarin - First ApplicationXamarin - First Application
Xamarin - First Application
 
Learn C# programming - Program Structure & Basic Syntax
Learn C# programming - Program Structure & Basic SyntaxLearn C# programming - Program Structure & Basic Syntax
Learn C# programming - Program Structure & Basic Syntax
 

Ähnlich wie Xamarin: C# Methods

Ähnlich wie Xamarin: C# Methods (20)

Unit 4 (1)
Unit 4 (1)Unit 4 (1)
Unit 4 (1)
 
Functions struct&union
Functions struct&unionFunctions struct&union
Functions struct&union
 
06slide.ppt
06slide.ppt06slide.ppt
06slide.ppt
 
Methods in Java
Methods in JavaMethods in Java
Methods in Java
 
static methods
static methodsstatic methods
static methods
 
09. Methods
09. Methods09. Methods
09. Methods
 
09. Java Methods
09. Java Methods09. Java Methods
09. Java Methods
 
CHAPTER 6
CHAPTER 6CHAPTER 6
CHAPTER 6
 
Methods intro-1.0
Methods intro-1.0Methods intro-1.0
Methods intro-1.0
 
JAVA_BASICS.ppt
JAVA_BASICS.pptJAVA_BASICS.ppt
JAVA_BASICS.ppt
 
Java programs
Java programsJava programs
Java programs
 
Pemrograman Python untuk Pemula
Pemrograman Python untuk PemulaPemrograman Python untuk Pemula
Pemrograman Python untuk Pemula
 
Computer programming 2 Lesson 15
Computer programming 2  Lesson 15Computer programming 2  Lesson 15
Computer programming 2 Lesson 15
 
Functional Programming in Java 8
Functional Programming in Java 8Functional Programming in Java 8
Functional Programming in Java 8
 
C function
C functionC function
C function
 
Java Foundations: Methods
Java Foundations: MethodsJava Foundations: Methods
Java Foundations: Methods
 
java150929145120-lva1-app6892 (2).pptx
java150929145120-lva1-app6892 (2).pptxjava150929145120-lva1-app6892 (2).pptx
java150929145120-lva1-app6892 (2).pptx
 
Presentation on Function in C Programming
Presentation on Function in C ProgrammingPresentation on Function in C Programming
Presentation on Function in C Programming
 
Function in c program
Function in c programFunction in c program
Function in c program
 
SeneJug java_8_prez_122015
SeneJug java_8_prez_122015SeneJug java_8_prez_122015
SeneJug java_8_prez_122015
 

Mehr von Eng Teong Cheah

Mehr von Eng Teong Cheah (20)

Monitoring Models
Monitoring ModelsMonitoring Models
Monitoring Models
 
Responsible Machine Learning
Responsible Machine LearningResponsible Machine Learning
Responsible Machine Learning
 
Training Optimal Models
Training Optimal ModelsTraining Optimal Models
Training Optimal Models
 
Deploying Models
Deploying ModelsDeploying Models
Deploying Models
 
Machine Learning Workflows
Machine Learning WorkflowsMachine Learning Workflows
Machine Learning Workflows
 
Working with Compute
Working with ComputeWorking with Compute
Working with Compute
 
Working with Data
Working with DataWorking with Data
Working with Data
 
Experiments & TrainingModels
Experiments & TrainingModelsExperiments & TrainingModels
Experiments & TrainingModels
 
Automated Machine Learning
Automated Machine LearningAutomated Machine Learning
Automated Machine Learning
 
Getting Started with Azure Machine Learning
Getting Started with Azure Machine LearningGetting Started with Azure Machine Learning
Getting Started with Azure Machine Learning
 
Hacking Containers - Container Storage
Hacking Containers - Container StorageHacking Containers - Container Storage
Hacking Containers - Container Storage
 
Hacking Containers - Looking at Cgroups
Hacking Containers - Looking at CgroupsHacking Containers - Looking at Cgroups
Hacking Containers - Looking at Cgroups
 
Hacking Containers - Linux Containers
Hacking Containers - Linux ContainersHacking Containers - Linux Containers
Hacking Containers - Linux Containers
 
Data Security - Storage Security
Data Security - Storage SecurityData Security - Storage Security
Data Security - Storage Security
 
Application Security- App security
Application Security- App securityApplication Security- App security
Application Security- App security
 
Application Security - Key Vault
Application Security - Key VaultApplication Security - Key Vault
Application Security - Key Vault
 
Compute Security - Container Security
Compute Security - Container SecurityCompute Security - Container Security
Compute Security - Container Security
 
Compute Security - Host Security
Compute Security - Host SecurityCompute Security - Host Security
Compute Security - Host Security
 
Virtual Networking Security - Network Security
Virtual Networking Security - Network SecurityVirtual Networking Security - Network Security
Virtual Networking Security - Network Security
 
Virtual Networking Security - Perimeter Security
Virtual Networking Security - Perimeter SecurityVirtual Networking Security - Perimeter Security
Virtual Networking Security - Perimeter Security
 

Kürzlich hochgeladen

IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
Enterprise Knowledge
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
Earley Information Science
 

Kürzlich hochgeladen (20)

Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 

Xamarin: C# Methods

  • 1. Methods Eng Teong Cheah Microsoft MVP Windows Development
  • 2. Xamarin: the complete mobile lifecycle solution
  • 4. Methods Methods are used to define logic once and use it at many places, which make maintenance of program easier. Syntax of declaring methods” [Attributes] access-modifiers return-type methodname( parameters ) { methodbody }
  • 5. Methods Access modifiers can be private, public, protected, internal, protected internal. - Return type can be any valid date type or void if nothing is returned by method. - Method name is name of the method. - Parameters can be any data type. Parameters are optional. Methods are of two types: - Static Methods - Instance methods
  • 6. Static methods Static Methods are associated with class. Example: class Program { //declaring and using static method public static void print() { Console.WriteLine("printing done"); } static void Main(string[] args) { print(); Console.ReadKey(); } }
  • 7. Instance methods Instance methods are associated with instance. Example: class Program { //declaring and using instance method public void print() { Console.WriteLine("printing done"); } static void Main(string[] args) { Program p = new Program(); p.print(); Console.ReadKey(); } }
  • 8. Methods parameters There are four types of method parameters: - Value parameters Passing parameter by value - Reference parameters Passing parameter by reference - Out parameters Out parameter is used when we want multiple values output from single method and don’t know the initialization value of input parameter. - Parameters arrays Passing array as parameter
  • 9. Value parameters Passing parameter by value: class Program { //passing parameter by value public void increment(int i) { ++i; } static void Main(string[] args) { Program p = new Program(); int j = 10; p.increment(j); Console.WriteLine(j); Console.ReadKey(); } } Result :- 10
  • 10. Reference parameters Keyword ref is use for passing the value type by reference. Passing parameter by reference: class Program { //passing parameter by reference public void increment(ref int i) { ++i; } static void Main(string[] args) { Program p = new Program(); int j = 10; p.increment(ref j); Console.WriteLine(j); Console.ReadKey(); } }
  • 11. Out parameters Out parameters is used when we want multiple values output from single method and don’t know the initialization value of input parameter. class Program { //passing out parameter public int initialize(out int i) { i = 10; int j = i + 1; return j; } static void Main(string[] args) { Program p = new Program(); int j; int increasedvalue = p.initialize(out j); Console.WriteLine("value and incremented value is {0} {1}", j, increasedvalue); Console.ReadKey(); } }
  • 12. Parameter arrays Passing array as parameter:class Program { //passing array as parameter public int[] initialize(int[] arr) { for (int i = 0; i < arr.Length - 1; i++) { arr[i] = i; } return arr; } static void Main(string[] args) { Program p = new Program(); int[] arr = new int[10]; // passing array as parameter; p.initialize(arr); for (int i = 0; i < arr.Length - 1; i++) { Console.WriteLine(arr[i]); } Console.ReadKey(); } }
  • 13. Optional and Named parameters Using Optional parameters: class Program { // length and breadth are optional parameter public void print(int pages, int length = 100, int breadth = 40) { Console.WriteLine("length, breadth, no of pages {0} {1} {2}", length, breadth, pages); } static void Main(string[] args) { Program p = new Program(); p.print(10); Console.ReadKey(); } }
  • 14. Optional and Named parameters Using Named parameters: class Program { public void print(int pages, int length, int breadth) { Console.WriteLine("length, breadth, no of pages {0} {1} {2}", length, breadth, pages); } static void Main(string[] args) { Program p = new Program(); // passing parameters by name p.print(length: 10, breadth: 16, pages: 10); Console.ReadKey(); } }
  • 15. Demo