C# 运算符 技术教程文章

MVC c# 没有为类型“System.Nullable`1[System.Int32]”和“System.Int32”定义二进制运算符 Equal。

Entity framework 运用过程中,条件查询时会出现 MVC c# 没有为类型“System.Nullable`1[System.Int32]”和“System.Int32”定义二进制运算符 Equal。 处理方法:int类型进行连接是, 非空和非零的前提下才进行Equal,所以尝试转换成int成功就可以处理掉这个异常。 int UserId ; if (int.TryParse(Request["UserId"], out UserId)) { where.Equal("UserId", UserId); ...

c# -- is和as运算符【代码】

一、is运算符用于判断一个对象是否可以转换为指定的类型,不会抛出异常,返回bool值用来表示是否转换成功:if (myObj is MyClass) {//do... }  当满足以下任意一种情况时转换成功:  1.转换对象是目标类型的实例;  2.转换对象是派生自目标类型的类型的实例;  3.转换对象是实现目标接口的类型的实例;  在C#7.0以上版本中支持is模式匹配表达式和switch模式匹配表达式;  二、as运算符与强制转换类似,但不会抛出异常,...

四、C#入门—表达式与运算符

四、C#表达式与运算符  4.1.表达式    操作数+运算符     4.2.数学运算符    var1=+var2  var1乘以1    var1=-var2  var1乘以-1    var++  先用后加    ++var  先加后用          4.3.赋值运算符    略   4.4.关系运算符    结果只会是bool类型    1)对象的不同      数值类型比较两个数的大小      字符类比较Unicode编码大小,‘A‘=65 ‘a‘=97 ‘0‘...

为什么C#不接受布尔运算符作为当前运算符的单词(“and”,“或”,“not”,…等)?【代码】

我的if stetement:if (!string.IsNullOrEmpty(Person.Name))// some code我觉得 ”!”编写命令式代码时,运算符的可读性较差, 你是否同意我的以下代码更具可读性?if (not string.IsNullOrEmpty(Person.Name))// some code我不是在谈论替换当前的运算符,但是如果语言接受它们的话,问题出在哪里?解决方法:有几个很好的理由: >有一些细节,这些词语传达得不好.例如,短路AND运算符与不短路的运算符(&& vs&). VB最近(ish)推出了一个名...

C#Null条件运算符替代(条件赋值)?【代码】

C# null-conditional operator允许有用的短路:double? range = (unit as RangedUnit)?.WeaponRange;遗憾的是,无条件运算符不能以相同的方式用于短手赋值,因为它返回一个值(不能在左手赋值中使用):(unit as RangedUnit)?.PreferredTarget = UnitType.Melee;导致可能的替代语法:if (unit is RangedUnit) {(unit as RangedUnit).PreferredTarget = UnitType.Melee; }如果编译器知道RangedUnit是引用类型(不是值类型),为什么它不能有...

c# – 如何在没有任何条件语句或运算符的情况下编写条件?【代码】

我的计算机老师要我编写代??码来连接两个字符串(以一种奇怪的方式).代码应该是这样的,如果两个字符串的长度相等,那么输出应该是(string1 string2).否则输出应该是长度更大的字符串.挑战是我不应该使用if else语句或条件?exp1:exp2.这是我能够提出的(a和b是输入字符串的名称):int aLen = a.Length; int bLen = b.Length; //+1 is added to lengths to prevent divide by zero int bGreatFlag = ((aLen+1) % (bLen + 1)) / (aLen...

c# – 我可以使用三元运算符,每个函数返回void吗?【代码】

参见英文答案 > Ternary Expression Possible? 2个在C#中可以使用没有值的if内联条件,换句话说返回void?public void FuncReturningVoid () {return; }public void AnotherFuncReturningVoid() {return; }public void Test () {int a = 1;int b = 2;// I whish I could to do this:a == b ? FuncReturningVoid() : AnotherFuncReturningVoid();//would be the same...if (a == b){FuncReturnin...

C#运算符

1 算术运算符: + 加; - 减;乘 *; 除 /;取余 %; 2 关系运算符: > 大于;<小于;>= 大于等于 ;<=小于等于;==等于;!= 不等于 3 布尔逻辑运算符:& 逻辑与(两操作数均为true,结果才为true); | 逻辑或( 两操作数均为flase 时 结果才为flase); ! 取反(列:操作数A 与 !A 相反); ^异或 (两操作数同真同假时,结果才为flase); && 条件与 (两操作...

c# – 三元运算符中的意外行为【代码】

当我将if-else更改为三元运算符以返回语句时,我遇到了一种奇怪的行为. 我在这里简化了代码:class Foo {private bool condition;private int intValue = 1;private decimal decimalValue = 1M;public object TernaryGet{get{return condition ? decimalValue : intValue;}}public object IfElseGet{get{if (condition)return decimalValue;return intValue;}}public Foo(bool condition){this.condition = condition;} }class Prog...

c# – 编译器中的错误还是误解?或短裤上的运算符【代码】

参见英文答案 > Bitwise-or operator used on a sign-extended operand in Visual Studio 2015 2个我有一行代码在VS2015中给我一个警告信息(CS0675),但在2013年没有.shortValue |= (short)anEnum;Warning CS0675 Bitwise-or operator used on a sign-extended operand;consider casting to a smaller unsigned type first. The compilerimplicitly widened and sign-extended a variable, a...

C#-是运算符-检查所有可用转换的可铸性【代码】

进一步阅读后进行编辑,修改后的问题更具体. 按照Microsoft documentation:An is expression evaluates to true if the provided expression isnon-null, and the provided object can be cast to the provided typewithout causing an exception to be thrown. Otherwise, the expressionevaluates to false.这是下面的问题.var test = (Int32)(Int16)1; // Non-null and does not cause an exception. var test2 = (Int16)1 is I...

C#类字段声明中的此运算符【代码】

这是一个纯粹的学术问题-我已经很容易找到解决方法. 在将VB.Net类移植到C#时,我遇到了一个类中的字段声明,该类使用this关键字作为new()语句中的参数.编译器说“关键字’this’在当前上下文中不可用”(VB编译器认为这种情况没有问题).我很容易解决此问题,方法是将字段的初始化移到类的构造函数上. 编辑:阅读评论后,我添加了以下代码块 public class cTransactions{private List Trans = new List();private List Archive = new ...

c#-以编程方式获取强制转换和输出运算符的类型【代码】

在C#中,给定两个输入类型,是否可以为运算符确定输出类型和隐式向上转换类型?例如,考虑表达式s i.说我有以下信息:short s; int i; Type leftType = typeof(short); Type rightType = typeof(int);我可以确定有关表达式s i的以下信息吗?Type leftUpcastType = typeof(int); Type rightUpcastType = typeof(int); Type outputType = typeof(int);我显然可以使用包含所有类型和运算符的庞大查找表来执行此操作,但是可能会有更简单的...

c# – 使用[==]运算符和.Equals()的Int,Char,Object数据类型【代码】

我对这段代码感到困惑,这两个条件有什么区别?为什么结果不一样?示例编号1 – 不具有相同值的相同数据类型,但它返回相同的true结果int value1 = 'a';char value2 ='a'; Console.WriteLine(value1 == value2);Console.WriteLine(value1.Equals(value2));示例编号2 – 它们具有相同值的相同数据类型,但它返回false&真正object obj1 = "Object One"; object obj2 = new string("Object One".ToCharArray()); Console.WriteLine(obj...

C#入门之各种运算符

C#入门之条件运算符(如<=这种) (一)条件运算符的基本运用 (二)各种应用的写法 (三)不能进行范围比较 (四)不同类型之间的比较 C#入门之逻辑运算符 (一)逻辑与(有假则假) (二)逻辑或(有真则真) (三)逻辑非 (四)混合使用优先级问题 (五)逻辑运算符的短路规则 C#入门之位运算符 (一)位与&(有0则0) int a=1;//001 int b=5;//101 int c=a&b//001转成十进制就是1 (二)位或|(有1则1) (三)异或^(相同为0...

c# – 空条件运算符【代码】

var a = b?.c.d;这个表达式不应该总是给出编译错误吗?如果b为null,则传播null值,因此c也将为null,因此也需要此运算符.据我所知,在表达式中使用此运算符会传播病毒. 但是Visual Studio 2015和Resharper都没有对我说什么,我在这里错过了什么吗?解决方法:运算符只是语法糖:MyType a = b == null ? null: b.c.d;为什么这应该抛出编译错误我不清楚.If b is null, the null value is propagated through so c will also be null and t...

c# – 为什么有赋值运算符(&=,=)但没有非赋值运算符(&,)用于短基元?【代码】

参见英文答案 > Integer summing blues, short += short problem 5个我偶然发现在小于32位的基本类型上不允许按位或算术运算的行为,但事实上允许相应的赋值操作:short BitwiseAnd(short a, short b) { return a & b; } // error short BitwiseAndAssignment(ref short a, short b) { a &= b; } // worksshort Add(short a, short b) { return a + b; } // error short...

在C#中,如何正确地重载我的类中的Equals运算符,以便Queue.Contains()工作?【代码】

我创建了一个类State.对于State对象的队列,我想测试Queue是否已经包含一个值相等的State对象.当数组的所有值相等且顺序相同时,两个State对象(每个对象包含一个2D布尔数组)相等. 这是我的相关代码:public class State {Boolean[,] grid = new Boolean[4,4];Public State(Boolean[,] passedGrid){ //Constructorgrid = Array.Copy(passedGrid, grid, 16);}public bool Equals(State s2){ //Overloaded equals operatorfor (int x = ...

c# – 出于某种原因,不能将XOR运算符与两个char一起使用,有人知道为什么?【代码】

是否有一些正式标准不允许人们在C#中使用具有两个字符的^或XOR函数?public char[] XORinput(char[] input, char SN){int InputSize = input.Length;//initialize an output arraychar[] output = new char[InputSize];for (int i = 0; i < InputSize; i++){output[i] = input[i] ^ SN;}return output;}无论出于何种原因,此代码都给我这个错误,错误1400无法将类型’int’隐式转换为’char’.存在显式转换(您是否错过了演员?) 这没...

c# – 即使我有隐式运算符,类型转换也会失败【代码】

我用隐式强制转换运算符编写了自定义类型public class TcBool : TcDataTypeBase {public TcBool() : base(1, false) { } //For somewhat reason without this callin new TcBool() failspublic TcBool(bool value = false) : base(1, value) { }public static implicit operator bool(TcBool var) => (bool)var.Value;public static implicit operator TcBool(bool value) => new TcBool(value); }public abstract class TcDataTyp...