Illustration of C# (C Sharp) Keywords (params, ref, out) Used to DeclareMethod
Parameters
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
{ 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 dont 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
{ 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
{ Output of this code will be: Result of
10*20 = 200 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.
|