• Business
  • Finance
  • Banking
  • Education
  • General
  • Law
  • Science
  • IT
  • English

Key Differences

Know the Differences & Comparisons

Difference Between ref and out parameter in c#

Last updated on June 8, 2019 by Surbhi S

ref and out parameterref (Reference) Parameter

“ref” parameter is used to pass the value by reference from actual parameter to formal parameter i.e from callee method to the called method. In C#, a parameter declared with a “ref” modifier is a reference parameter. When you pass parameters by reference, unlike value parameters, a new location is not created for this parameter. Any changes made to the formal parameter will reflect in the actual parameter.

Example of ref Parameter
refAs shown in the above program snippet, any changes made to m will be reflected to n, as the parameter is passed by reference.

out (Output) Parameter

The output parameter is used to pass the result back to the calling function. ‘out’ keyword is used to declare the parameter as an Output parameter. Similar to a reference parameter, an output parameter does not create a new storage location. Instead, it becomes an alias to the parameter in the calling method.

Example of out Parameter
ref outIn the above program, x is declared as an out parameter in calling function and as you can see, x has not been initialized. When Square function terminates, the value of q will be copied to the output parameter x.

Difference between ref and out parameter in C#

  1. In the reference parameter, changes made to the formal parameter reflects the actual parameter, while in out parameter, value is reflected back to the out parameter of the calling function.
  2. The ‘output’ actual parameter usually not assigned a value while an actual parameter declared as the reference should always be assigned a value before calling.
    Note: You must assign value to the out parameter in method body; otherwise the method won’t be compiled.
  3. Both out and ref parameters don’t create a new memory location.
  4. The ref and out parameters are treated differently at run-time, but they are treated the same at compile time.

Related Differences

  • Difference Between Copyright Infringement and Plagiarism
  • Difference Between Convex and Concave Lens
  • Difference Between To and Too
  • Difference Between Physical Capital and Human Capital
  • Difference Between CV and Resume

You Might Also Like:

method overloading vs overridingDifference Between Method Overloading and Overriding write vs writelineDifference Between Write and WriteLine parameter vs statisticDifference Between Statistic and Parameter abstract class vs interfaceDifference Between Abstract class and Interface in C# binomial vs poisson distributionDifference Between Binomial and Poisson Distribution Standard Costing vs Budgetary controlDifference Between Standard Costing and Budgetary Control

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Top 5 Differences

  • Difference Between PERT and CPM
  • Difference Between Micro and Macro Economics
  • Difference Between Developed Countries and Developing Countries
  • Difference Between Management and Administration
  • Difference Between Qualitative and Quantitative Research

Related Differences

  • Difference Between Copyright Infringement and Plagiarism
  • Difference Between Convex and Concave Lens
  • Difference Between To and Too
  • Difference Between Physical Capital and Human Capital
  • Difference Between CV and Resume



New Additions

  • Difference Between Society and Community
  • Difference Between Sourcing and Procurement
  • Difference Between National Income and Per Capita Income
  • Difference Between Departmental Store and Multiple Shops
  • Difference Between Thesis and Research Paper
  • Difference Between MBO and MBE
  • Difference Between Receipt and Payment Account and Income and Expenditure Account
  • Difference Between Stock Dividend and Stock Split
  • Difference Between FOB and CIF Contracts
  • Difference Between Verification and Valuation
fb-follow youtube follow

Copyright © 2023 · Key Differences · Contact Us · About Us · Privacy