Questions tagged [c#-7.2]
For issues relating to development with C#, version 7.2.
c#-7.2
85
questions
138
votes
5
answers
37k
views
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 ...
136
votes
6
answers
13k
views
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 ...
102
votes
4
answers
33k
views
What is the difference between Span<T> and Memory<T> in C# 7.2?
C# 7.2 introduces two new types: Span<T> and Memory<T> that have better performance over earlier C# types like string[].
Question: What is the difference between Span<T> and Memory&...
82
votes
4
answers
10k
views
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 ...
46
votes
5
answers
26k
views
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 ...
42
votes
3
answers
42k
views
How do I convert a C# string to a Span<char>? (Span<T>)
How do I convert a string to a Span<char> in C#?
Span<char> mySpan = "My sample source string";
23
votes
2
answers
8k
views
Where do I find the new Span<T>?
Everyone is writing about how great the new type Span<T> 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 ...
17
votes
4
answers
2k
views
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 ...
16
votes
1
answer
4k
views
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<int, Foo>(ref ...
15
votes
3
answers
880
views
Span<T> 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<char> s1;
var l1 = s1.Length;...
15
votes
2
answers
5k
views
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 ...
14
votes
3
answers
3k
views
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 ...
14
votes
1
answer
3k
views
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<byte>, Span<char>,...
13
votes
2
answers
3k
views
Should ReadOnlySpan<T> parameters use the "in" modifier?
C# 7.2 introduced reference semantics with value-types, and alongside this Microsoft have developed types like Span<T> and ReadOnlySpan<T> to potentially improve performance for apps that ...
13
votes
4
answers
1k
views
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 ...