Questions tagged [c#-7.2]

For issues relating to development with C#, version 7.2.

C# 7.2 is a version of the programming language. It ships with the 15.5 release of Visual Studio 2017.

New language features are:

  • Reference semantics with value types
  • Non-trailing named arguments
  • Leading underscores in numeric literals
  • private protected access modifier
84 questions
136
votes
6 answers

What is the meaning of the planned "private protected" C# access modifier?

As part of the Roslyn documentation on GitHub, there's a page called Language feature implementation status, with planned language features for C# and VB. One feature I couldn't wrap my head around was private protected access modifier: private…
Kobi
  • 135,331
  • 41
  • 252
  • 292
128
votes
5 answers

Why would one ever use the "in" parameter modifier in C#?

So, I (think I) understand what the in parameter modifier does. But what it does appears to be quite redundant. Usually, I'd think that the only reason to use a ref would be to modify the calling variable, which is explicitly forbidden by in. So…
Travis Reed
  • 1,572
  • 2
  • 8
  • 11
93
votes
4 answers

What is the difference between Span and Memory in C# 7.2?

C# 7.2 introduces two new types: Span and Memory that have better performance over earlier C# types like string[]. Question: What is the difference between Span and Memory? Why would I use one over the other?
Dan Sorensen
  • 11,403
  • 19
  • 67
  • 100
80
votes
4 answers

What is the use case for the (C# 7.2) "private protected" modifier?

C# 7.2 introduces the private protected modifier. I've always protected access to fields with properties, allowing access via the Get/Set methods as I typically don't want the internal state of my object modified by anything other than my own…
Jay
  • 9,561
  • 7
  • 51
  • 72
41
votes
5 answers

Check if value tuple is default

How to check if a System.ValueTuple is default? Rough example: (string foo, string bar) MyMethod() => default; // Later var result = MyMethod(); if (result is default){ } // doesnt work I can return a default value in MyMethod using default syntax…
nawfal
  • 70,104
  • 56
  • 326
  • 368
38
votes
3 answers

How do I convert a C# string to a Span? (Span)

How do I convert a string to a Span? Span mySpan = "My sample source string";
Dan Sorensen
  • 11,403
  • 19
  • 67
  • 100
23
votes
2 answers

Where do I find the new Span?

Everyone is writing about how great the new type Span is so I eagerly wanted to start rewriting a couple of methods in my libraries but where do I actually find it? I've updated Visual Studio 2017 to the latest version 15.5.0 where the change-log…
t3chb0t
  • 16,340
  • 13
  • 78
  • 118
17
votes
4 answers

Using C# 7.2 in modifier for parameters with primitive types

C# 7.2 introduced the in modifier for passing arguments by reference with the guarantee that the recipient will not modify the parameter. This article says: You should never use a non-readonly struct as the in parameters because it may negatively…
Drew Noakes
  • 300,895
  • 165
  • 679
  • 742
16
votes
1 answer

Why ref structs cannot be used as type arguments?

C# 7.2 introduced ref structs. However, given a ref struct like this: public ref struct Foo { public int Bar; } I cannot use it as a type argument: int i = 0; var x = Unsafe.As(ref i); // <- Error CS0306 The type 'Foo' may not be used…
Fit Dev
  • 3,413
  • 3
  • 30
  • 54
15
votes
3 answers

Span does not require local variable assignment. Is that a feature?

I notice that the following will compile and execute even though the local variables are not initialized. Is this a feature of Span? void Uninitialized() { Span s1; var l1 = s1.Length; Span s2; UninitializedOut(out s2); var l2…
jyoung
  • 5,071
  • 4
  • 30
  • 47
14
votes
3 answers

How to make readonly structs XML serializable?

I have an immutable struct with only one field: struct MyStruct { private readonly double number; public MyStruct(double number) => this.number = number; } And I want this to be able to get serialized/deserialized by: Data…
Şafak Gür
  • 7,045
  • 5
  • 59
  • 96
13
votes
4 answers

What is the point of the in modifier for classes

C# 7.2 introduces the in modifier for parameters which makes perfect sense for structs and in particular for readonly structs. It is also allowed to use it for a reference type void Method(in StringBuilder value) { } As reference types are passed…
user4388177
  • 2,433
  • 3
  • 16
  • 30
13
votes
1 answer

Where is c# 7.2 in visual studio project settings?

Ive seen people using and discussing c# 7.2 features but I cant seem to find it. Ive got latest updates and only up to version 7.1 is listed. why and how can I get v7.2? specs: Visual studio 2017 version 15.4.4 Visual C# 2017 -…
M.kazem Akhgary
  • 18,645
  • 8
  • 57
  • 118
12
votes
1 answer

Span and Memory as a replacement for arrays in method signatures?

Replace arguments with Span in methods? Should I replace all my array (such as byte[], char[], and string[]) parameters in my synchronous methods with Span (such as Span, Span, and Span)? Example: public void Foo(byte[]…
Fred
  • 12,086
  • 7
  • 60
  • 83
12
votes
2 answers

Why is casting a struct via Pointer slow, while Unsafe.As is fast?

Background I wanted to make a few integer-sized structs (i.e. 32 and 64 bits) that are easily convertible to/from primitive unmanaged types of the same size (i.e. Int32 and UInt32 for 32-bit-sized struct in particular). The structs would then expose…
Fit Dev
  • 3,413
  • 3
  • 30
  • 54
1
2 3 4 5 6