0% found this document useful (0 votes)
272 views6 pages

Passing Arguments and Returning Values To / From Methods

The document discusses different C# concepts related to methods: 1. Methods can receive arguments and return values. An example class demonstrates methods that set values, display values, and return the maximum or minimum value. 2. Reference parameters allow a method to modify the actual parameter passed in. 3. Method overloading allows multiple methods to have the same name but differ in parameters. Examples show overloading based on parameter type and number.

Uploaded by

Ramesh Kumar
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC or read online on Scribd
0% found this document useful (0 votes)
272 views6 pages

Passing Arguments and Returning Values To / From Methods

The document discusses different C# concepts related to methods: 1. Methods can receive arguments and return values. An example class demonstrates methods that set values, display values, and return the maximum or minimum value. 2. Reference parameters allow a method to modify the actual parameter passed in. 3. Method overloading allows multiple methods to have the same name but differ in parameters. Examples show overloading based on parameter type and number.

Uploaded by

Ramesh Kumar
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC or read online on Scribd
You are on page 1/ 6

Passing Arguments and Returning Values to / from Methods

usingAs you know in C++, the member methods can receive one or more System; using System.Collections.Generic; arguments and also can return a value at the end of the method. using System.Linq; using System.Text;

Application 16: Passing Arguments and Returning ArgsAndReturnDemo namespace Values


{

class MyNumbers { //data members private int a, b; //methods public void SetValues(int FirstValue, int SecondValue) { a = FirstValue; b = SecondValue; } public void ShowValues() { Console.WriteLine("The values are " + a + " and " + b); } public int GetMax() { if (a > b) return (a); else return (b); } public int GetMin() { if (a < b) return (a); else return (b); } } class Program { static void Main(string[] args) { //read values from keyboard int FirstVal, SecondVal; Console.WriteLine("Enter any two values:"); FirstVal = Convert.ToInt32(Console.ReadLine()); SecondVal = Convert.ToInt32(Console.ReadLine()); //create instance of MyNumbers class MyNumbers mn; mn = new MyNumbers(); //access methods mn.SetValues(FirstVal, SecondVal); mn.ShowValues(); Console.WriteLine("The maximum value is " + mn.GetMax()); Console.WriteLine("The minimum value is " + mn.GetMin()); .NET 3.5 and Visual Studio 2008 Hour 15 - Page Console.Read(); } } }

1 of 6

int FirstVal, SecondVal; Console.WriteLine("Enter any two values:"); FirstVal = Convert.ToInt32(Console.ReadLine()); SecondVal = Convert.ToInt32(Console.ReadLine()); //create instance of MyNumbers class MyNumbers mn; mn = new MyNumbers(); //access methods mn.SetValues(FirstVal, SecondVal); mn.ShowValues(); Console.WriteLine("The maximum value is " + mn.GetMax()); Console.WriteLine("The minimum value is " + mn.GetMin()); Console.Read(); } } }

Output:

Ref Parameters
The reference parameters are similar to the normal parameters. using System; The only difference between the normal parameters and reference using System.Collections.Generic; usingparameters is: When the value is changed in the reference parameter, System.Linq; using System.Text; would automatically affect the actual parameter in the calling portion. namespace RefParameterDemo { class SampleClass parameter at the calling portion cant be a constant. Rule: The actual { This is just like Call by reference concept in C/C++. public void FirstMethod(int x, int y) Application 17: Ref { Parameters x++; y++; } public void SecondMethod(int x, ref int y) { x++; y++; } } class Program { static void Main(string[] args) .NET 3.5 and Visual Studio 2008 Hour 15 - Page { int a = 10, b = 20; SampleClass sc = new SampleClass();

2 of 6

sc.FirstMethod(a, b); Console.WriteLine(a + ", " + b); sc.SecondMethod(a, ref b); Console.WriteLine(a + ", " + b); Console.Read(); } } }

} class Program { static void Main(string[] args) { int a = 10, b = 20; SampleClass sc = new SampleClass(); Console.WriteLine(a + ", " + b); sc.FirstMethod(a, b); Console.WriteLine(a + ", " + b); sc.SecondMethod(a, ref b); Console.WriteLine(a + ", " + b); Console.Read(); } } }

public void FirstMethod(int x, int y) { x++; y++; } public void SecondMethod(int x, ref int y) { x++; y++; }

Output:

Method Overloading

Just like in C++, C# allows you to overload the methods. Method overloading is nothing but writing multiple methods with same name. To overload methods, you should follow the below rules. 1. All of the overloaded methods name should be same.

.NET 3.5 and Visual Studio 2008

Hour 15 - Page 3 of 6

2. Any difference between the method arguments should be maintained.


The difference may be in no. of arguments or the data types of arguments.

Application 18: Method Overloading

using using using using

System; System.Collections.Generic; System.Linq; System.Text;

namespace SimpleMethodOverloading { class SimpleOverloadDemo { public void Show(int n) { Console.WriteLine("An integer value is found: " + n); } public void Show(double d) { Console.WriteLine("A double value is found: " + d); } public void Show(string s) { Console.WriteLine("A string value is found: " + s); } } class Program { static void Main(string[] args) { SimpleOverloadDemo sod = new SimpleOverloadDemo(); sod.Show(92); sod.Show(1043.3948); sod.Show("Hello, World!"); Console.Read(); } } }

Output:

.NET 3.5 and Visual Studio 2008

Hour 15 - Page 4 of 6

Application 19: Method Overloading


using using using using System; System.Collections.Generic; System.Linq; System.Text;

namespace MethodOverloading { class OverloadSample { public int GetMax(int a, int b) { if (a > b) return (a); else return (b); } public double GetMax(double x, double y) { if (x > y) return (x); else return (y); } public double GetMax(double x, double y, double z) { if (x > y && x > z) return (x); else if (y > x && y > z) return (y); else return (z); } } class Program { static void Main(string[] args) { OverloadSample os = new OverloadSample(); Console.WriteLine(os.GetMax(15, 20)); Console.WriteLine(os.GetMax(87.12, 102.8273, 98.56 )); Console.WriteLine(os.GetMax(87.56, 45.12 )); Console.Read(); } .NET 3.5 and Visual Studio 2008 Hour 15 } }

- Page 5 of 6

Output:

Recursion

Same as C/C++.

Operator Overloading

Same as C++, but the operator method should be static.

.NET 3.5 and Visual Studio 2008

Hour 15 - Page 6 of 6

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy