Illustration of C# (C Sharp) Keywords (params, ref, out) Used to Declare

Method Parameters

While coding, you might have come across situations where in you need to return multiple values from a single method. But a method can return only one return value. Then how do you accomplish it?


You might use multiple methods to return one value each or you can put all return values into an array, send the array as the result of the method and the receiving method should parse the array content and assign it to required variables. This will be a round about way. Is there any direct way to return multiple values from a function? Yes you can do it using ref and out parameters of a method. This article will discuss in detail about these parameters. In addition, you will also understand about how a single method parameter can accept varying arguments.

ref Parameters:

You can return multiple values from your method using ref parameters. Here is an example:

class sampleClass {
int member1, member2;
public void setValues(int data1, int data2) {
member1 = data1;
member2 = data2;
}
public void getValues(ref int data1, ref int data2) {
data1 = member1;
data2 = member2;
}
}
class testClass {
public static void Main() {
sampleClass obj = new sampleClass();
obj.setValues(100, 200);
int data1 = 0;
int data2 = 0;
obj.getValues(ref data1, ref data2);
Console.WriteLine(“The Values are: {0}, {1}”, data1, data2);
}
}

Output of this code will be:

The Values are: 100, 200

In this example, you want to return member1 and member2 values of sampleClass instance. Instead of having two methods to return member1 and member2 separately, you try to return both the values using the same method. How did you do it?

Create a method called getValues inside sampleClass. You want to return member1 and member2 as data1 and data2 respectively. Hence define data1 and data2 as method parameters of getValues method. Since they are return values and not input parameters of the method, associate ref keyword along with both the parameters.

Inside method body of getValues, you assign member1 and member2 to data1 and data2 respectively. Now create a class called testClass. Inside Main() method of testClass, instantiate sampleClass and set values for member1 and member2. To retrieve these values, call getValues method passing two ref arguments data1 and data2. Note that data1 and data2 have to be declared and initialized before using them as ref parameters. After initializing the variables and calling the getValues method by passing these variables, the value of member1 and member2 will now be available in data1 and data2 respectively. Print these variables and check if the values are returned correctly.

Hope you will be impressed with this approach of returning multiple values as method parameters. But as stated above, you should always initialize ref parameters before passing them to the actual method. If you don’t initialize these variables, then you will end up in an error during compilation.

Is there a way to avoid this initialization? Yes, you can achieve the same result without initializing the parameters using out parameter.

out Parameters:

The above discussed example can be modified to use out parameters as shown below:

class sampleClass {
int member1, member2;
public void setValues(int data1, int data2) {
member1 = data1;
member2 = data2;
}
public void getValues(out int data1, out int data2) {
data1 = member1;
data2 = member2;
}
}
class testClass {
public static void Main() {
sampleClass obj = new sampleClass();
obj.setValues(100, 200);
int data1, data2;
obj.getValues(out data1, out data2);
Console.WriteLine(“The Values are: {0}, {1}”, data1, data2);
}
}

Output of this code will be:

The Values are: 100, 200

This example incorporates the same functionality as that of earlier example. Both ref and out parameters are meant for returning multiple return values. The only difference is that, while using ref parameters you have to initialize the parameters before passing them to the method. But out parameters need not be initialized.

params Keyword:

Assume that you write a method to multiply integer numbers. User might multiply two numbers or three numbers or even ten numbers? How will you accommodate it in your code? You have to overload the method to multiply two numbers, three numbers and more. Instead what if you can pass varying number of arguments to the same method and multiply any number of integers using the same method? Interesting… C# provides you the option to do it using params keyword. The scenario discussed can be implemented as shown below:

class sampleClass {
public int multiply(params int[] numbers) {
int returnValue = 0;
if (numbers.Length > 0) {
returnValue = 1;
foreach(int number in numbers) {
returnValue = returnValue * number;
}
}
return returnValue;
}
}
class testClass {
public static void Main() {
sampleClass obj = new sampleClass();
int result = obj.multiply(10,20);
Console.WriteLine(“Result of 10*20 = {0}”, result);
result = obj.multiply(1,2,3,4);
Console.WriteLine(“Result of 1*2*3*4= {0}”, result);
}
}

Output of this code will be:

Result of 10*20 = 200
Result of 1*2*3*4 = 24

In the multiply method, you define the argument as an integer array associated with params keyword. While calling the method, you pass any number of integer values comma separated.

SetApp - 100 Apps for everything you ever wanted to do on Mac

Add to My Yahoo!


FREE Subscription

Subscribe to our mailing list and receive new articles
through email. Keep yourself updated with latest
developments in the industry.

Name:
Email:

Note : We never rent, trade, or sell my email lists to
anyone. We assure that your privacy is respected
and protected.

Visit .NET Programming Tutorial Homepage

______________________________________________________

Recommended Resource

| How Do You Establish Polymorphism in C# (C Sharp) | How Do You Overload == Operator in C# (C Sharp) ? | How Do You Overload == Operator in C# (C Sharp) ? | How to Perform User Defined Conversions Between Structures (Struct) | Illustration of C# (C Sharp) Keywords (params, ref, out) Used to Declare | Introduction to Static Classes of C# (C Sharp) | Introduction to Static Members of C# ( C Sharp) | Overview of Structs in C# (C Sharp) | Using Constructors and Destructors in C# (C Sharp) |


“Amazon and the Amazon logo are trademarks of Amazon.com, Inc. or its affiliates.”

| Privacy Policy for www.dotnet-guide.com | Disclosure | Contact |

Copyright - © 2004 - 2019 - All Rights Reserved.