Skip to content

ErcanOPAK.com

  • ASP.Net WebForms
  • ASP.Net MVC
  • C#
  • SQL
  • MySQL
  • PHP
  • Devexpress
  • Reportviewer
  • About
C#

What is the difference between ‘ref’ and ‘out’ keywords in C#

- 14.01.23 - ErcanOPAK

The best way to understand that difference is to show it in code.

Let’s say we have an ‘Add’ function. And the only thing that method does is add 10 to the parameter value.

static void Main (string[] args) 
{ 
     int myParameter = 20; 
     Add(myParameter); 

     Console.WriteLine(myParameter); }

static void Add(int myParameter)
{
     myParameter = myParameter + 10;
}

What happens if that code works?

static void Main (string[] args) 
{ 
int myParameter = 20; //myParameter = 20;
Add(myParameter); //myParameter is sent to function and there a new copy of this value manipulated to 30 (20 + 10)

Console.WriteLine(myParameter); //myParameter = 20 (Add function did not change the value of myParameter in the main.) 
}

 

 

Now it is time to use “ref“:

static void Main (string[] args) 
{ 
     int myParameter = 20; 
     Add(myParameter); 

     Console.WriteLine(ref myParameter); }

static void Add(ref int myParameter)
{
     myParameter = myParameter + 10;
}

What happens if that code works?

static void Main (string[] args) 
{ 
int myParameter = 20; //myParameter = 20;
Add(myParameter); //myParameter is sent to function and its refence value updated to 30 (20 + 10)

Console.WriteLine(myParameter); //myParameter = 30 (Add function updated the value of myParameter in the main.)
}

 

 

And finally, let’s use “out“:

static void Main (string[] args) 
{ 
     int myParameter = 20; 
     Add(myParameter); 

     Console.WriteLine(out myParameter); }

static void Add(out int myParameter)
{
     myParameter = 0; //We have to initialize the out parameter inside the function.
     myParameter = myParameter + 10;
}

What happens if that code works?

static void Main (string[] args) 
{ 
int myParameter = 20; //myParameter = 20;
Add(myParameter); //myParameter is sent to function and its refence value updated to 10 (0 + 10)

Console.WriteLine(myParameter); //myParameter = 10 (Add function updated the value of myParameter in the main. But this time the value we sent in the main is ignored.)
}

So here are the main differences between ref and out:
– You have to initialize your ref parameter before sending. But you don’t have to do that for out parameter.
– You have to initialize your out parameter in the function you sent. But you don’t have to do that for ref parameter.

Related posts:

How to access session variable in App_Code class file?
Change some value inside List
Using View Model to pass Multiple Models in Single View in MVC
What is the difference between IQueryable and IEnumerable?
Post Views: 1

Post navigation

How to check if javascript is enabled on the client’s browser

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

January 2023
M T W T F S S
 1
2345678
9101112131415
16171819202122
23242526272829
3031  
« Dec    

Most Viewed Posts

  • Get the First and Last Word from a String or Sentence in SQL (512)
  • Add Constraint to SQL Table to ensure email contains @ (288)
  • Get the User Name and Domain Name from an Email Address in SQL (274)
  • How to use Map Mode for Vertical Scroll Mode in Visual Studio (263)
  • Find numbers with more than two decimal places in SQL (246)
  • ASPxGridView – Disable CheckBox based on condition in GridViewCommandColumn (230)
  • Confirm before process with ASPxButton in Devexpress (229)
  • How to solve “Response.Redirect cannot be called in a Page callback” for DevExpress Components (221)
  • How to make some specific word(s) Bold or Underline in ReportViewer (205)
  • Devexpress ASPxGridview Column Grouping in Code (205)

Recent Posts

  • What is the difference between ‘ref’ and ‘out’ keywords in C#
  • How to check if javascript is enabled on the client’s browser
  • How to disable ASP.Net button after click to prevent double clicking
  • What is the difference between HashSet and List in .net?
  • What is the purpose of nameof in C#?
  • What is the difference between Select and SelectMany in Linq
  • Differences between FirstOrDefault and SingleOrDefault in LINQ
  • How to reset identity seed after deleting records in SQL
  • The Ternary Operator in C# (?:)
  • The Null Conditional Operator in C# (?.)

Most Viewed Posts

  • Get the First and Last Word from a String or Sentence in SQL (512)
  • Add Constraint to SQL Table to ensure email contains @ (288)
  • Get the User Name and Domain Name from an Email Address in SQL (274)
  • How to use Map Mode for Vertical Scroll Mode in Visual Studio (263)
  • Find numbers with more than two decimal places in SQL (246)

Recent Posts

  • What is the difference between ‘ref’ and ‘out’ keywords in C#
  • How to check if javascript is enabled on the client’s browser
  • How to disable ASP.Net button after click to prevent double clicking
  • What is the difference between HashSet and List in .net?
  • What is the purpose of nameof in C#?

Social

  • ErcanOPAK.com
  • GoodReads
  • LetterBoxD
  • Linkedin
  • The Blog
  • Twitter

© 2023 ErcanOPAK.com

Proudly powered by WordPress | Theme: Xblog Plus by wpthemespace.com