Difference between ref and out parameters

Original link: http://www.dotnet-tricks.com/Tutorial/csharp/K0Hb060414-Difference-between-ref-and-out-parameters.html

Following content is directly reprinted from above link, please go to the original link for more details.

Difference between ref and out parameters

Author: Shailendra Chauhan

Ref and out parameters are used to pass an argument within a method. In this article, you will learn the differences between these two parameters.

Ref

The ref keyword is used to pass an argument as a reference. This means that when value of that parameter is changed in the method, it gets reflected in the calling method. An argument that is passed using a ref keyword must be initialized in the calling method before it is passed to the called method.

Out

The out keyword is also used to pass an argument like ref keyword, but the argument can be passed without assigning any value to it. An argument that is passed using an out keyword must be initialized in the called method before it returns back to calling method.

Program with ref and out keyword

        public static void Main() //calling method
        {
            int val1 = 0; //must be initialized
            int val2; //optional

            Example1(ref val1);
            Console.WriteLine(val1); // val1=1

            Example2(out val2);
            Console.WriteLine(val2); // val2=2
        }

        static void Example1(ref int value) //called method
        {
            value = 1;
        }
        static void Example2(out int value) //called method
        {
            value = 2; //must be initialized
        }
    /* Output
     1
     2

Note

  1. Do not be confused with the concept of passing by reference and the concept of reference type. These two concepts are not the same.
  2. A value type or a reference type can be passed to method parameter by using ref keyword. There is no boxing of a value type when it is passed by reference.
  3. Properties cannot be passed to ref or out parameters since internally they are functions and not members/variables.

Ref and out in method overloading

Both ref and out cannot be used in method overloading simultaneously. However, ref and out are treated differently at run-time but they are treated same at compile time (CLR doesn‘t differentiates between the two while it created IL for ref and out). Hence methods cannot be overloaded when one method takes a ref parameter and other method takes an out parameter. The following two methods are identical in terms of compilation.

       public void Method(out int a) // compiler error “cannot define overloaded”
        {
            // method that differ only on ref and out"
            a = 0; //must be initialized
        }
        public void Method(ref int a)
        {
            // method that differ only on ref and out"
        }

However, method overloading can be done, if one method takes a ref or out argument and the other method takes simple argument. The following example is perfectly valid to be overloaded.

class MyClass
        {
            public void Method(int a)
            {

            }
            public void Method(out int a)
            {
                // method differ in signature.
                a = 0; //must be initialized
            }
        }
What do you think?

I hope you will enjoy the ref and out keywords while programming with C#. I would like to have feedback from my blog readers. Your valuable feedback, question, or comments about this article are always welcome.

时间: 2024-10-25 00:08:01

Difference between ref and out parameters的相关文章

PHP时间日期比较

若要使用PHP来比较日期,最好用DateTime::diff 但是这个是5.3才支持的,如果没有这样的环境,可以使用<.>来比较 如下例子,会输出right $date1=strtotime('2009-8-9 20:00'); $date2=strtotime('2009-9-9 1:00'); if($date1<$date2) echo 'right'; else echo 'wrong'; 注:DateTime::diff DateTime::diff (PHP 5 >=

使用 Swagger 文档化和定义 RESTful API

大部分 Web 应用程序都支持 RESTful API,但不同于 SOAP API——REST API 依赖于 HTTP 方法,缺少与 Web 服务描述语言(Web Services Description Language,WSDL)类似的语言来定义使用者与提供者之间的请求和响应结构.由于没有充分的合同服务,许多 REST API 提供者使用 Microsoft Word 文档或维基页面来记录 API 用法.这些格式使协作和文档版本控制变得很困难,尤其对于有许多 API 或资源的应用程序,或者

SWAGGER SPECIFICATION

OpenAPI Specification (fka Swagger RESTful API Documentation Specification) Version 2.0 The key words "MUST", "MUST NOT", "REQUIRED", "SHALL", "SHALL NOT", "SHOULD", "SHOULD NOT", "

C# Study Note: Values and References

1.   Reference Type VS Value Type Most primitive types such as int, float, double, char…(NOT string) are value types. When you declare a value type variable, the compiler generate codes to allocate a block of memory for the variable. Class types are

【11g体系结构,6】参数文件和一些参数

一.参数文件的作用: 参数文件记录了数据库的配置.在数据库启动时,Oracle 要根据参数文件中的参数设置, 配置数据库.如要为各个内存池分配多少内存,允许打开的进程数和会话等等.要想让数 据库启动,必需先读取参数文件.参数文件中的参数,我们通常称其为初始化参数,简称就 是参数. 二.参数文件共有两种参数文件 参数文件包含pfile 和spfile.区别如下: 1.PFILE    为文本文件,可以使用vi编辑器进行修改,可以放在客户端和服务端. 文件名为:init<sid>.ora. 从or

ABAP 自建透明表维护

*&---------------------------------------------------------------------* *& Report  ZMMR011 *& *&---------------------------------------------------------------------* *&程序名称:供应商.工厂对应公里数维护 *&事物代码:ZMM031 *&导入模板:无 *&作者:董冬 *&a

ABAP 供应商、工厂对应公里数维护

*&---------------------------------------------------------------------* *& Report  ZMMR011 *& *&---------------------------------------------------------------------* *&程序名称:供应商.工厂对应公里数维护 *&事物代码:ZMM031 *&导入模板:无 *&作者:董冬 *&a

挑战一下吧!C#测试开发工程师英语面试题

1. Given a rectangular (cuboidal for the puritans) cake with a rectangular piece removed (any size or orientation), how would you cut the remainder of the cake into two equal halves with one straight cut of a knife?2. Given an array of size N in whic

【翻译】JavaScript中5个值得被广泛使用的数组方法

原文地址:http://colintoh.com/blog/5-array-methods-that-you-should-use-today?utm_source=javascriptweekly&utm_medium=email 在2009年十月ECMAScript 5被定义正式规范以来,一些可以提高工作效率的数组方法被提出.但是,由于ES5糟糕的浏览器支持率,这些方法并未被开发者广泛使用. “多余的”数组方法 没有人会质疑这些方法的可用性,但是如果针对它们而编写polyfill(关于pol