zoukankan      html  css  js  c++  java
  • C#引用类型(class)和值类型(struct)

    1. 值参数

      当利用值向方法传递参数时,编译程序给实参的值做一份拷贝,并且将此拷贝传递给该方法。被调用的方法不传内存中实参的值,所以使用值参数时,可以保证实际值是安全的。

      

    using System;
    class Test
    {
    static void Swap(int x,int y){
    int temp=x;
    x=y;
    y=temp;
    }
    static void Main(){
    int i=1,j=2;
    Swap(i,j);
    Console.WriteLine("i={0},j={1}",i,j);
    }
    }

      i=1,j=2

    2. 引用型参数

      和值参不同的是,引用型参数并不开辟新的内存区域。当利用引用型参数向方法传递形参时,编译程序将把实际值在内存中的地址传递给方法。

      

    using System;
    class Test
    {
    static void Swap(ref int x,ref int y){
    int temp=x;
    x=y;
    y=temp;
    }
    static void Main(){
    int i=1,j=2;
    Swap(ref i,ref j);
    Console.WriteLine("i={0},j={1}",i,j);
    }
    }

      i=2,j=1

  • 相关阅读:
    算法设计--求连续子向量的最大和问题--论想法思路的重要性
    --a和a--
    程序员一个知道的一些法则
    django admin
    Python递归
    Python内置函数
    Python协程函数
    Python 生成器
    Python迭代器
    Python装饰器
  • 原文地址:https://www.cnblogs.com/WebApp-DotNet/p/6237663.html
Copyright © 2011-2022 走看看