问题描述
有人可以向我解释为什么输出为False,False而不是False,True吗?
Could someone explain to me why the output is False, False and not False, True?
感谢任何提示.
public class Program
{
static void Main(string[] args)
{
var user = new User() { Nom = "Clement" };
Console.WriteLine("Avant: " + user.Panier.EstRegle);
user.Panier.Regle();
Console.WriteLine("Apres: " + user.Panier.EstRegle);
Console.ReadKey();
}
public struct Panier
{
public bool EstRegle;
public void Regle()
{
EstRegle = true;
}
}
public class User
{
public string Nom { get; set; }
public Panier Panier { get; set; }
public User()
{
this.Panier = new TestConsole.Program.Panier();
}
}
}
MR
MR
推荐答案
public struct Panier
{
public bool EstRegle;
public void Regle()
{
EstRegle = true;
}
}
因为结构的行为与C#中的类不同.
如果Panier是类而不是结构,那么您会看到
假"和"True".
11.3类和结构上的差异
https://msdn.microsoft.com/en-us/library/aa664471 (v = vs.71).aspx
类和结构(C#编程指南)
https://msdn.microsoft.com/en-us/library/ms173109.aspx
在类和结构之间进行选择
https://msdn.microsoft.com/en-us/library/ms229017 (v = vs.110).aspx
-韦恩
Because structs behave differently than classes in C#.
If Panier were a class instead of a struct you would see
"False" and "True".
11.3 Class and struct differences
https://msdn.microsoft.com/en-us/library/aa664471(v=vs.71).aspx
Classes and Structs (C# Programming Guide)
https://msdn.microsoft.com/en-us/library/ms173109.aspx
Choosing Between Class and Struct
https://msdn.microsoft.com/en-us/library/ms229017(v=vs.110).aspx
- Wayne
这篇关于使用结构的示例程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!