【C#中的空结合运算符和运算符\u0026\u0026】教程文章相关的互联网学习教程文章

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...

二元运算符的其中一个参数必须是包含类型c#【代码】

public static int[,] operator *(int[,] arr1, int[,] arr2){int sum;int[,] res = new int[arr1.GetLength(0), arr2.GetLength(1)];for (int i = 0; i < arr1.GetLength(0); i++){for (int j = 0; j < arr2.GetLength(1); j++){sum = 0;for (int k = 0; k < arr1.GetLength(1); k++){sum = sum + (arr1[i, k] * arr2[k, j]);}res[i, j] = sum;//Console.Write("{0} ", res[i, j]);}//Console.WriteLine();}return res;}在这里我试...

c# – 条件运算符表达式(连续几个)【代码】

bool isGeneric = variableA != null ? variableB != null ? false : true : true;嗨伙计们,我遇到过这条线.任何人都可以破译这条线/将它们分组到我的括号中吗? 感谢给予的任何帮助.提前致谢最佳答案:它是三元内部的三元组:bool isGeneric = variableA != null ? (variableB != null ? false : true) : (true);如果variableA不等于null,请检查第一个条件,否则返回true.在第一个条件中,如果variableB不为null,则返回false,如果是,...

c# – 当T是接口时,为什么我的演员运算符从T到SomeStruct不工作?【代码】

当我尝试使用从接口类型到泛型结构类型的用户定义的强制转换操作符时,我收到一个编译错误,指出该类型无法转换:public interface J { } public struct S<T> {public static explicit operator S<T>(T value) {return new S<T>();} } public static class C {public static S<J> Test(J j) {return (S<J>)j; // <- error: cannot convert type 'J' to type 'S<J>'} }请注意,如果J是一个类,转换将起作用. 有一个similar question abo...

LINQ体验(13)——LINQ to SQL语句之运算符转换和ADO.NET与LINQ to SQL【代码】

q =from p in db.Products.AsEnumerable()where isValidProduct(p)select p;语句描写叙述:这个样例就是使用AsEnumerable以便使用Where的clientIEnumerable实现,而不是默认的IQueryable将在server上转换为SQL并运行的默认Query<T>实现。这非常有必要,由于Where子句引用了用户定义的client方法isValidProduct,该方法不能转换为SQL。2.ToArray:将序列转换为数组使用 ToArray <TSource>可从序列创建数组。 var q =from c in db.Cu...

C# Operator 运算符【代码】

operator 可以用于重载运算符和自定义类型转换 实例 public class _20210302_1_Model{public string Name { get; set; }public int Age { get; set; }/** operator 用于重载运算符*/// 重载运算符"+"public static int operator + (_20210302_1_Model m1, _20210302_1_Model m2){return m1.Age + m2.Age;}// 重载运算符"-"public static int operator -(_20210302_1_Model m1, _20210302_1_Model m2){return m1.Age - m2.Age;}...

[C#基本知识] null合并运算符-null条件运算符-switch语句【代码】

核心代码演示: static void Main(string[] args){//1.null合并运算符string s1 = null;string s2 = s1 ?? "nothing";Console.Write(s2);//2.null条件运算符(Elvis运算符)System.Text.StringBuilder sb = null;string s3 = sb?.ToString();Console.Write(s3);//3.两者结合使用System.Text.StringBuilder sb2 = null;string s4 = sb2?.ToString() ?? "nothing";Console.Write(s4);//4.switch 测试object x = 2000.0d;switch(x){case ...

C# 有丰富的内置运算符【代码】

C# 运算符 运算符是一种告诉编译器执行特定的数学或逻辑操作的符号。C# 有丰富的内置运算符,分类如下: 算术运算符 关系运算符 逻辑运算符 位运算符 赋值运算符 杂项运算符 本教程将逐一讲解算术运算符、关系运算符、逻辑运算符、位运算符、赋值运算符及其他运算符。 算术运算符 下表显示了 C# 支持的所有算术运算符。假设变量 A 的值为 10,变量 B 的值为 20,则: 运算符 描述 实例把两个操作数相加 A + B 将得到 30从第一个操作...

C#可空类型及其衍生运算符【图】

这节讲一下C#可空类型(Nullable) 我们知道,值类型在使用前必须设置值,而引用类型则可以是null,但在某些情况下,为值类型设置为空是必要的(如处理数据库数据的时候),微软因此推出了可空类型 System.Nullable<T> 这是一个泛型类,其中,T就代表一个具体的值类型。 在声明和使用中,以下两种情况是不允许的: 以下情况则可以(了解VS的同学会知道,类型颜色变灰说明此处可以简化,这就涉及到它的一个衍生运...

C#中运算符“ =>”的作用

C#的Lambda 表达式都使用 Lambda?运算符?=>,该运算符读为“goes to”。语法如下:public string GetWelcomMsg()=> "你好, .Net Core 3.1";注:函数体多于一条语句的可用大括号括起;=> 运算符具有与赋值运算符?(=) 相同的优先级,并且是右结合运算符。

【死背硬记系列】C#运算符及表达式【代码】

运算符 关系运算符 所谓关系,就是我和你,你和他,我和他,这里基基的,受受的。常见的其实大家在小学时就已经见过了。我们这里简单的再了解一下。 大于:> 小于:< 大于等于:>= 小于等于:<= 等于:==(这里是个特殊的东西,一会后面我们会再解释) 不等于:!=(这里是个特殊的东西,因为小学时不这么写) 想必大家都已经了解,以上的关系运算符反馈给我们的答案只有两个“对”与“否”。在C#中我们还有一种类型叫做bool类型: bool...

c#-是否有\u0026\u0026和||的“和赋值运算符”速记?【代码】

要添加一个值并将其分配回自己,我可以这样做:x = x + y;为了简化表示,我可以这样使用add assignment operator:x += y;是否有&&这样的速记和||运算符吗?:x = x && y;我在下面尝试过,结果与上面不同:x &= y;解决方法:您可以看到C#运算符here的列表.x &= y – AND assignment. AND the value of y with the value of x, storethe result in x, and return the new value. x |= y – OR assignment. OR the value of y with the v...