鉴于以下课程
public class Foo
{
public int FooId { get; set; }
public string FooName { get; set; }
public override bool Equals(object obj)
{
Foo fooItem = obj as Foo;
if (fooItem == null)
{
return false;
}
return fooItem.FooId == this.FooId;
}
public override int GetHashCode()
{
// Which is preferred?
return base.GetHashCode();
//return this.FooId.GetHashCode();
}
}
Run Code Online (Sandbox Code Playgroud)
我已经覆盖了该Equals
方法,因为它Foo
代表了Foo
s表的一行.哪个是覆盖的首选方法GetHashCode
?
覆盖为什么重要GetHashCode
?
我在Silverlight应用程序中有一个比较2个字符串的条件,由于某种原因,当我使用==
它时返回false而.Equals()
返回true.
这是代码:
if (((ListBoxItem)lstBaseMenu.SelectedItem).Content.Equals("Energy Attack"))
{
// Execute code
}
if (((ListBoxItem)lstBaseMenu.SelectedItem).Content == "Energy Attack")
{
// Execute code
}
Run Code Online (Sandbox Code Playgroud)
任何理由为什么会这样?
有人可以解释一下这个重载是什么意思吗?
public static bool operator ==(Shop lhs, Shop rhs)
{
if (Object.ReferenceEquals(lhs, null))
{
if (Object.ReferenceEquals(rhs, null))
{
return true;
}
return false;
}
return lhs.Equals(rhs);
}
Run Code Online (Sandbox Code Playgroud)
我从未在重载中看到过Object.ReferenceEquals
我目前在Unity中设置了两个脚本来处理一些UI音频.一个是经理,另一个是为特定的UI元素播放声音.我所拥有的简化版本如下:
public class AudioUIManager : MonoBehaviour //Only one of these in the scene
{
public AudioClip genericUISound; //This is set in the inspector.
}
Run Code Online (Sandbox Code Playgroud)
public class AudioUITextAnimation : MonoBehaviour
{
[SerializeField]
private AudioClip specifiedUISound; //This is not set in the inspector
[SerializeField]
private AudioUIManager audioUIManager; // I get a reference to this elsewhere
void Start()
{
//Use generic sounds from audio manager if nothing is specified.
specifiedUISound = specifiedUISound ?? audioUIManager.genericUISound;
print(specifiedUISound);
}
}
Run Code Online (Sandbox Code Playgroud)
我在这里想要实现的是让该specifiedUISound
字段使用在检查器中分配给它的声音.如果未分配声音,则使用UI管理器中的通用声音.这样可以节省我为需要相同声音的数百万个按钮分配相同的声音,但如果我愿意,可以选择为一个按钮设置特定的声音.
然而,在空coalessing算asigning null …
c# null serialization null-coalescing-operator unity-game-engine
如图所示 这里,创建在运行时不能被拘留的字符串。
但是,以下代码:
class Program {
static void Main(string[] args)
{
string s1 = "Programming Is Fun";
string s3 = s1.ToString();
Console.WriteLine(Object.ReferenceEquals(s1, s3));
}
}
Run Code Online (Sandbox Code Playgroud)
给出(VS 2015):
True
Run Code Online (Sandbox Code Playgroud)
那么,它是否以某种方式指定了在运行时生成哪些字符串?
顺便提一句:
编码:
using System;
using System.Text;
class Program {
static void Main(string[] args)
{
string s1 = "hop";
StringBuilder s2 = new StringBuilder(s1);
string s3 = s2.ToString();
Console.WriteLine(Object.ReferenceEquals(s1, s3));
}
}
Run Code Online (Sandbox Code Playgroud)
给出(VS 2015):
False
Run Code Online (Sandbox Code Playgroud)
与单声道(版本 4.0.2)相反,它给出
True
Run Code Online (Sandbox Code Playgroud)
请参阅工作示例。
如何比较一个列表中的对象?我重载了==
比较两个字符串的运算符:
public static bool operator ==(User one, User two)
{
return one.Email == two.Email;
}
Run Code Online (Sandbox Code Playgroud)
我应该通过列表比较它们之间的比较.我已经提出了一个解决方案,它完成了这项工作,但我希望有更好的方法来实现这一点,使用LINQ或lambda表达式.
foreach (User u in up)
{
foreach (User u2 in up)
{
if (ReferenceEquals(u, u2)) continue;
if (u == u2) Console.WriteLine("Users {0} and {1} have the same mail.", u.ToString(), u2.ToString());
}
}
Run Code Online (Sandbox Code Playgroud) 我用C#编写了这个小程序
private void Form1_Load(object sender, EventArgs e)
{
MessageBox.Show(("7797302D875A8922EBFC7DECBD352FE88F35642F" == "?7797302D875A8922EBFC7DECBD352FE88F35642F").ToString());
var a = "7797302D875A8922EBFC7DECBD352FE88F35642F";
var b = "7797302D875A8922EBFC7DECBD352FE88F35642F";
MessageBox.Show((a == b).ToString());
}
Run Code Online (Sandbox Code Playgroud)
第一个messageBox显示"False",而Messagebox显示"True".
我的问题是:为什么我不能将这两个字符串与==
运算符进行比较?
== &
Referencequals 之间存在一些差异
看下面的代码:
string s1 = "1";
string s2 = string.Copy("1");
Console.WriteLine(object.ReferenceEquals(s1, s2));
Console.WriteLine(s1 == s2);
Console.Read();
Run Code Online (Sandbox Code Playgroud)
输出是:
False
True
Run Code Online (Sandbox Code Playgroud)
任何人都能解释这种行为吗?