c#-7.0

Are C# anonymous types redundant in C# 7

a 夏天 提交于 2020-01-12 13:59:27
问题 Since C# 7 introduces value tuples, is there a meaningful scenario where they are better suited than tuples? For example, the following line collection.Select((x, i) => (x, i)).Where(y => arr[y.i].f(y.x)).ToArray(); makes the following line collection.Select((x, i) => new {x, i}).Where(y => arr[y.i].f(y.x)).ToArray(); redundant. What would be the use case where one is better used over the other (for either performance reasons or optimization)? Obviously, if there is a need for more than six

Why is throwing an exception inside lambda a C# 7 feature? [duplicate]

£可爱£侵袭症+ 提交于 2020-01-04 05:22:18
问题 This question already has answers here : Why can't I throw exceptions from an expression-bodied member? (5 answers) Closed 2 years ago . This statement doesn't compile in VS2015, but does in VS2017: var example = new Action( () => throw new Exception() What had to change in the way labmdas are parsed in order to support throwing an exception inside a labmda expression? Especially because if I use a lambda body, VS2015 is perfectly happy: My question is similar to Why can't I throw exceptions

An expression tree may not contain a reference to a local function

生来就可爱ヽ(ⅴ<●) 提交于 2020-01-01 09:11:50
问题 Error: An expression tree may not contain a reference to a local function public void Initialize() { CloudStorageProperties ImageFileProperties(string fileName) => _cloudStorage.GetBlob(CloudStorageType.Image, fileName).FileProperties; Config = new MapperConfiguration(x => { x.CreateMap<Category, CategoryViewModel>() .ForMember(vm => vm.ImagePath, m => m.MapFrom(src => ImageFileProperties(src.ImageFile.Name).Uri.AbsoluteUri)); }); } I can replace the local function with an anonymous function

An expression tree may not contain a reference to a local function

戏子无情 提交于 2020-01-01 09:11:14
问题 Error: An expression tree may not contain a reference to a local function public void Initialize() { CloudStorageProperties ImageFileProperties(string fileName) => _cloudStorage.GetBlob(CloudStorageType.Image, fileName).FileProperties; Config = new MapperConfiguration(x => { x.CreateMap<Category, CategoryViewModel>() .ForMember(vm => vm.ImagePath, m => m.MapFrom(src => ImageFileProperties(src.ImageFile.Name).Uri.AbsoluteUri)); }); } I can replace the local function with an anonymous function

VB.NET equivalent for C# 7 Type pattern matching

孤街醉人 提交于 2019-12-30 17:14:18
问题 Is there a VB.NET equivalent to this? Note in particular the bmp in the code sample. public void MyMethod(Object obj) { if (obj is Bitmap bmp) { // ... } } Or the short pattern matching syntax with is is exclusive to C#? EDIT: I already know these syntaxes: If TypeOf obj Is Bitmap Then Dim bmp As Bitmap = obj ' ... End If or Dim bmp As Bitmap = TryCast(obj, Bitmap) If bmp IsNot Nothing Then ' ... End If What I want to know is whether there is something even shorter, like that new C#7 syntax..

How can I remove exception “Predefined type 'ValueTuple`2' must be a struct” when debugging?

时光怂恿深爱的人放手 提交于 2019-12-30 09:58:24
问题 I started using the new tuple feature in c# 7.0 but I noticed that neither in the function that returns a tuple nor in its caller is possible to check the variable values in debug mode. Instead an exception is shown: $exception error CS8182: Predefined type 'ValueTuple`2' must be a struct. Is there a way to get rid of that glitch and debug normally? 回答1: It seems a bug that Microsoft has fixed but it will be available in a future update (2017) https://github.com/dotnet/roslyn/pull/16930 回答2:

HttpRequestMessageExtensions not being found at run-time in Azure Function

岁酱吖の 提交于 2019-12-30 04:17:06
问题 I've got an Azure Function app that creates a precompiled DLL (so it uses normal .cs files, not the older .csx method, pre-VS2017). Previously, it was targeting .Net Framework 4.5.2. I updated it to 4.7 so as to use some of the new C# 7 features. I updated my NuGet packages by doing "Update-Package -Reinstall" and verified that they all have the "net47" target set in my packages.config file. Everything compiles fine. But when I call a function that uses either of 2

Unable to cast object of type Newtonsoft.Json.Linq.JObject even though I am trying to cast to an object with matching properties

人走茶凉 提交于 2019-12-29 09:56:30
问题 I am working with ASP.NET Core 2.0 in VS2017. I am trying to deserialize some JSON that is returned in an HttpResponseMessage but I am getting an "Unable to cast object of type..." exception. Here is the code that is failing; FilesUploadedListResponse fileUploadListResponse = new FilesUploadedListResponse(); string jsonResult = response.Content.ReadAsStringAsync().Result; fileUploadListResponse = (FilesUploadedListResponse)JsonConvert.DeserializeObject(jsonResult); The last line is where I

Is there any point in using local functions if to only use them once?

一世执手 提交于 2019-12-24 09:58:56
问题 Imagine I have this code: public void Foo() { // Do bar work // Do baz work // Do foobar work } And I realize I can (and should because it was doing more than one thing) refactor it into: public void Foo() { bar(); baz(); foobar(); } private void bar() { /* do bar work */ } private void baz() { /* do baz work */ } private void foobar() { /* do foobar work */ } But then I realize I will never use these functions outside of Foo() , so those functions are just cluttering the main page and the

What is the purpose of Deconstruct method in KeyValuePair<> struct?

被刻印的时光 ゝ 提交于 2019-12-24 02:55:26
问题 I was looking at System.Collections.Generic.KeyValuePair<TKey, TValue> struct in System.Runtime, Version=4.2.1.0 and this method took my attention. Here's the signature: public void Deconstruct(out TKey key, out TValue value); Does it contain any logic besides simply forwarding Key and Value properties? Why would anyone ever prefer it over property getters? 回答1: Deconstruction is a feature that was introduced mainly for value tuples in C# 7, letting you "unpackage all the items in a tuple in