对c#中的按引用传递和按值传递感到困惑
confused about Passing by reference and passing by value in c#
我是这里的新程序员。我有以下代码。我按值传递了对象,但是当我打印结果时,我得到了这个
elf attacked orc for 20 damage!
Current Health for orc is 80
elf attacked orc for 20 damage!
Current Health for orc is 80
这让我对按引用传递感到困惑,因为自从我按值传递对象后,我没想到主要的健康状况是 80。有人可以解释程序主要功能中的健康结果是 80 而不是 100 吗?
//MAIN class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace passingTest
{
class Program
{
static void Main(string[] args)
{
// Enemy Objects
Enemy elf = new Enemy("elf", 100, 1, 20);
Enemy orc = new Enemy("orc", 100, 1, 20);
elf.Attack(orc);
Console.WriteLine("{0} attacked {1} for {2} damage!", elf.Nm, orc.Nm, elf.Wpn);
Console.WriteLine("Current Health for {0} is {1}", orc.Nm, orc.Hlth);
Console.ReadLine();
}
}
}
// Enemy Class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace passingTest
{
class Enemy
{
// variables
string nm = "";
int hlth = 0;
int lvl = 0;
int wpn = 0;
public string Nm
{
get { return nm; }
set { nm = value; }
}
public int Wpn
{
get { return wpn; }
set { wpn = value; }
}
public int Hlth
{
get { return hlth; }
set { hlth = value; }
}
public Enemy(string name, int health, int level, int weapon)
{
nm = name;
hlth = health;
lvl = level;
wpn = weapon;
}
public void Attack(Enemy rival){
rival.hlth -= this.wpn;
Console.WriteLine("{0} attacked {1} for {2} damage!", this.nm, rival.nm, this.wpn);
Console.WriteLine("Current Health for {0} is {1}", rival.nm, rival.hlth);
}
}
}
在C#/.NET中,对象是按引用传递还是按值传递是由对象的类型决定的。如果对象是引用类型(即用 class
声明),则通过引用传递。如果对象是值类型(即用 struct
声明),则按值传递。
如果将 Enemy 的声明更改为
struct Enemy
您将看到按值传递语义。
在 C# 中,类 被视为引用类型。引用类型是一种类型,其值是对适当数据的引用而不是数据本身。例如,考虑以下代码:
这里是 link,其中包含有关该主题的更多信息:http://jonskeet.uk/csharp/parameters.html
我是这里的新程序员。我有以下代码。我按值传递了对象,但是当我打印结果时,我得到了这个
elf attacked orc for 20 damage!
Current Health for orc is 80
elf attacked orc for 20 damage!
Current Health for orc is 80
这让我对按引用传递感到困惑,因为自从我按值传递对象后,我没想到主要的健康状况是 80。有人可以解释程序主要功能中的健康结果是 80 而不是 100 吗?
//MAIN class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace passingTest
{
class Program
{
static void Main(string[] args)
{
// Enemy Objects
Enemy elf = new Enemy("elf", 100, 1, 20);
Enemy orc = new Enemy("orc", 100, 1, 20);
elf.Attack(orc);
Console.WriteLine("{0} attacked {1} for {2} damage!", elf.Nm, orc.Nm, elf.Wpn);
Console.WriteLine("Current Health for {0} is {1}", orc.Nm, orc.Hlth);
Console.ReadLine();
}
}
}
// Enemy Class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace passingTest
{
class Enemy
{
// variables
string nm = "";
int hlth = 0;
int lvl = 0;
int wpn = 0;
public string Nm
{
get { return nm; }
set { nm = value; }
}
public int Wpn
{
get { return wpn; }
set { wpn = value; }
}
public int Hlth
{
get { return hlth; }
set { hlth = value; }
}
public Enemy(string name, int health, int level, int weapon)
{
nm = name;
hlth = health;
lvl = level;
wpn = weapon;
}
public void Attack(Enemy rival){
rival.hlth -= this.wpn;
Console.WriteLine("{0} attacked {1} for {2} damage!", this.nm, rival.nm, this.wpn);
Console.WriteLine("Current Health for {0} is {1}", rival.nm, rival.hlth);
}
}
}
在C#/.NET中,对象是按引用传递还是按值传递是由对象的类型决定的。如果对象是引用类型(即用 class
声明),则通过引用传递。如果对象是值类型(即用 struct
声明),则按值传递。
如果将 Enemy 的声明更改为
struct Enemy
您将看到按值传递语义。
在 C# 中,类 被视为引用类型。引用类型是一种类型,其值是对适当数据的引用而不是数据本身。例如,考虑以下代码:
这里是 link,其中包含有关该主题的更多信息:http://jonskeet.uk/csharp/parameters.html