TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

C# Ref Returns and Locals

C# Ref Returns and Locals for beginners and professionals with examples on overloading, method overriding, inheritance, aggregation, base, polymorphism, sealed, abstract, interface, namespaces, exception handling, file io, collections, multithreading, reflection etc.

<< Back to C-SHARP

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

using System;
namespace CSharpFeatures
{
    class RefReturnsExample
    {
        public static void Main(string[] args)
        {
            string[] students = { "Rahul", "John", "Mayank", "Irfan" };
            // Calling a method that returns a reference
            ref string student = ref FindStudent(students, "John");
            Console.WriteLine(student);
        }
        // A method that returns a ref type
        static ref string FindStudent(string[] students, string student)
        {
            for (int i = 0; i < students.Length; i++)
            {
                if (students[i].Equals(student))
                {
                    return ref students[i];
                }
            }
            throw new Exception("Student not found");
        }
    }
}

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

using System;
namespace CSharpFeatures
{
    class RefReturnsExample
    {
        public static void Main(string[] args)
        {
            string[] students = { "Rahul", "John", "Mayank", "Irfan"};
            Console.WriteLine("Array: [" + string.Join(",",students)+"]");
            // Creating local reference
            ref string student = ref students[3];
            student = "Peter";  // It will change array value at third index
            Console.WriteLine("Updated array: ["+string.Join(",",students)+"]");
        }
    }
}

Output:

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





Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf