
正文
C#中关键字ref修饰类对象或结构体[转]
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
using System;
using System.Collections.Generic;
using System.Text;
namespace CSharpTest
{
struct Dog
{
public int _nFeet;
public string _sound;
public Dog(int n, string sound)
{
_nFeet = n;
_sound = sound;
}
public int NFeet
{
get { return _nFeet; }
set { _nFeet = value; }
}
public string Sound
{
get { return _sound; }
set { _sound = value; }
}
public string ToString()
{
return String.Format("The dog has {0} feet, and sounds {1}.",
_nFeet.ToString(), _sound);
}
}
class Cat
{
private int _nFeet;
private string _sound;
public Cat(int n, string sound)
{
_nFeet = n;
_sound = sound;
}
public int NFeet
{
get { return _nFeet; }
set { _nFeet = value; }
}
public string Sound
{
get { return _sound; }
set { _sound = value; }
}
public string ToString()
{
return String.Format("The cat has {0} feet, and sounds {1}.",
_nFeet.ToString(), _sound);
}
}
class Program
{
static void Main(string[] args)
{
// Cat
Cat cat = new Cat(, "miao~");
System.Console.WriteLine(cat.ToString());
BeatCat(cat);
System.Console.WriteLine(cat.ToString());
BeatCat2(ref cat);
System.Console.WriteLine(cat.ToString());
System.Console.WriteLine();
// Dog
Dog dog = new Dog(, "wang!");
System.Console.WriteLine(dog.ToString());
BeatDog(dog);
System.Console.WriteLine(dog.ToString());
BeatDog2(ref dog);
System.Console.WriteLine(dog.ToString());
}
public static void BeatCat(Cat cat)
{
cat.NFeet = ;
cat.Sound = "wang~";
}
public static void BeatCat2(ref Cat cat)
{
cat.NFeet = ;
cat.Sound = "huhu~";
}
public static void BeatDog(Dog dog)
{
dog.NFeet = ;
dog.Sound = "miao!";
}
public static void BeatDog2(ref Dog dog)
{
dog.NFeet = ;
dog.Sound = "huhu!";
}
}
}







