Home » C# Ref Returns and Locals

C# Ref Returns and Locals

by Online Tutorials Library

C# Ref returns and locals

C# ref keyword allows a method to return reference rather than a value. In C# prior versions, a method can return only value.

A variable that holds returned reference known as ref local.

A method that returns reference has certain restrictions that are listed below.

  • Method can’t use void return type
  • Method can’t return local variable
  • Method can’t return null value
  • Method can’t return constant, an enumeration, and property of a class or struct

Let’s see an example.


C# Ref Returns Example

Output:

John 

Ref local is a variable that is used to store the reference returned by the method. Let’s see an example.

C# Ref Local Example

Output:

Array: [Rahul,John,Mayank,Irfan] Updated array: [Rahul,John,Mayank,Peter] 

You may also like