bodied function members efficiency and performance in C# 6.0

In a new C# 6.0 we can define methods and properties using lambda expressions. For instance this property public string Name { get { return First + " " + Last; } } can be now defined as follows: public string Name => First + " " + Last; The information about expression-boided function members you can find here: http://blogs.msdn.com/b/csharpfaq/archive/2014/11/20/new-features-in-c-6.asp

在C#6.0中体现函数成员的效率和性能

在新的C#6.0中,我们可以使用lambda表达式定义方法和属性。 例如这个属性 public string Name { get { return First + " " + Last; } } 现在可以定义如下: public string Name => First + " " + Last; 关于表达式函数成员的信息可以在这里找到:http://blogs.msdn.com/b/csharpfaq/archive/2014/11/20/new-features-in-c-6.aspx 有谁知道在使用新语法时是否有任何开销? 它可以减慢应用程序的运行速度(或提高效

What is the => assignment in C# in a property signature

I came across some code that said public int MaxHealth => Memory[Address].IsValid ? Memory[Address].Read<int>(Offs.Life.MaxHp) : 0; Now I am somewhat familiar with Lambda expressions. I just have not seen it used it this way. What would be the difference between the above statement and public int MaxHealth = x ? y:z; What you're looking at is an expression-bodied member, not

在属性签名中,C#中的赋值是什么?

我遇到了一些代码说 public int MaxHealth => Memory[Address].IsValid ? Memory[Address].Read<int>(Offs.Life.MaxHp) : 0; 现在我对Lambda表达式有一定的了解。 我只是没有看到它以这种方式使用它。 上述声明和声明之间的区别是什么? public int MaxHealth = x ? y:z; 你看到的是一个表达式的成员,而不是一个lambda表达式。 当编译器遇到一个表达式属性成员时,它将基本上将它转换为get ter,如下所示:

Can I initialize public properties of a class using a different type in C#?

In Java, I can have an object like this: public class MyObject { private Date date; public Date getDate() { return date; } public void setDate(Date date) { this.date = date; } public void setDate(String date) { this.date = parseDateString(date); } private Date parseDateString(String date) { // do magic here return dateO

我可以使用C#中的不同类型初始化一个类的公共属性吗?

在Java中,我可以有这样一个对象: public class MyObject { private Date date; public Date getDate() { return date; } public void setDate(Date date) { this.date = date; } public void setDate(String date) { this.date = parseDateString(date); } private Date parseDateString(String date) { // do magic here return dateObj;

Socket throws nullreferenceexception while asynchrously receiving data

So I have my server-client protocol and they cannot perfectly but when the client has received data from the server it throws a nullreference exception, this code handles the received data: private void AsyncReceive(IAsyncResult result) { int bytesTransferred; try { bytesTransferred = _handle.EndReceive(result); //throws the exception if (byt

Socket在异步接收数据时抛出nullreferenceexception

所以我有我的服务器 - 客户端协议,他们不能完美的,但是当客户端从服务器接收到数据时,它会抛出一个nullreference异常,这段代码处理接收到的数据: private void AsyncReceive(IAsyncResult result) { int bytesTransferred; try { bytesTransferred = _handle.EndReceive(result); //throws the exception if (bytesTransferred <= 0) throw new

C# Null Conditional Operator alternative (conditional assignment)?

The C# null-conditional operator allows for useful short circuiting: double? range = (unit as RangedUnit)?.WeaponRange; Unfortunately the null-conditional operator cannot be used in the same way for short hand assignment, because it returns a value (which cannot be used in left handed assignment): (unit as RangedUnit)?.PreferredTarget = UnitType.Melee; resulting in possible alternative syntax

C#空条件运算符替代(有条件赋值)?

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

What is meant by "the null conditional operator short circuits"?

Note to future visitors: This question was based on faulty repro code. The ?. operator does indeed short circuit. You can close this browser tab now. There are many sources on the web that claim that the null conditional operator ( ?. ) short circuits (eg http://www.informit.com/articles/article.aspx?p=2421572, search for "circuit"). I cannot detect any such thing: static voi

“无条件运营商短路”是什么意思?

给未来访客的提示:这个问题是基于错误的repro代码。 这个?. 操作员确实发生短路。 您现在可以关闭此浏览器选项卡。 网络上有很多来源声称空条件运算符( ?. )短路(例如http://www.informit.com/articles/article.aspx?p=2421572,搜索“电路”)。 我无法检测到任何这样的事情: static void Main() { var c = new C(); Console.WriteLine(c?.Inner?.Inner); //does not rely on short circuiti

How to make multiplication operator (*) behave as short

I have lots of computations, specially multiplication, where first part is sometimes zero and I don't want to evaluate second operand in that case. There are at least two short-circuit operators in C#: && and || which evaluate second operand only if necessary. I want to achieve similar behavior with multiplication operator. In .net you can't overload && operator direc

如何使乘法运算符(*)的行为如此短

我有很多计算,特别是乘法,其中第一部分有时是零,我不想在这种情况下评估第二个操作数。 C#中至少有两个短路操作符: &&和|| 仅在必要时才评估第二个操作数。 我想用乘法运算符来实现类似的行为。 在.net中,不能直接重载&&操作符,但是可以重载&和false操作符,因此可以使用扩展点来改变短路操作符的行为。 你可以在这篇文章中找到更多的细节C#操作符重载:'&&'操作符 是否有

Xamarin Studio c# null propagating operator

Before filling a bug case about Xamarin Studio I wanted to ask your opinion public class Class1 { public int AProp { get; set; } } Considering this simple scenario Class1 c; var aProp = c?.AProp; Shouldn't aProp be inferred as an int? instance as it is in c#-6.0 on Visual Studio 2015+? Because actually it is not, it is inferred as a plain int instead. Xamarin Studio is not complai

Xamarin Studio c#空传播运算符

在填写关于Xamarin Studio的错误案例之前,我想问你的意见 public class Class1 { public int AProp { get; set; } } 考虑到这个简单的情况 Class1 c; var aProp = c?.AProp; 不宜aProp被推断为一个int? 因为它在Visual Studio 2015+上的c#-6.0中? 因为实际上它不是,它被推断为一个纯int 。 Xamarin工作室并不抱怨操作员,但它并不认可aProp为可空的类型,因此抱怨。例如, .HasValue属性评估; 不仅是智能感知,

Null propagation operator and dynamic variable

I have been looking at the null-propagation operator in C#6 and tried to make it work with the variables of dynamic type but without success. Consider the code below, it compiles but CLR throws AccessViolationException at runtime when the null-propagation is applied to dynamic object. class SomeType { public object SomeProperty { get; set; } static void Main() { var obj = n

空传播算子和动态变量

我一直在研究C#6中的空传播运算符,并试图使它适用于dynamic类型的变量,但没有成功。 考虑下面的代码,它编译,但CLR在运行时将空传播应用于动态对象时抛出AccessViolationException 。 class SomeType { public object SomeProperty { get; set; } static void Main() { var obj = new SomeType() { SomeProperty = "ABCD" }; var p1 = ((dynamic)obj).SomeProperty; //OK, p1 is set to "A

Why does .NET foreach loop throw NullRefException when collection is null?

So I frequently run into this situation... where Do.Something(...) returns a null collection, like so: int[] returnArray = Do.Something(...); Then, I try to use this collection like so: foreach (int i in returnArray) { // do some more stuff } I'm just curious, why can't a foreach loop operate on a null collection? It seems logical to me that 0 iterations would get executed with a

为什么.NET foreach循环在收集为空时抛出NullRefException?

所以我经常遇到这种情况...... Do.Something(...)返回一个Do.Something(...) ,如下所示: int[] returnArray = Do.Something(...); 然后,我尝试像这样使用这个集合: foreach (int i in returnArray) { // do some more stuff } 我只是好奇,为什么不能在一个空集合上运行一个foreach循环? 对我来说,看起来合乎逻辑的是,0次迭代将得到一个NullReferenceException执行......相反,它会抛出一个NullReferenceExcepti