c#-6.0

Asp.net MVC Razor view - CS1525: Invalid expression term '.'

纵然是瞬间 提交于 2019-12-01 04:17:14
I have two identical ASP.Net 4.6 MVC project, project 1 is using roslyn complier within the site which is working fine. c:\windows\system32\inetsrv>C:\Websites1\bin\roslyn\csc.exe Microsoft (R) Visual C# Compiler version 1.2.0.60325 With the second project I'm getting the error below, it's using the complier from .Net framework. c:\windows\system32\inetsrv> "C:\Windows\Microsoft.NET\Framework64\v4.0.30319\csc.exe" Microsoft (R) Visual C# Compiler version 4.6.1590.0 Line 6: @if (!Model?.Item?.IsDerived(Templates.PageMetadata.ID) ?? true) Line 7: { Line 8: return; c:\Website2\Views\metadata

Is it possible to pass interpolated strings as parameter to a method?

僤鯓⒐⒋嵵緔 提交于 2019-12-01 04:02:50
I have started to use Interpolated Strings (new feature of C# 6) and it is really useful and gracefully. But according to my needs I have to pass format of string to a method as a parameter. Something like next: MyMethod(string format) In the past, I used it in the next way: MyMethod("AAA{0:00}") Now I tried this code: MyMethod($"AAA{i:00}") But this doesn't work, because i is created inside of the method and is out of scope in this context. Is it possible to use any trick for passing interpolated strings as a parameter to a method? You cannot do that, and that would not be a very good idea

Difference between (auto) properties initialization syntax in C# 6

旧城冷巷雨未停 提交于 2019-12-01 03:48:59
What is the difference between the following expressions for initializing the properties in C# 6: 1. Auto-Property initialized from constructor public class Context1 { public Context1() { this.Items = new List<string>(); } public List<string> Items { get; private set; } } 2: Property initialized from a backing field public class Context2 { private readonly List<string> items; public Context2() { this.items = new List<string>(); } public List<string> Items { get { return this.items; } } } 3: Auto-Property new syntax in C# 6 public class Context3 { public List<string> Items { get; } = new List

How to emulate C# 6 null-conditional in C# < 6

左心房为你撑大大i 提交于 2019-12-01 03:18:39
With C# 6.0 I can do this var isEqual = x.Id == y.Id && x.UpdatedAt == y.UpdatedAt && x.Name == y.Name && x.RulesUrl == y.RulesUrl && x.OngoingChallenges?.Count == y.OngoingChallenges?.Count && x.MembershipIds?.Count == y.MembershipIds?.Count; Is there any nice solution to do this with C# < 6.0? I mean this part && x.OngoingChallenges?.Count == y.OngoingChallenges?.Count && x.MembershipIds?.Count == y.MembershipIds?.Count; Because in old projects we do not have possibility to use C# 6.0. How to write isEqual efficiently? x.OnGoingChallenges?.Count is equivalent to x.OnGoingChallenges != null ?

Why can't I throw exceptions from an expression-bodied member?

自闭症网瘾萝莉.ら 提交于 2019-12-01 02:06:42
Using expression-bodied members allows you to define the body of a method or property as a single expression without a return keyword (should it return something). For example it turns these int Method1() { return 5; } void Method2() { Console.WriteLine(); } into these int Method1() => 5; void Method2() => Console.WriteLine(); A difference comes into play when you throw an exception from the body: void Method3() { throw new Exception(); } However, the following will not compile: void Method3() => throw new Exception(); with the following messages: Warning The member 'Program.Exception()' does

Is it possible to pass interpolated strings as parameter to a method?

青春壹個敷衍的年華 提交于 2019-12-01 01:45:30
问题 I have started to use Interpolated Strings (new feature of C# 6) and it is really useful and gracefully. But according to my needs I have to pass format of string to a method as a parameter. Something like next: MyMethod(string format) In the past, I used it in the next way: MyMethod("AAA{0:00}") Now I tried this code: MyMethod($"AAA{i:00}") But this doesn't work, because i is created inside of the method and is out of scope in this context. Is it possible to use any trick for passing

What is the benefit of using “Expression Bodied Functions and Properties” [duplicate]

。_饼干妹妹 提交于 2019-11-30 23:06:41
问题 This question already has answers here : Expression-bodied function members efficiency and performance in C# 6.0 (2 answers) Closed 3 years ago . I do have seen many using that new feature , but what is the benefit of using those expressions? Examples: public override string ToString() => string.Format("{0}, {1}", First, Second); public string Text => string.Format("{0}: {1} - {2} ({3})", TimeStamp, Process, Config, User); This question is different to this one, because I am not only asking

How to initialise ReadOnlyDictionary?

怎甘沉沦 提交于 2019-11-30 17:44:18
I have an unchanging dictionary that is exposed in a class. Currently my code looks like using System.Collections.Generic; using System.Collections.ObjectModel; public class FooClass { private static ReadOnlyDictionary<string, byte> _validRevisions = new ReadOnlyDictionary<string, byte>( new Dictionary<string, byte>() { { "1.0", 0x00 }, { "1.1", 0x01 }, { "1.2", 0x02 }, { "1.3", 0x03 } } ); public static ReadOnlyDictionary<string, byte> ValidRevisions => _validRevisions; // other FooClass contents... } I've used the backing field _validRevisions as I could not figure out a better way of

Public readonly field v.s. get-only property

牧云@^-^@ 提交于 2019-11-30 17:08:54
Are there cases when you would want a public readonly field v.s. a get-only auto-implemented property? public class Foo { public readonly string Hello; public string Hello2 { get; } } Both can only be set during the constructor and both offer readonly access outside of the class.. I'm a little tired so I might be missing something. PaulF One reason would be for data binding - .net implements binding to properties but not to public fields. Some discussion here : Why can't we use public fields for data binding in C#? Making it a property rather than a field means it can be used on interfaces.

What is the optional argument in C# interpolated string for?

≯℡__Kan透↙ 提交于 2019-11-30 17:01:58
Interpolated strings is one of the new features of C# 6.0. According to MSDN, the syntax of the embedded C# expressions can contain an optional, comma-separated value, deemed as <optional-comma-field-width> in the documentation . Unfortunately I didn't find what this field is for. From its name one might think that this value sets the maximal size of the "interpolated" field, but when I try the following expression: var p = Process.GetCurrentProcess(); Console.WriteLine($"Process name is {p.ProcessName, 5}"); I get the following output: Process name is LINQPad.UserQuery It's the minimum width