c#-7.0

In C# can you define an alias to a value tuple with names?

▼魔方 西西 提交于 2019-12-02 17:48:46
I know it's possible to define aliases in C# with the using keyword. e.g. using ResponseKey = System.ValueTuple<System.Guid, string, string>; However, is it possible to define one using the new syntax for value tuples? using ResponseKey = (Guid venueId, string contentId, string answer); This syntax does not appear to work. Should it? This has been requested, and recorded in the Roslyn repo on Github . However it received a mixed reception there, and the proposed record types , would more than cover this requirement. The issue was closed in the Roslyn repo but is now being tracked in the C#

C# 7.0 'out variables' in Visual Studio 2015 [duplicate]

坚强是说给别人听的谎言 提交于 2019-12-02 09:24:31
This question already has an answer here: How to use C# 7 with Visual Studio 2015? 2 answers I would like to compile C# 7.0 solution in Visual Studio 2015, but I have syntax error: Error ErrorMessage : 'DateTime' is a type but is used like a variable. This is related to 'out variables' changed in C# 7.0 Unfortunately I can not use VS2017 (community) and THIS TUTORIAL - not working (master branch is adapted to VS2017 now). Do you have an idea how to solve this problem? You just need to declare a DateTime variable outside the call and pass the parameter with the out keyword like this: DateTime

C# 7 pattern matching semantics

安稳与你 提交于 2019-12-02 01:39:29
问题 I have the two blocks of code that I would like to think are equal: First the if based block Then the exact same but converted to switch case over types. (sorry for bad Resharper red squiggly markers, Resharper doesnt understand this yet) The switch based code will throw a Null reference exception on the first return Actor.Done which is not null. Is this a pattern match bug or is there some semantics that I am missing here? [edit] I've found a fix for it.. Changing this: case MessageEnvelope

C# 7 pattern matching semantics

余生颓废 提交于 2019-12-02 00:40:18
I have the two blocks of code that I would like to think are equal: First the if based block Then the exact same but converted to switch case over types. (sorry for bad Resharper red squiggly markers, Resharper doesnt understand this yet) The switch based code will throw a Null reference exception on the first return Actor.Done which is not null. Is this a pattern match bug or is there some semantics that I am missing here? [edit] I've found a fix for it.. Changing this: case MessageEnvelope env: to case MessageEnvelope _: var env = m as MessageEnvelope; Makes it all work. So that leaves me

C# 7 how to unit test local functions [duplicate]

此生再无相见时 提交于 2019-12-01 22:33:59
问题 This question already has answers here : private functions vs nested functions (2 answers) Closed 2 years ago . I've been looking at some articles about local functions, and the one sentence states: Local functions are defined within a method and aren't available outside of it So given the below code example is there any way to unit test the square method? int SumAndSquare(int x, int y) { var sum = x + y; return square(sum); int square(int z) { return z * z; } } 回答1: In general you can't in a

C# 7 how to unit test local functions [duplicate]

假装没事ソ 提交于 2019-12-01 21:36:14
This question already has an answer here: private functions vs nested functions 2 answers I've been looking at some articles about local functions, and the one sentence states: Local functions are defined within a method and aren't available outside of it So given the below code example is there any way to unit test the square method? int SumAndSquare(int x, int y) { var sum = x + y; return square(sum); int square(int z) { return z * z; } } xanatos In general you can't in a maintainable way for non-trivial local functions (reason explained in a comment to this response). A local function that

VB.NET equivalent for C# 7 Type pattern matching

[亡魂溺海] 提交于 2019-12-01 15:43:58
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... Thank you very much. Currently, no. If you want to implement this, you'll have to use some of the

Pattern matching - variable in scope outside if-block

谁说胖子不能爱 提交于 2019-12-01 10:43:26
I'm trying to understand why y is in scope in the following example: static void Main(string[] args) { int x = 1; if (x is int y) { } Console.WriteLine("This should NOT be in scope:" + y); // but it is... } If I change int x to object x , then y is no longer in scope (as expected). Why is y in scope when the expression being matched is of int type and not when the type is object ? It's odd that the scoping changes based on the expression type. y seems to stay in scope when the expression type and the pattern type are the same, and they're both value types . (Same issue exists when both types

Pattern matching - variable in scope outside if-block

余生长醉 提交于 2019-12-01 09:04:33
问题 I'm trying to understand why y is in scope in the following example: static void Main(string[] args) { int x = 1; if (x is int y) { } Console.WriteLine("This should NOT be in scope:" + y); // but it is... } If I change int x to object x , then y is no longer in scope (as expected). Why is y in scope when the expression being matched is of int type and not when the type is object ? It's odd that the scoping changes based on the expression type. y seems to stay in scope when the expression type

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

我只是一个虾纸丫 提交于 2019-12-01 06:43:04
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? 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 It is a bug in the current version of System.ValueTuple. To workaround it till MS releases the fix, downgrade the