How can we return Multiple values from a method in C#.

Introduction

Sometimes we need to more than one value from function. In C# we can use either of three approaches:

  1. Using output parameters
  2. Using array
  3. Using structs

In our example we have an array, in which we have to find min, max, and sum of all its elements, means we need to multiple return value from a method.

Using output parameters:

We could use output parameters to get multiple value return from method. In method that take output parameters (via out keyword),we must assign appropriate value before exiting the method scope. (if we fail to do so, we will receive compiler errors).

https://www.flickr.com/photos/124656725@N04/14246707793/player/

Using array:

we can use a array to get multiple value from method.

Using structs:

for e.g.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s