Questions tagged [c#-7.0]
The version of C#, released in 2017 that added value tuples, local functions, basic pattern matching, ref locals and returns, async main, and various other new features. In most cases you should also specify the c# tag.
470
questions
0
votes
2
answers
20
views
C#7: How to use tuples in generic methods (LINQ select example)
I have some heavily repeating code, which has always the same structure, just using different columns in a database for accessing it and doing similar stuff
A typical query looks like:
var ...
0
votes
1
answer
67
views
F#: How can I define a computed property in a type?
In C#, I can define a computed property as such:
public class MyViewModel
{
public DateTime StartDate { get; set; }
public string StartDateFormatted => StartDate.ToString("yyyy.MM.dd h:...
1
vote
0
answers
74
views
I cannot use discards in c#
According to this link discards are supported since c# version 7.0 https://docs.microsoft.com/en-us/dotnet/csharp/fundamentals/functional/discards
If I write an #error version it says 7.3
This code ...
-1
votes
1
answer
65
views
What does it means class that implements IEnumerable<object[]>
What does it means class that implements IEnumerable<object[]> ? (in the specific case it is an abstract class but I'm not sure it is relevant)
Does it means that it should contains IEnumerable&...
1
vote
2
answers
174
views
Reflection: Create instance of a class that inherit from another class located in separate assembly
I created an empty web form application that run under .Net Framework 4.7.2 and created a webform and a class named MyClass:
namespace WebApplication1
{
public partial class WebForm1 : System.Web....
0
votes
0
answers
42
views
C# Catch exception from anonymous action delegate
One of the main requirements of this code was that it was readable and very easy for people unfimilar to C# to be able to make changes. The purpose of the code was the automation of tasks.
As such, I ...
0
votes
1
answer
442
views
.NET Framework 4.8 seems to use C# version older than 7
It seems like I have got problems with vs not handing inline var declarations.
It turns out that is uses an old version of c#.
Here comes a screenshot that shows that out string link is not compiling.
...
8
votes
2
answers
749
views
Why does the C# 'is' operator give a correct result when comparing two boolean values and should I use it?
Id noticed that this is operator produces the same results and even compiles the same (see this gist) as the == operator. Is this a correct usage of the is operator given that I can only see things ...
-3
votes
1
answer
61
views
How to return export path without using copy to memory2? [closed]
I working on web API asp.net core 3.1 I need to return export path direct without copying to memory
so How to do that Please ?
How to return export path without copy to memory as below
return File(...
0
votes
2
answers
157
views
how to await until parallel task done
I'm new with C#. I'm writing window form for sorting files inside folder according to it extension.
It's work when do the task with one folder but I modified it to do it from multiple item in listbox.
...
1
vote
1
answer
395
views
Binding Configurations to a Complex Object
In ASP.NET Core 2.2, I'm trying to bind an array of JSON objects to a matching C# object, but it's not binding the members properly. (Spoilers: Test1 works, Test2 doesn't.)
In my appsettings.json, I ...
0
votes
0
answers
43
views
Auto Infer type from the usage
I have a function that reads app settings from the config file.
public static T GetValue< T>([CallerMemberName] string settingName = null)
{
T settingsValue = default(T);
...
1
vote
1
answer
720
views
How to create web Api execute stored procedure
I work on SQL server 2012 and web API entity framework .NET core 2.2
so I face issue I can't implement web API execute stored Procedure below
Create proc ItemCalculateStock
@ItemId int = NULL,
@...
0
votes
2
answers
84
views
Join with last record of details table
Please consider these two tables in my database:
Header:
Id Name
-------------------------------
1 London
2 Berlin
3 Paris
...
2
votes
1
answer
155
views
Should I avoid using the C# "is" operator for equality checks? [closed]
A colleague told me I could use is and is not instead of the equality operators == and !=. I thought the is operator had something to do with types, and the official docs on MSDN agrees, but it also ...
0
votes
2
answers
98
views
Create a Tuple from 2 Lists in C#
I have 2 lists below:
// From Below List I want to retrieve it Text of each plan like: foreach(var plan in AvailablePlanNames) and then use plan.Text property.
private IList<IWebElement> ...
0
votes
5
answers
594
views
Generic Extension Method with Concrete Class Override
I have a third party DLL that returns objects like Customers, Orders, etc. I'll call them Your Entities. They do have a common IYourEntity interface so I can use that as a source constraint.
I want to ...
6
votes
1
answer
88
views
Any one know how exactly IL/CLR generates Local Functions C#7
https://www.infoworld.com/article/3182416/c-7-in-depth-exploring-local-functions.html
I really need to know exactly following code how Local Functions supposed to be.
Am 0% IL Code/MSIL experience.
...
-1
votes
2
answers
60
views
Trying to use reflection to concatenate lists of objects
I have below class
public class HydronicEquipment
{
public List<LibraryHydronicEquipment> Source { get; set; }
public List<LibraryHydronicEquipment> Distribution { get; set; }
...
1
vote
1
answer
924
views
Should we use Tuple or ValueTuple for reference-type only variables?
There's many questions contains only difference between Tuple(class) vs ValueTuple(struct)
All gone with ValueTuple where there's no GC pressure. But I don't get enough with why ValueTuple accepts all ...
0
votes
0
answers
78
views
How to use ValueTuple with a single item?
In an ASP.NET Core 5 MVC application, I have this call:
var result = ctx.ExecQuery<(string Item1)>("select '' as Item1");
This throws 5 compile-time errors starting at
Invalid ...
2
votes
1
answer
431
views
Discard all parameters of method (Remove unused parameter 'sender' and 'args')
The situation
When assigning a Click event to a button, I get a premade eventhandler which specifies an object sender and RoutedEventArgs args.
Now for my implementation of this eventhandler I don't ...
1
vote
3
answers
1k
views
Is params Keyword supports with ValueTuple as a parameter C# 7.0?
I search in StackOverflow, don't find any articles or anything related to that.
For example, the following example describes array of ValueTuple
(string CategoryName, params string[] Properties)[] ...
0
votes
1
answer
86
views
C# 7.0 standalone discard confusion | difference between _ = FooTask() and _ = Task.Run(FooTask)
I have a Task FooTask which should be called in background. A new feature in C# 7.0 is the discard function. As already asked (C# 7.0 standalone discard confusion) it is possible to call the Task in ...
14
votes
3
answers
504
views
What is the difference between discard and not assigning a variable?
In c# 7.0, you can use discards. What is the difference between using a discard and simply not assigning a variable?
public List<string> DoSomething(List<string> aList)
{
//does something ...
2
votes
1
answer
798
views
C# - Tuple arrays are mutable, but tuple lists are not. How do I get around this?
I have an array of value pairs I want to modify. I need to add and remove values from this array as well, so I used a list. When I tried to use a list, I encountered an error.
Error CS1612 - Cannot ...
0
votes
0
answers
19
views
How to Add Mutually Data Collection
For example, I will create a class called Person and the class will have a property called relatives that type of person. When we add relative to person it will also add the person to relatives of the ...
1
vote
1
answer
196
views
How to get expression from expression-bodied property c#
I want to get the Expression of the Expression-bodied Property. I have no idea how to do that;/ Here is the simple code snippet:
class TestTest
{
public int A { get; set; } = 5;
public int ...
3
votes
1
answer
144
views
Why is the "is"-operator in an if a longer scope then the if?
So my question is:
Why (and maybe how to avoid it) has the Is-operator in C# a longer lifetime as used in an if?
Example:
Animal a = new Cat();
if (a is Cat c)
{
Console.WriteLine(c); // Works
}
...
14
votes
5
answers
14k
views
Invalid option '7.3' for /langversion; must be ISO-1, ISO-2, Default or an integer in range 1 to 6
I'm using Visual Studio 17 (version 15.8.5), my project targets .NET Framework 4.8 and I've tried setting the C# version to use (via Build tab in the Properties window) C# 7.3 (that's the maximum ...
2
votes
1
answer
53
views
Should I use == or 'is' to check for null in C# 7?
from the C# 7 we have available is pattern matching operator. I would like to ask if it is recommended to use is operator rather than == to check for null. Is there any difference between these two ...
-1
votes
1
answer
37
views
Condition statement in C#7
i want to avoid following writing style.
pointed out simply out of range should be written once
var a = 100; // valid range 1..100_000 int32
var b = 10_000; // valid range 100..30_000_000 int32
if(...
1
vote
1
answer
89
views
Is there a performance penalty for returning a new struct in a getter?
I have a public getter returning a struct that never changes:
public Vector3 Center => new Vector3(0.5f, 0.5f, 0.5f);
It's not static, because it may change in the future, but for now it's always ...
3
votes
1
answer
33
views
are there collection classes for .net which allow ref return values?
I want to have a list of larger values types (e.g. vectors or matrices) and want to access them by reference like this:
collection[index].X = newvalue;
or
ref Vector v = collection[index];
v.X = ...
-1
votes
1
answer
113
views
My simple implementation of a DI container [open]
I decided to make my own light DI container.
I know about Zenject, etc. I wanted a very light analog.
I'm concerned about a few things:
1) is it correct to use struct instead of class for reference ...
1
vote
2
answers
634
views
Ref return doesn't work when struct is created in the method [duplicate]
Why wouldn't this work?
I create the struct and want to return it by ref.
public readonly struct AuditResult
{
public readonly bool AcceptChanges;
public readonly string Reason;
public ...
0
votes
1
answer
49
views
EF Core 3.1 property value returns default field value
I have a class property of enum type LogLevel (using Microsoft.Extensions.Logging) being stored in my database and a class field that ins't mapped but stores the enum type value as such:
class ...
{
...
0
votes
1
answer
39
views
Conditional If not returning as expected
for (int i = 0; i < iMatrix1.Count(); i++)
{
if (Math.Abs(iMatrix1[i]) > 1E-20 & Math.Abs(iMatrix1[i]) < 0.0001) { oArrayList.Add(0); }
else { oArrayList....
0
votes
1
answer
438
views
Converting C# 8.0 to C# 7.0
I was about to use the below C# code.
await using (var producerClient = new EventHubProducerClient(ConnectionString, EventHubName))
{
using EventDataBatch eventBatch = await producerClient....
1
vote
2
answers
759
views
Inferring only one type with multiple generic types [duplicate]
I have a generic method, defined like this:
public static A Test<A, B>(B b)
where A : new()
{
return new A();
}
I would like to be able to call it something like this:
int result = ...
5
votes
1
answer
509
views
Distinguish between getter-only property and expression body property?
Is it possible, using reflection, to distinguish between a getter-only property and an expression body property?
class MyClass
{
DateTime GetterOnly { get; }
DateTime ExpressionBody => ...
-5
votes
3
answers
223
views
Error when calling a function in C# because variable is used in parameter [closed]
I'm new to c#, but I'm trying to create a basic program that can use a function that adds to numbers (this is just practice I know it's inefficient.
{
int AddNumbers(int num1, ...
15
votes
2
answers
3k
views
Private methods vs local functions
To my understanding, both local functions and private methods serve merely as implementation details - helpers for public methods. Why would I want to choose one over the other?
When using a private ...
4
votes
3
answers
347
views
Using discards in C# for return methods [duplicate]
Does the use of discards in C# have any performance benefit? In Visual Studio 2019, the editor suggests using discards for return type methods that are not using the returned value.
1
vote
2
answers
2k
views
Local functions benefits with async methods
According to the Microsoft Docs:
There are two common use cases for local functions: public iterator methods and public async methods. Both types of methods generate code that reports errors later ...
3
votes
3
answers
636
views
pattern matching and unassigned local variable
I do:
(bool success, string name1, string name2) MyFunc() {
if (DateTime.Now.Year > 2020) return (false, "", ""); // error
return (true, "some value", "some value");
}
My coding style is to ...
0
votes
0
answers
34
views
When should an explicit field be used over auto-properties? [duplicate]
Now that C# includes automatic properties and private set, you can emulate basic traditional accessors and modifiers without an explicit field, controlling access. You can even initialize properties ...
1
vote
6
answers
924
views
Function returning named ValueTuple and success flag
I am running into this situation again and again, I want a function to return both a named ValueTuple and a success flag
All the options I can think of are not great :(
Option1:
bool MyFunc(T1 ...
1
vote
1
answer
86
views
How to declare event property in interface?
In C# 7.0, is it possible to declare an event property in an in a interface, something like:
delegate void Del();
interface I
{
event Del E { add; remove; }
}
Similar to properties?
0
votes
1
answer
3k
views
Can I use pattern matching for code like this (on generic types) [duplicate]
I want use pattern matching to replace multiple if statements as shown below in method Select<T>(). I want to use the switch() statement on typeof(T).
static class Program
{
static void Main(...