C# 6.0 Null Propagation Operator & Property Assignment

This question has been completely overhauled in the interest of being thorough in explanation. I have noticed what appears to be quite a poor limitation of the null propagation operator in C# 6.0 in that you cannot call property setters against an object that has been null propagated (though you can call property getters against an object that has been null propagated). As you will see from th

C#6.0空传播操作员和属性分配

为彻底解释这个问题,这个问题已经彻底改变了。 我注意到C#6.0中的空传播操作符似乎是一个很差的限制,因为您无法针对已传播空的对象调用属性setter (尽管您可以针对已传播空的对象调用属性getter ) 。 正如你从生成的IL(我已经反映到C#中)所看到的那样,没有任何东西会限制使用空传播调用属性设置器的能力。 首先,我创建了一个简单的类,使用Java样式的Get / Set方法以及一个具有公共getter / setter访问权限的属性

Shortcut &= acting like & not &&

public static void Main() { var result = false; Xpto xpto = null; result &= xpto.Some; } public class Xpto { public bool Some { get; set; } } I'm using C# 6.0 VS2015; the code above triggers a null exception. The &= operator is behaving like the & operator. I would expect it to behave similar to && and skip evaluation of the right side if value already

Shortcut&= like like&not &&

public static void Main() { var result = false; Xpto xpto = null; result &= xpto.Some; } public class Xpto { public bool Some { get; set; } } 我正在使用C#6.0 VS2015; 上面的代码触发一个空的异常。 &=运算符的行为与&运算符相同。 我希望它的行为与&&类似,并且如果值已经为false,则跳过右侧的评估(请参阅条件AND运算符(&&)文章中的“短路”行为的解释)。 这

Monadic null checking in C# 6.0

I stumbled across an interesting site, where some of the new (proposed) features of C# 6.0 are addressed. You may read it here: Probable C# 6.0 features. What I find particular interesting is the monadic null checking (also known as the null-propagation operator ?. ). According to the site, the following statement var bestValue = points?.FirstOrDefault()?.X ?? -1; contains the monadic null

Monadic在C#6.0中空检查

我偶然发现了一个有趣的网站,其中一些C#6.0的新功能已提出。 您可以在这里阅读:可能的C#6.0功能。 我觉得特别有趣的是单调空检查(也称为空传播算子? )。 根据该网站,以下声明 var bestValue = points?.FirstOrDefault()?.X ?? -1; 包含monadic null检查,它目前用这段代码实现: if (points != null) { var next = points.FirstOrDefault(); if (next != null && next.X != null) return next.X; }

Null propagation operator and foreach

Reading a lot about the Null propagation operator ?. , I found no answer whether it is helpful in the following scenario. Code that throws: int[] values = null; foreach ( var i in values ) // Throws since values is null. { // ... } To make this working, I have to add a null check before access to the values variable. Most likely the above code is out of scope of the design considerati

空传播操作符和foreach

读了很多关于空传播操作符?. ,我发现没有答案,它是否有助于在以下情况。 抛出的代码: int[] values = null; foreach ( var i in values ) // Throws since values is null. { // ... } 为了使这个工作,我必须在访问values变量之前添加一个null检查。 上述代码很可能超出了空传播运算符的设计考虑范围。 不过,可以肯定的是,我不得不问。 我的问题: 试图访问的时候是空的传播经营者有帮助null的藏品foreac

How to convert string to double with proper cultureinfo

I have two nvarchar fields in a database to store the DataType and DefaultValue, and I have a DataType Double and value as 65.89875 in English format. Now I want the user to see the value as per the selected browser language format (65.89875 in English should be displayed as 65,89875 in German). Now if the user edits from German format to 65,89875 which is 65.89875 equivalent in English, and t

如何使用正确的cultureinfo将字符串转换为double

我在数据库中有两个nvarchar字段来存储DataType和DefaultValue,并且我有一个DataType Double和值为65.89875的英文格式。 现在我希望用户根据所选的浏览器语言格式查看值(英语中的65.89875应显示为德语中的65,89875)。 现在,如果用户从德语格式编辑为65,89875(英语为65.89875),而其他用户从英文浏览器查看则为6589875。 这是因为在它的数据库中nvarchar列被存储为65,89875和使用英语的文化转变,当它变成6589875因为

Is Int32.ToString() culture

I'm running a beta version of ReSharper, and it's giving me warnings for the following code: int id; // ... DoSomethingWith(id.ToString()); The warning is on the id.ToString() call, and it's telling me "Specify a culture in string conversion explicitly". I understand the warning, and I know how to fix it -- just change the code to the much more unwieldy id.ToString(Cultur

是Int32.ToString()文化

我正在运行ReSharper的测试版,它给我警告以下代码: int id; // ... DoSomethingWith(id.ToString()); 警告是在id.ToString()调用,它告诉我“明确指定字符串转换中的文化”。 我理解这个警告,并且我知道如何解决它 - 只需将代码更改为id.ToString(CultureInfo.InvariantCulture) 。 但我的问题是:这是必要的吗? 我的意思是,当你使用DateTime (不同的文化有不同的日期格式)和Double (不同的字符用于小数点)时,指定

Why won't my symbols (pdb) load from a local path?

I've just built my app which consists of some C++ and C# code and installed it on another machine. When I connect the remote debugger, I can debug C++ code ok, and step though it. However, when I try to debug C# code, the symbols don't seem to be loaded. I've tried bringing up the modules pane in visual studio and have browsed to the correct .pdb file, but I just get a message box

为什么我的符号(pdb)不能从本地路径加载?

我刚刚构建了一个由一些C ++和C#代码组成的应用程序,并将它安装在另一台计算机上。 当我连接远程调试器时,我可以调试C ++代码,然后执行它。 但是,当我尝试调试C#代码时,这些符号似乎并未加载。 我试过在Visual Studio中调出模块窗格,并浏览了正确的.pdb文件,但我只是收到一个消息框,告诉我它不匹配。 我无法弄清楚为什么会发生这种情况。 我从这里使用chkmatch:http://www.debuginfo.com/tools/chkmatch.html

Caching web service results

I have a long running web service (3-5 mins) which returns a large custom object. I declare the web method like this... public class MyService : System.Web.Services.WebService { [WebMethod(CacheDuration=86400)] [XmlInclude(typeof(MyObject))] public MyObject Items(string myParam) { return new MyObject(myParam); } } I then consume the service in a classic

缓存Web服务结果

我有一个长时间运行的Web服务(3-5分钟),它返回一个大的自定义对象。 我宣布这样的网络方法... public class MyService : System.Web.Services.WebService { [WebMethod(CacheDuration=86400)] [XmlInclude(typeof(MyObject))] public MyObject Items(string myParam) { return new MyObject(myParam); } } 然后,我使用经典的ASP.NET 4.0应用程序中的服务并缓存“Items”的元素,

How can I limit Parallel.ForEach?

I have a Parallel.ForEach() async loop with which I download some webpages. My bandwidth is limited so I can download only x pages per time but Parallel.ForEach executes whole list of desired webpages. Is there a way to limit thread number or any other limiter while running Parallel.ForEach? Demo code: Parallel.ForEach(listOfWebpages, webpage => { Download(webpage); }); The real task

我怎样才能限制Parallel.ForEach?

我有一个Parallel.ForEach()异步循环,我下载了一些网页。 我的带宽有限,所以我只能每次下载x页,但是Parallel.ForEach执行所需网页的整个列表。 有没有办法在运行Parallel.ForEach时限制线程号或任何其他限制器? 演示代码: Parallel.ForEach(listOfWebpages, webpage => { Download(webpage); }); 真正的任务与网页无关,所以创造性的网页爬行解决方案将无济于事。 您可以在ParallelOptions参数中指定MaxDeg

Setting TempData within a ActionFilterAttribute

I have a custom action filter, that inside the OnActionExecuting , depending on certain criteria, logs out a user and redirects them to the home page of the site. The (stripped back) code for the redirect part is below filterContext.Controller.TempData.Add("key", "Message"); filterContext.Result = new RedirectResult("/"); As above, i am also setting a tempData message. Because the user h

在ActionFilterAttribute中设置TempData

我有一个自定义的操作过滤器,在OnActionExecuting内部,根据特定条件,将用户注销并将其重定向到网站的主页。 重定向部分的(剥离)代码如下 filterContext.Controller.TempData.Add("key", "Message"); filterContext.Result = new RedirectResult("/"); 如上所述,我也设置了一个tempData消息。 因为用户已经注销,所以当他们到达主页时, [Authorize]属性将把它们重定向到登录GET页面。 在登录视图中,我显示来自t