Skip to main content

What is Ref and out parameters in c# ?


About Ref and out parameters in c#!
Introduction :
ref :
we can pass an argument as a referance with this keyword. If any changes happen in the methos then the value will be reflected in the calling method. The value of the ref variable mustbe initalized befour calling the methos. Static keyword we have to use in the method.

Syntex:

public static return_type method_name(ref variable_name)
{
// body of the method
}


out :
The functionality of out key word is also same like ref but here we can pass an argument without initalized the value. Here also static key word have to use.

Syntex:
public static return_type method_name(out variable_name)
{
// body of the method
}

Example :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace codePractice
{
         class Program
        {
             static void Main(string[] args)
            {
                int x = 0, y = 10;
                callFunction(ref x);
                Console.WriteLine("value of x = " + x);
                Console.WriteLine("Befour calling method value of y = " + y);
                callFunction2(out y);
                Console.WriteLine("value of y = " + y);
                Console.ReadKey();
            }
                static void callFunction(ref int x)
                {
                         x = 15;
                }
                static void callFunction2(out int y)
                {
                                y = 15;
                }
        }
}

Output :

value of x = 15
Befour calling method value of y = 10

value of y = 15
Note :
In the method overloading time we can not use ref and out keyword at the same time.

Example :

static void Main(string[] args)
{
                int x = 0, y = 10;
                callFunction(ref x);
                Console.WriteLine("value of x = " + x);
                Console.WriteLine("Befour calling method value of y = " + y);
                callFunction(out y);
                Console.WriteLine("value of y = " + y);
                Console.ReadKey();
}
static void callFunction(ref int x)
{
                x = 15;
}
static void callFunction(out int y)
{
                y = 15;
}

Error:
//Error 1 Cannot define overloaded method 'callFunction' because it differs from another method only on
ref and out

Comments

Popular posts from this blog

How to Create Login Page with Material Design !

Material Design for web application In this article I’m going to show you how to how to apply material design in web application.  we will take  materialize CSS framework to design our login page .   Materialize CSS is free open source frontend designing framework. It provides material design.   It is like a bootstrap framework , but we can do many thing using this framework. < link href ="https://fonts.googleapis.com/icon?family=Material+Icons" rel ="stylesheet" />   < link rel ="stylesheet" href ="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.100.2/css/materialize.min.css" />   < script type ="text/javascript" src ="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></ script >   < script type ="text/javascript" src ="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.100.2/js/materialize.min.js"></ s...

How to find sum of the digits of a number in c#?

Sum of the Digits of a number Program: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApplication1 {     class Program     {         static void Main( string [] args)         {             int num, sum = 0;             Console .WriteLine( "Enter a number :\n" );             num = Convert .ToInt32( Console .ReadLine());             if (num > 0)             {                 for ( int temp = num; tem...

How to check a number palindrome or not in c# ?

Check a number palindrome  or not! Program:  using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApplication1 {     class Program     {         static void Main( string [] args)         {             int num, rem, rev = 0;             Console .WriteLine( "//How to check a number palindrome or not in c# ?" );             Console .WriteLine( "Enter A number :" );             num = Convert .ToInt32( Console .ReadLine());             if (num > 0)      ...