您的位置:首页 > 其它

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

2012-01-19 16:42 369 查看
新建一个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!");
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: