TheDeveloperBlog.com

Home | Contact Us

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

C# Call By Reference

C# Call by Reference 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

C# Call By Reference

C# provides a ref keyword to pass argument as reference-type. It passes reference of arguments to the function rather than copy of original value. The changes in passed values are permanent and modify the original variable value.

C# Call By Reference Example

using System;
namespace CallByReference
{
    class Program
    {
        // User defined function
        public void Show(ref int val)
        {
             val *= val; // Manipulating value
            Console.WriteLine("Value inside the show function "+val);
            // No return statement
        }
        // Main function, execution entry point of the program
        static void Main(string[] args)
        {
            int val = 50;
            Program program = new Program(); // Creating Object
            Console.WriteLine("Value before calling the function "+val);
            program.Show(ref val); // Calling Function by passing reference          
            Console.WriteLine("Value after calling the function " + val);
        }
    }
}

Output:

Value before calling the function 50
Value inside the show function 2500
Value after calling the function 2500

Next TopicC# Out Parameter




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