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

Key Differences

Know the Differences & Comparisons

Difference Between ref and out parameter in c#

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.

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



New Additions

  • Difference Between Deforestation, Reforestation and Afforestation
  • Difference Between Race and Ethnicity
  • Difference Between Customer Service and Customer Experience
  • Difference Between Human Capital and Human Development
  • Difference Between Systems Approach and Contingency Approach
  • Difference Between Industrial Relations and Employee Relations
  • Difference Between Introduction and Executive Summary
  • Difference Between Programmed and Non-programmed Decisions
  • Difference Between Depreciation and Devaluation of Currency
  • Difference Between Consumer Market and Business Market

Categories

  • Banking
  • Business
  • Education
  • English
  • Finance
  • General
  • IT
  • Law
  • Science
fb-follow youtube follow

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