• 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 Immigrate and Emigrate
  • Difference Between Newspaper and Magazine
  • Difference Between Formal and Informal Writing
  • Difference Between Passed and Past
  • Difference Between Nominee and Legal Heir

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 Immigrate and Emigrate
  • Difference Between Newspaper and Magazine
  • Difference Between Formal and Informal Writing
  • Difference Between Passed and Past
  • Difference Between Nominee and Legal Heir



New Additions

  • Difference Between Liquidated and Unliquidated Damages
  • Difference Between When and While
  • Difference Between Tort and Contract
  • Difference Between Monopoly and Perfect Competition
  • Difference Between Economic and Social Infrastructure
  • Difference Between Transitive and Intransitive Verbs
  • Difference Between Economic Development and Sustainable Development
  • Difference Between Autonomous and Accommodating Items
  • Difference Between Personal and Personnel
  • Difference Between Ex-showroom Price and On-road Price
fb-follow youtube follow

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