Вы находитесь на странице: 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

Вам также может понравиться