How to return more than one value from the function in C#.

You may have seen in many programming languages that function returns no value or one value but they can’t return more than one value. In this article you will understand how to return more than one value.

We can return multiple values through out keyword and also with ref keyword. Mostly out is keyword is used not ref but we can use both.

Ref Keyword:-

Ref keyword used to pass reference of the variable which is defined outside of the function. So we can use it by passing the value in function through it’s parameter and if we change the value in the parameter then the variable value will also changes. Value changes because when we pass the value through ref key then the variable inside the function is only having memory address of outside. Normally if we pass variable in parameter without ref keyword then new copy of the variable will be created.

In the below example we have demonstrated the usage of ref keyword.

As you can see that value of the variable refInt is getting changed even if we have not changed it directly. So to use it as to return value we can assign any value that we want to return then use it after function execution. But purpose of ref keyword is to use the value of variable in the function without creating another copy of the same value.

Out Keyword:-

Same as ref keyword we can use out keyword there is no difference in syntax just keyword is getting changed. So let’s see the example of out keyword.

As you can see that variable outString is also having value which is assigned to it after function successfully executed. Purpose of ref and out keyword is very different, let’s understand that as well.

Difference between ref and out keyword:-

Ref keyword is used to pass the reference of the object as parameter in method. It is used when you want to use the same value which is present outside without creating another copy.

Out is used to pass the reference of the object as parameter in the method. It is used when you want to return more than one value from the function.

It is necessary to initialize the value of object before using it in parameter as ref keyword.

It is not necessary to initialize the value of object before using it in parameter as out keyword

In case of ref it is not necessary to change the value of object inside the method.

In case of out it is necessary to change the value of the object inside the method.

I hope you understand how to return multiple values from function in C#. If you like this article please like, comment and share with your friends.

Bhagavad Gita Verse of the Day

अपि चेत्सुदुराचारो भजते मामनन्यभाक् |
साधुरेव स मन्तव्य: सम्यग्व्यवसितो हि स: || 30||

api chet su-durāchāro bhajate mām ananya-bhāk
sādhur eva sa mantavyaḥ samyag vyavasito hi saḥ

BG 9.30: Even if the vilest sinners worship Me with exclusive devotion, they are to be considered righteous, for they have made the proper resolve.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Vishal Pathak

Vishal Pathak

72 Followers

love ❤ coding, solving some industry problems technologies: JavaScript, C#, Angular, PLSQL, Docker Want to learn: Python, Go language, AI, ML and Cloud