草庐IT

equality-operator

全部标签

c# - 字符串比较 - strA.ToLower()==strB.ToLower() 或 strA.Equals(strB,StringComparisonType)?

根据标题,您使用什么字符串比较实践,为什么? 最佳答案 您没有指定平台,但我猜是.NET。我强烈建议您使用后一种形式——因为案例比较并不像您预期​​的那么简单。(它也避免了创建额外的字符串,但这是另一回事。)例如,当代码在土耳其运行时出现“mail”和“MAIL”时,您希望您的代码执行什么操作?如果您使用ToLower它将返回false,同样如果您使用CurrentCultureIgnoreCase-但如果您使用InvariantCultureIgnoreCase它将返回true。您需要考虑数据的来源以及您尝试使用它实现的目标。参见

c# - System.Net.Mail.SmtpException : The operation has timed out. 错误在asp.net发送邮件代码使用godaddy托管

我正在使用以下代码和平使用godaddy托管发送邮件。但它抛出System.Net.Mail.SmtpException:Theoperationhastimedout.protectedvoidsendmail(){varfromAddress="frommailid@site.com";//anyaddresswheretheemailwillbesendingvartoAddress="to@gmail.com";//PasswordofyourgmailaddressconststringfromPassword="mypassword";//Passingthevaluesa

c# - LinqToSQL 错误 : Operation is not valid due to the current state of the object

在更新命令期间,我收到以下错误:Operationisnotvalidduetothecurrentstateoftheobject我试图从更新命令中删除一列并且它工作正常。此列是一个FK,与其他工作正常的FK相似。这是执行更新的代码:ti.NumeroTitolo=titolo.Numero;ti.RKTipoTitoloGenereTitolo=titolo.RkTipoTitoloGenereTitolo;ti.RKBanca=titolo.RkBanca;ti.DataScadenza=titolo.DataScadenza;ti.RKTipoEsito=titolo.RkTi

c# - 为什么 is-operator 会导致不必要的装箱?

documentation与is运算符(exprisconstant)的常量模式匹配状态:Theconstantexpressionisevaluatedasfollows:Ifexprandconstantareintegraltypes,theC#equalityoperatordetermineswhethertheexpressionreturnstrue(thatis,whetherexpr==constant).Otherwise,thevalueoftheexpressionisdeterminedbyacalltothestaticObject.Equals(expr,

c# - 无法使用 EFCore、EntityState.Modified : "Database operation expected to affect 1 row(s) but actually affected 0 row(s)." 编辑数据库条目

我将IdentityCore1.0与ASP.NETMVCCore1.0和EntityFrameworkCore1.0结合使用来创建一个简单的用户注册系统thisarticle作为起点,我正在尝试添加用户角色。我可以添加用户角色,但无法编辑它们。这是RolesController中的Edit操作:[HttpPost][ValidateAntiForgeryToken]publicIActionResultEdit(IdentityRolerole){try{_db.Roles.Attach(role);_db.Entry(role).State=Microsoft.EntityFrame

c# - 为什么在添加到集合时不为所有对象调用 Equals()

我有一个类型,我将其用作IDictionary中的键。类型如下publicclassEmployee{publicstringName{get;set;}publicintID{get;set;}publicoverrideboolEquals(objectobj){Employeeemp=objasEmployee;if(emp!=null)returnemp.Name.Equals(this.Name);returnfalse;}publicoverrideintGetHashCode(){returnthis.Name.GetHashCode();}}现在我已经创建了一个字典,如

c# - 在泛型函数中使用重载的 operator==

考虑以下代码:classCustomClass{publicCustomClass(stringvalue){m_value=value;}publicstaticbooloperator==(CustomClassa,CustomClassb){returna.m_value==b.m_value;}publicstaticbooloperator!=(CustomClassa,CustomClassb){returna.m_value!=b.m_value;}publicoverrideboolEquals(objecto){returnm_value==(oasCustomCla

c# - Equals 和 GetHashCode 方法不一致

读完这个问题Whydo"int"and"sbyte"GetHashCodefunctionsgeneratedifferentvalues?我想进一步挖掘并发现以下行为:sbytei=1;intj=1;object.Equals(i,j)//false(1)object.Equals(j,i)//false(2)i.Equals(j)//false(3)j.Equals(i)//true(4)i==j//true(5)j==i//true(6)i.GetHashCode()==j.GetHashCode()//false(7)(3)和(4)之间的差异打破了Equals应该对称的要求。(

c# - 如何在实体数据模型创建的对象上覆盖 Equals?

我有一个我创建的实体数据模型,它从SQLite数据库中提取记录。其中一个表是People,我想重写person.Equals()方法,但我不确定去哪里进行这样的更改,因为Person对象是自动生成的,我什至看不到autogen代码在哪里居住。我知道如何在手工制作的对象上覆盖Equals,它只是在自动生成的对象上执行此操作的位置。 最佳答案 您需要创建一个分部类。向您的解决方案添加一个新的.cs文件,然后像这样启动它:publicpartialclassPerson{publicoverrideboolEquals(Objectobj

c# - "The binary operator Add is not defined for the types ' System.String ' and ' System.String '."-- 真的吗?

尝试运行以下代码时:Expression>stringExpression=Expression.Lambda>(Expression.Add(stringParam,Expression.Constant("A")),newList(){stringParam});stringAB=stringExpression.Compile()("B");我收到标题中提到的错误:“二元运算符Add没有为类型‘System.String’和‘System.String’定义。”真的是这样吗?显然在C#中它有效。在C#中执行strings="A"+"B"是表达式编译器无法访问的特殊语法糖吗?