c#-7.2

Why ref structs cannot be used as type arguments?

随声附和 提交于 2020-05-25 06:40:42
问题 C# 7.2 introduced ref struct s. 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 i); // <- Error CS0306 The type 'Foo' may not be used as a type argument. I understand that ref structs can only exist on the stack, and not the heap. But what if the generic method that would use such ref structs is guaranteed to never put them on the heap, as in the example above that uses System

Why ref structs cannot be used as type arguments?

╄→尐↘猪︶ㄣ 提交于 2020-05-25 06:40:29
问题 C# 7.2 introduced ref struct s. 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 i); // <- Error CS0306 The type 'Foo' may not be used as a type argument. I understand that ref structs can only exist on the stack, and not the heap. But what if the generic method that would use such ref structs is guaranteed to never put them on the heap, as in the example above that uses System

How to pin a generic Span<T> instance to work on it with Parallel.For?

a 夏天 提交于 2020-01-02 01:56:12
问题 I'm rewriting some of my extension methods using the new Span<T> type and I'm having trouble finding a way to properly pin a generic instance to be able to use parallel code to work on it. As an example, consider this extension method: public static unsafe void Fill<T>(this Span<T> span, [NotNull] Func<T> provider) where T : struct { int cores = Environment.ProcessorCount, batch = span.Length / cores, mod = span.Length % cores, sizeT = Unsafe.SizeOf<T>(); //fixed (void* p0 = &span

LINQ: Group by index and value [duplicate]

情到浓时终转凉″ 提交于 2019-12-25 18:31:31
问题 This question already has answers here : linq group by contiguous blocks (5 answers) Closed last year . Lets say I have an list of strings with the following values: ["a","a","b","a","a","a","c","c"] I want to execute a linq query that will group into 4 groups: Group 1: ["a","a"] Group 2: ["b"] Group 3: ["a","a","a"] Group 4: ["c","c"] Basically I want to create 2 different groups for the value "a" because they are not coming from the same "index sequence". Anyone has a LINQ solution for this

C# Indexers with Ref Return Gets that also Support Sets

戏子无情 提交于 2019-12-23 12:45:03
问题 Am I doing something wrong here, or as of C# 7.2 Indexers that return by ref and allow set are not supported? Works: public ref byte this[int index] { get { return ref bytes[index]; } } Works too: public byte this[int index] { get { return bytes[index]; } set { bytes[index] = value; } } Fails: public ref byte this[int index] { get { return ref bytes[index]; } set { //<-- CS8147 Properties which return by reference cannot have set accessors bytes[index] = value; } } Fails too: public ref byte

Where do I find the new Span<T>?

南笙酒味 提交于 2019-12-18 07:28:53
问题 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 2017 to the latest version 15.5.0 where the change-log says: The C# compiler now supports the 7.2 set of language features including: Support for the Span<T> type being used throughout Kestrel and CoreFX via the ref struct modifier. but when I try to use it my code I'm getting an error and

Using C# 7.2 in modifier for parameters with primitive types

孤人 提交于 2019-12-18 03:59:08
问题 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 affect performance and could lead to an obscure behavior if the struct is mutable What does this mean for built-in primitives such as int , double ? I would like to use in to express intent in code, but not at the cost of performance losses to

Where do I find the new Span<T>?

半腔热情 提交于 2019-12-13 03:52:40
问题 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 2017 to the latest version 15.5.0 where the change-log says: The C# compiler now supports the 7.2 set of language features including: Support for the Span<T> type being used throughout Kestrel and CoreFX via the ref struct modifier. but when I try to use it my code I'm getting an error and

Error CS0305 Using the generic type 'Memory' requires 1 type arguments

雨燕双飞 提交于 2019-12-11 18:17:03
问题 I have the below snippet of code to test/use dotnet 2.1 in vs 2017 in order to try out and run C# 7.2s Span functionality. Where can I find the SDK that allows me to run this within Visual Studio. I can only find frameworks up to 2.0. using System; using System.Memory; namespace sim { class Program { static void Main(string[] args) { var arr = new byte[10]; Span<byte> bytes = arr; // Implicit cast from T[] to Span<T> Span<byte> slicedBytes = bytes.Slice(start: 5, length: 2); } } } Otherwise I

IL method to pin a ref T value as void* (to work on Span<T> from parallel code)

随声附和 提交于 2019-12-11 15:57:20
问题 Before the actual question, a small disclaimer: This is a related/follow up question to this one, but since here I'm talking about a more general issue (how to pin a ref T variable to be able to perform pointer operations on it), and I'm proposing a possible (probably wrong) solution, I opened a separate question. So, the question is: given a ref T variable (assuming it's the first item of an array), how can I pin it so that the GC won't cause problems while working on the underlying array