练习使用ref, out关键字来传递class, struct, int, double的数据

新建一个Cat 类

class Cat
    {
        private string _sound;

        //========================================================================
        //  コンストラクタ名 : Cat
        /// <summary>
        /// Cat构造函数
        /// </summary>
        /// <remarks>
        /// 给Cat建一个有参数的构造函数
        /// </remarks>
        /// <param name="sound">辅助变量</param>
        //========================================================================
        public Cat(string sound)
        {
            _sound = sound;
        }

        /// <summary>soundを設定/取得する</summary>
        /// <value>value</value>
        public string Sound
        {
            get { return _sound; }
            set { _sound = value; }
        }

        //========================================================================
        //  メソッド名 : ToSound
        /// <summary>
        /// ToSound
        /// </summary>
        /// <remarks>
        /// 给Cat设置声音
        /// </remarks>
        /// <returns></returns>
        //========================================================================
        public string ToSound()
        {
            return String.Format("The cat's sounds {0}.", _sound);
        }
    }


新建一个dog结构体

 struct Dog
    {
        /// <summary></summary>
        public string _sound;

        //========================================================================
        //  コンストラクタ名 : Dog
        /// <summary>
        /// Dog构造函数
        /// </summary>
        /// <remarks>
        /// 给Dog建一个有参数的构造函数
        /// </remarks>
        /// <param name="sound">辅助变量</param>
        //========================================================================
        public Dog(string sound)
        {
            _sound = sound;
        }

        /// <summary>soundを設定/取得する</summary>
        /// <value>value</value>
        public string Sound
        {
            get { return _sound; }
            set { _sound = value; }
        }

        //========================================================================
        //  メソッド名 : ToSound
        /// <summary>
        /// ToSound方法
        /// </summary>
        /// <remarks>
        /// 给Dog设置声音
        /// </remarks>
        /// <returns></returns>
        //========================================================================
        public string ToSound()
        {
            return String.Format("The dog's sounds {0}.", _sound);
        }
    }


实现

using System;

namespace RefAndOutPractice
{
    //========================================================================
    //  クラス名 : RefAndOutPractice
    /// <summary>
    /// RefAndOutPractice
    /// </summary>
    /// <remarks>
    /// 练习使用ref, out关键字来传递class, struct, int, double的数据
    /// </remarks>
    //========================================================================
    class RefAndOutPractice
    {
        //========================================================================
        //  メソッド名 : Main
        //========================================================================
        static void Main(string[] args)
        {
            Console.WriteLine("Class And Struct!");
            Cat cat = new Cat("miao~");
            Console.WriteLine(cat.ToSound());

            BeatCat(cat);
            Console.WriteLine(cat.ToSound());

            BeatCatRef(ref cat);
            Console.WriteLine(cat.ToSound());

            BeatCatOut(out cat);
            Console.WriteLine("catout:{0}", cat);
            Console.WriteLine();

            Dog dog = new Dog("wang!");
            Console.WriteLine(dog.ToSound());

            BeatDog(dog);
            Console.WriteLine(dog.ToSound());

            BeatDogRef(ref dog);
            Console.WriteLine(dog.ToSound());

            BeatDogOut(out dog);
            Console.WriteLine("BeatDogOut:{0}", dog);
            Console.WriteLine();

            Console.WriteLine("Int And Double!");
            int i = 1;
            int j = 2;
            SwapNoRef(i, j);
            Console.WriteLine("i={0},j={1}", i, j);

            SwapRef(ref i, ref j);
            Console.WriteLine("i={0},j={1}", i, j);

            double de1 = 3.25654555;
            double de2 = 4.56112324;
            DoubleNoRef(de1, de2);
            Console.WriteLine("de1={0},de2={1}", de1, de2);

            DoubleRef(ref de1, ref de2);
            Console.WriteLine("de1={0},de2={1}", de1, de2);

            OutFunction(out i, out de1);
            Console.WriteLine("i={0},de1={1}", i, de1);
        }

        //========================================================================
        //  メソッド名 : SwapNoRef
        /// <summary>
        /// *值参数
        /// *当利用值向方法传递参数时,编译程序给实参的值做一份拷贝,
        /// *并且将此拷贝传递给该方法。被调用的方法不会修改内存中实参的值,
        /// *所以使用值参数时,可以保证实际值是安全的。
        /// </summary>
        /// <param name="x">辅助变量</param>
        /// <param name="y">辅助变量</param>
        //========================================================================
        static void SwapNoRef(int x, int y)
        {
            int temp = x;
            x = y;
            y = temp;
        }

        //========================================================================
        //  メソッド名 : SwapRef
        /// <summary>
        ///  ref
        /// 引用型参数
        /// 和至参数不同的是,引用型参数并不开辟新的内存区域。
        /// 当利用引用型参数向方法传递形参时,编译程序将把实际值在内存中的地址传递给方法。
        /// </summary>
        /// <param name="x">辅助变量</param>
        /// <param name="y">辅助变量</param>
        //========================================================================
        static void SwapRef(ref int x, ref int y)
        {
            int temp = x;
            x = y;
            y = temp;
        }

        //========================================================================
        //  メソッド名 : DoubleNoRef
        /// <summary>
        /// DoubleNoRef
        /// </summary>
        /// <remarks>
        /// Double型数值值参数
        /// </remarks>
        //========================================================================
        static void DoubleNoRef(double d1, double d2)
        {
            double d = d1;
            d1 = d2;
            d2 = d;
        }

        //========================================================================
        //  メソッド名 : DoubleRef
        /// <summary>
        /// DoubleRef
        /// </summary>
        /// <remarks>
        /// Double型数值Ref引用型参数
        /// </remarks>
        //========================================================================
        static void DoubleRef(ref double d1, ref double d2)
        {
            double d = d1;
            d1 = d2;
            d2 = d;
        }

        //========================================================================
        //  メソッド名 : OutFunction
        /// <summary>
        /// /*
        /// * out
        /// * 输出参数
        /// * 与引用类型参数类似,输出型参数也不开辟新的内存区域。
        /// * 输出参数与引用型参数的差别在于,调用方法前无需对变量进行初始化。
        /// * 输出型参数用于传递方法返回的数据。
        /// * out修饰符后应跟与形参的类型相同的类型申明。在方法返回后,传递的变量被认为经过了初始化。
        /// */
        /// </summary>
        //========================================================================
        static void OutFunction(out int i, out double de1)
        {
            i = 100;
            de1 = 4.18;
        }

        //========================================================================
        //  メソッド名 : BeatCat
        /// <summary>
        /// BeatCat
        /// </summary>
        /// <remarks>
        /// 简单的传递Class
        /// </remarks>
        //========================================================================
        static void BeatCat(Cat cat)
        {
            cat.Sound = "BeatCat miao ~";
        }

        //========================================================================
        //  メソッド名 : BeatCatRef
        /// <summary>
        /// BeatCatRef
        /// </summary>
        /// <remarks>
        /// 利用ref传递Class
        /// </remarks>
        //========================================================================
        static void BeatCatRef(ref Cat cat)
        {
            cat.Sound = "BeatCat2 huhu ~";
        }

        //========================================================================
        //  メソッド名 : BeatCatOut
        /// <summary>
        /// BeatCatOut
        /// </summary>
        /// <remarks>
        /// 利用out传递Class
        /// </remarks>
        //========================================================================
        static void BeatCatOut(out Cat cat)
        {
            cat = new Cat("BeatCatOut miao ~");
        }

        //========================================================================
        //  メソッド名 : BeatDog
        /// <summary>
        /// BeatDog
        /// </summary>
        /// <remarks>
        /// 简单传递Struct
        /// </remarks>
        //========================================================================
        static void BeatDog(Dog dog)
        {
            dog.Sound = "BeatDog wang !";
        }

        //========================================================================
        //  メソッド名 : BeatDogRef
        /// <summary>
        /// BeatDogRef
        /// </summary>
        /// <remarks>
        /// 利用Ref传递Struct
        /// </remarks>
        //========================================================================
        static void BeatDogRef(ref Dog dog)
        {
            dog.Sound = "BeatDog2 huhu !";
        }

        //========================================================================
        //  メソッド名 : BeatDogOut
        /// <summary>
        /// BeatDogOut
        /// </summary>
        /// <remarks>
        /// 利用out传递Struct
        /// </remarks>
        //========================================================================
        static void BeatDogOut(out Dog dog)
        {
            dog = new Dog("BeatDogOut wang!");
        }
    }
}


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值