C# ref Vs out

C# ref Vs out

C# Ref Vs Out Parameters Would you like to see how to use Ref parameter in C#.net and Vb.net? Here are very simple example of both ref and out parameters in dot net with syntax. In C# ref Vs. out difference is, you must declare the value for the variable before passing it to function with ref keyword and using Out keyword, you don't need to assign the value to it.

If you want to pass the value type data as a reference type to a function, use ref keyword.

C# ref example.
using System;

namespace Csharp.Ref.Example
{
  class Program
  {

    static void Main(string[] args)
    {

      int number = 10;

       ChangeNumber(ref number);

Console.WriteLine("Number 10 is passed with ref parameter and it's changed value is : {0}",number);

      Console.ReadLine();
    }

    private static int ChangeNumber(ref int number)
    {
      number = number + 10;         
      return number;
    }    

  }
}
Vb.net Ref example.

Imports System

Namespace Csharp.Ref.Example
	Class Program

	Private Shared Sub Main(args As String())

		Dim number As Integer = 10

		ChangeNumber(number)
Console.WriteLine("Number 10 is passed with ref parameter and it's changed value is : {0}", number)

		Console.ReadLine()
		End Sub

		Private Shared Function ChangeNumber(ByRef number As Integer) As Integer
			number = number + 10
			Return number
		End Function

	End Class
End Namespace
Output

Number 10 is passed with ref parameter and it's changed value is : 20.

Argument '1' must be passed with the 'ref' keyword.

You get compile time error "Argument '1' must be passed with the 'ref' keyword". The correct syntax is

In calling method.

ChangeNumber(ref number);

And implementation is like below one.

int ChangeNumber(ref int number)
    {
      number = number + 10;         
      return number;
    }    

C# and Vb.net Out parameter examples

Using Out keyword in parameter, you don't need to assign variabble before passing it to function. But, in function, you must assign a value to it.

C# out parameter example
using System;

namespace Csharp.Out.Example
{
  class Program
  {

    static void Main(string[] args)
    {

      int number;

       ChangeNumber(out number);

      Console.WriteLine("Number is not declared before passing to function and it's value after function call is : {0}",number);

      Console.ReadLine();
    }

    private static int ChangeNumber(out int number)
    {
      number = 10 + 50;
      return number;
    }    

  }
}
VB.net Out parameter example.

Imports System

Namespace Csharp.Out.Example
	Class Program

		Private Shared Sub Main(args As String())

			Dim number As Integer

			ChangeNumber(number)

			Console.WriteLine("Number is not declared before passing to function and it's value after function call is : {0}", number)

			Console.ReadLine()
		End Sub

		Private Shared Function ChangeNumber(number As Integer) As Integer
			number = 10 + 50
			Return number
		End Function

	End Class
End Namespace
Output.

Number is not declared before passing to function and it's value after function call is : 60

Tags:

CSS Padding Vs Margin CSS Margin Vs PaddingLCD Vs LED Vs (Edge Led) Televisions.Change Firefox Proxy SettingsC# foreach loopc# arraylistc# stringFirefox downloadC# DateTime Difference.net String vs stringC# XML XmlReaderC# Regular Expression Get Text Between Brackets

Author

My name is Satalaj, but people call me Sat. Here is my homepage: . I live in Pune, PN and work as a Software Engineer. I'm former MVP in ASP.net year 2010.
Disclaimer: Views or opinion expressed here are my personal research and it has nothing to do with my employer. You are free to use the code, ideas/hints in your projects. However, you should not copy and paste my original content to other web sites. Feel free to copy or extend the code.
If you want to fight with me, this website is not for you.
 

I'm Satalaj.