Questions tagged [c#-7.3]

For issues relating to development with C#, version 7.3. In most cases you should also specify the c# tag.

-1
votes
2answers
49 views

Better way of rewriting below method

I have below method that does work Okay but I believe we can make it better by some how getting rid of these if conditions, but not sure how? protected void SaveSession<T>(T sessionProperty, ...
0
votes
1answer
51 views

Conflicting constraints 'value' and 'System.Enum' when using explicit interface implementation

I'm building an extensible system for converting between various enum types. The idea is that a developer can inherit a BaseEnumConverter class and provide an implementation for an abstract method ...
0
votes
0answers
36 views

CS1580 fired with a paramater that is generic and nullable (xml doc)

I'm using VS2019 16.1.1 (in VS2017 15.9.12 this does not happen) I have method with this definition: public static bool HasValue<T>(this T? source) where T : struct and in a xml doc I have ...
1
vote
1answer
78 views

How to know the version of the C# using the preprocessor?

My project in Debug mode contains constructs that only supports C# 7.3. But in Release mode, the project should be built on C# 7.0 without specific code lines. I know about some standard preprocessor ...
0
votes
2answers
125 views

expression of type 'T' cannot be handled by a pattern of type '<null>'

Here is a minimal sample: class C { public bool F<T>(T x) => x is null; } The real code causing the issue is here https://github.com/kofifus/With/blob/master/With.cs#L35 Until today this ...
4
votes
1answer
89 views

Why can ValueTuple not be const?

ValueTuple types, declared as fields, can be mutable: class Foo { (int, int) bar = (0, 1); } or readonly: class Foo { readonly (int, int) bar = (0, 1); } and this (im)mutability applies to ...
14
votes
1answer
279 views

Deconstruction is ambiguous

I have a vector class with two deconstruction methods as follows: public readonly struct Vector2 { public readonly double X, Y; ... public void Deconstruct( out double x, out double y ) ...
2
votes
2answers
29 views

Operator '&' cannot be applied to operands of type 'T' and 'T', where T : Enum

I have this enum with Flags attribute [Flags] public enum Animal { Null = 0, Cat = 1, Dog = 2, CatAndDog = Cat | Dog } And use C# 7.3 which allows type constraints like: where T : ...
24
votes
4answers
935 views

Equality and polymorphism

With two immutable classes Base and Derived (which derives from Base) I want to define Equality so that equality is always polymorphic - that is ((Base)derived1).Equals((Base)derived2) will call ...
2
votes
4answers
97 views

c# 7.3 ValueTuple - check for default after destructuring

I have (string name, int val) f(bool ok) { if (!ok) return default; return ('John', 5); } I then do somewhere: var result = f(true); if (result==default) return false; var (name, val) = ...
10
votes
1answer
430 views

Struct pointer (address), and default constructor

Does taking address of a C# struct cause default constructor call? For example, I got such structs: [StructLayout(LayoutKind.Sequential, Pack = 1)] public struct HEADER { public byte OPCODE; ...
0
votes
0answers
81 views

The modifier 'readonly' is not valid for explicit interface implementation. Strange analyser error

This is C# 7.3 netFramework 4.7.1 Unity(auto-generated) project. Rider (2018.3) states strange error: The modifier 'readonly' is not valid for explicit interface implementation. I have interface with ...
3
votes
1answer
156 views

Is a Span<T> pointing to Fixed Sized Buffers without a fixed expression possible?

I'm using .NET Core 2.1 and language standard 7.3. I wish to reference a fixed buffer without obtaining a pointer to it. Is it currently possible? public unsafe struct InteropStruct { private ...
9
votes
1answer
513 views

How is it that a struct containing ValueTuple can satisfy unmanaged constraints, but ValueTuple itself cannot?

Consider the following types: (int, int) → managed. struct MyStruct { public (int,int) Value; } → unmanaged! Problem: A non-generic structure MyStruct, which has a managed member (int,int) has been ...
7
votes
2answers
192 views

How do I check if a type fits the unmanaged constraint in C#?

How do I check if a type T fits the unmanaged type constraint, such that it could be used in a context like this: class Foo<T> where T : unmanaged? My first idea was typeof(T).IsUnmanaged or ...
6
votes
1answer
128 views

Why does the Visual Studio watch window show wrong values for ValueTuples in a collection?

I'm finding that ValueTuples evaluate differently when I access their properties from a collection. public static List<Tuple<string, bool>> MyTupleList = new List<Tuple<string, ...
0
votes
1answer
448 views

Error CS8107: Feature 'tuple equality' is not available in C# 7.0. Please use language version 7.3 or greater

I have the lastest Visual Studio 2017 version I have tried: Specifically set C# Version project to 7.3 Specifically set C# Version project to latest to manually modified the .csproj of the project (...
3
votes
1answer
170 views

Why generic structs cannot be unmanaged?

Please look at this code: namespace ConsoleApp { public struct MyPoorGenericStructThatCannotBeUnmanaged<T> where T: unmanaged { public T Field; } public class MyClass&...
0
votes
0answers
90 views

FileStream.SetLength: Are the contents of the stream between the old and the new length really undefined, or are they zeros?

The FileStream.SetLength documentation says: the contents of the stream between the old and the new length are undefined But this blog post says: The most basic way of doing this is to use ...
1
vote
1answer
143 views

GetPinnableReference Implementation that Pins Underlying String

I am trying to implement the new pattern introduced in C# 7.3 that supports pinning of custom types using fixed statement. See article on the Docs I am however concerned that in the code below I ...
2
votes
1answer
74 views

When is it Safe to Return Span<T> Initialized from Stack-Allocated Variable / Buffer?

Are the following 2 examples of returning Span<byte> (and Span<T> more generally) safe in a sense that when the method returns, the memory location to which the returned instance of Span&...
0
votes
1answer
55 views

Ref readonly property with class type

I'm trying to create a readonly property (c# 7.2 feature). public class JobStatus { public int Id { get; set; } } public class Job { public ref readonly JobStatus Status => ref _jobStatus;...
1
vote
2answers
258 views

How to get 'named' tuple components inside Linq queries?

Suppose I have a of tuples, say List<(string, Table)> and I want to iterate over it using a Parallel.ForEach, using the 'named version' of the tuples' components. The following code does that: ...
1
vote
1answer
33 views

Formatting for showing temperature at a particular time and date in en-US culture

I searched online and went through this MSDN site, as well but still could not figure out what the following format means. Could someone please explain with an example? I'm using en-US culture and ...
4
votes
1answer
1k views

Can I use C# 7.3 with .Net Framework 4.6.1?

I'm interested in enum generic constraints, but when I'm switching language version for project on Build → Advanced I'm still getting error «not available in C#5; please use language version 7.3 or ...
0
votes
1answer
23 views

C# targetting multiple frameworks

I've tried doing some searching on this but as I am unsure of "what" this is I didn't get very far so it's most likely a duplicate of another question I couldn't find. I'm looking at a piece of code ...
2
votes
1answer
94 views

Cannot apply '&' operator for generic Enum parameters

I'm trying to write some generic extension methods for flag-style enums. As of C# 7.3, the TFlag type argument can be marked as Enum, but the compiler throws an error for the expression flags & ...
2
votes
2answers
416 views

Possible to use extensions methods on a generic Enum Type? (not enum value)

There are some recent updates to C# that mean you can use Enum as a generic type constraint, and I'm trying to leverage this new feature to make some extension methods that will work on all enums. ...
2
votes
0answers
102 views

C# 7.3 ref return with pinvoke

I would like to know what the difference is in using a ref return versus a pointer when doing pinvokes. The native method signature that I am calling is: typedef struct Buffer { const void* Data; ...
0
votes
1answer
27 views

Check items from a List of ValueTuple and return results in another List of ValueTuple C#7

Please consider the following list of ValueTuple C#7 static void Main(string[] args) { List<(double prices, int matches)> myList = new List<(double, int)>(); myList....
1
vote
2answers
131 views

Check individual items from a list of Valuetuple in c# 7

Please consider the following list of ValueTuple C#7 List<(double prices, int matches)> myList = new List<(double, int)>(); myList.Add((100, 9)); ...
0
votes
1answer
73 views

MSDN Tuple equality C#7.3 weird line

I was reading about the ValueTuple on MSDN and there is one line of code I don't understand in this sample presenting C#7.3's new tuple equality : var left = (a: 5, b: 10); var right = (a: 5, b: 10); ...
0
votes
0answers
80 views

How to get the peak memory usage (PeakWorkingSet64) for each instance in a single process?

I am running my algorithm on 734 instances one by one. I understand that I can get my Process, and access the peak memory usage so far; see below: Process proc = Process.GetCurrentProcess(); long ...
0
votes
0answers
137 views

How are threads reused in Asp Net Core

Hello i am building an tcp server where i will have multiple clients connected that will send and receive data to the server. I want to know if the framework does not create a 1:1 Thread to Client ...
4
votes
0answers
385 views

When to use in keyword with ReadOnlyMemory or ReadOnlySpan?

I was wondering what would be usage of the in keyword with ReadOnlyMemory<T> and ReadOnlySpan<T>. void Method(ReadOnlyMemory<int> memory) { // Code that modifies memory wont ...
4
votes
1answer
360 views

How does stackalloc work?

Hello i am trying to figure out how does stackalloc work.So coming from C/C++ from my knowledge (limited) you can not allocate memory on the stack dynamically like in here: C/C++ example: void ...
7
votes
1answer
321 views

Why can a full property in C# be overridden with only a getter but it can still be set?

I came across a behavior that surprises me. Given the following two classes: class Parent { public virtual bool Property { get; set; } } class Child : Parent { public override bool Property ...
4
votes
1answer
2k views

Writing byte array to Span and sending it with Memory

I am receiving a buffer and i want from it to create a new buffer ( concatenating bytes prefixed,infixed and postfixed) and send it later on to a socket. Eg: Initial buffer: "aaaa" Final buffer: "$...
37
votes
2answers
3k views

C# 7.3 Enum constraint: Why can't I use the nullable enum?

Now that we have enum constraint, why doesn't compiler allow me to write this code? public static TResult? ToEnum<TResult>(this String value, TResult? defaultValue) where TResult : Enum { ...
10
votes
1answer
419 views

Why is a generic type constrained by 'Enum' failing to qualify as a 'struct' in C# 7.3?

If I have a generic interface with a struct constraint like this: public interface IStruct<T> where T : struct { } I can supply an enumeration as my type T like so, because an enum satisfies a ...
3
votes
3answers
4k views

VS Code CSC : error CS1617: Invalid option '7.3' for /langversion

I downloaded VS 2017 15.7, .NET Core 2.1.2 and Blazor to try it out. It wasn't working in VS 2017 properly and thought I would try through the dotnet cli and VS Code. Instead I was met with the ...
19
votes
1answer
2k views

C# 7.3 Enum constraint: Why can't I use the enum keyword?

To constrain a generic type parameter to be of an enum type, I previously constrained them like this, which was the best I could go for constraining type T for enums in pre-C# 7.3: void DoSomething&...