Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • C# Data Types
  • C# Decision Making
  • C# Methods
  • C# Delegates
  • C# Constructors
  • C# Arrays
  • C# ArrayList
  • C# String
  • C# Tuple
  • C# Indexers
  • C# Interface
  • C# Multithreading
  • C# Exception
Open In App
Next Article:
Difference Between List and Set in C#
Next article icon

Difference between Ref and Out keywords in C#

Last Updated : 16 Sep, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

The out is a keyword in C# which is used for the passing the arguments to methods as a reference type. It is generally used when a method returns multiple values. The out parameter does not pass the property.

Example :

CSharp
// C# program to illustrate the // concept of out parameter using System;  class GFG {      // Main method     static public void Main()     {          // Declaring variable         // without assigning value         int G;          // Pass variable G to the method         // using out keyword         Sum(out G);          // Display the value G         Console.WriteLine("The sum of" +                  " the value is: {0}", G);     }      // Method in which out parameter is passed     // and this method returns the value of     // the passed parameter     public static void Sum(out int G)     {         G = 80;         G += G;     } } 

Output:

The sum of the value is: 160

The ref is a keyword in C# which is used for the passing the arguments by a reference. Or we can say that if any changes made in this argument in the method will reflect in that variable when the control return to the calling method. The ref parameter does not pass the property.

Example:

CSharp
// C# program to illustrate the // concept of ref parameter using System;  class GFG {      // Main Method     public static void Main()     {          // Assign string value         string str = "Geek";          // Pass as a reference parameter         SetValue(ref str);          // Display the given string         Console.WriteLine(str);     }      static void SetValue(ref string str1)     {          // Check parameter value         if (str1 == "Geek") {             Console.WriteLine("Hello!!Geek");         }          // Assign the new value         // of the parameter         str1 = "GeeksforGeeks";     } } 

Output:

Hello!!Geek GeeksforGeeks 
Difference between Ref and Out keywords
ref keywordout keyword
It is necessary the parameters should initialize before it pass to ref. It is not necessary to initialize parameters before it pass to out.
It is not necessary to initialize the value of a parameter before returning to the calling method.It is necessary to initialize the value of a parameter before returning to the calling method.
The passing of value through ref parameter is useful when the called method also need to change the value of passed parameter.The declaring of parameter through out parameter is useful when a method return multiple values.
When ref keyword is used the data may pass in bi-directional. When out keyword is used the data only passed in unidirectional.

Note:

Both

ref

and

out

parameter treated same at compile-time but different at run-time.



Next Article
Difference Between List and Set in C#
author
ankita_saini
Improve
Article Tags :
  • C#
  • CSharp-keyword

Similar Reads

  • Difference between readonly and const keyword in C#
    In C#, a const keyword is used to declare constant fields and constant local. The value of the constant field is the same throughout the program or in other words, once the constant field is assigned the value of this field is not be changed. In C#, constant fields and locals are not variables, a co
    3 min read
  • Difference Between List and Set in C#
    The list is C# is the same as the list in JAVA. Basically, it is a type of object which can store variables. But in difference with objects, it stores the variables only in a specific order. Following is the syntax from which we can declare variables: Syntax: List<int> numbers = new List<in
    2 min read
  • Difference Between VB.NET and C#
    Visual Basic .NET is a high-level programming language that was initially developed in 1991. It was the first programming language that directly supported programming graphical user interfaces using language-supplied objects. It supports all the concepts of an object-oriented such as object, class,
    2 min read
  • Difference Between Properties and Indexers in C#
    Properties in C# are named members that use access modifiers to set and retrieve values of fields declared in a secured manner. Properties are used for abstracting and encapsulating access to a field of a class by defining only important actions and hiding their implementation. Properties are invoke
    2 min read
  • Difference between String and string in C#
    String is an alias for System.String class and instead of writing System.String one can use String which is a shorthand for System.String class and is defined in the .NET base class library. The size of the String object in memory is 2GB and this is an immutable object, we can not modify the charact
    2 min read
  • Difference Between C# and ASP.NET
    Pre-requisites: C#, ASP.NET C# (also known as C sharp) is an object-oriented programming language that is used to produce an array of applications for gaming, mobile, web, and Windows platforms also It is a modern and type-safe language and provides simple syntax which makes it easier to learn and i
    2 min read
  • Difference between C and C#
    C language: C language is a middle programming language that was developed at Bell research lab in 1972 by Dennis Ritchie. C language combines the properties of low level and high-level language. therefore its thought-about a middle programming Language. C may be a high-level classical kind programm
    2 min read
  • Difference between Method Overriding and Method Hiding in C#
    Method Overriding is a technique that allows the invoking of functions from another class (base class) in the derived class. Creating a method in the derived class with the same signature as a method in the base class is called Method Overriding. In simple words, Overriding is a feature that allows
    4 min read
  • Difference between Boxing and Unboxing in C#
    Boxing and unboxing is an important concept in C#. C# Type System contains three data types: Value Types (int, char, etc), Reference Types (object) and Pointer Types. Basically, it converts a Value Type to a Reference Type, and vice versa. Boxing and Unboxing enables a unified view of the type syste
    2 min read
  • Difference between Console.Write and Console.WriteLine in C#
    In C#, to print the data on the console output screen the following method are used - Console.Write() and Console.WriteLine() method. Console is a predefined class of System namespace. While Write() and WriteLine() both are the Console Class methods. The only difference between the Write() and Write
    1 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences