Questions tagged [c#]
C# (pronounced "see sharp") is a high-level, statically typed, multi-paradigm programming language developed by Microsoft. C# code usually targets Microsoft's .NET ecosystem, which include .NET, .NET Framework, .NET MAUI, and Xamarin among others. Use this tag for questions about code written in C# or about C#'s formal specification.
1,620,177
questions
1217
votes
14
answers
283k
views
Returning IEnumerable<T> vs. IQueryable<T>
What is the difference between returning IQueryable<T> vs. IEnumerable<T>, when should one be preferred over the other?
IQueryable<Customer> custs = from c in db.Customers
where c....
1196
votes
25
answers
1.1m
views
Get property value from string using reflection
I am trying implement the Data transformation using Reflection1 example in my code.
The GetSourceValue function has a switch comparing various types, but I want to remove these types and properties ...
1196
votes
16
answers
1.0m
views
Collection was modified; enumeration operation may not execute
I can't get to the bottom of this error, because when the debugger is attached, it does not seem to occur.
Collection was modified; enumeration operation may not execute
Below is the code.
This is a ...
1189
votes
31
answers
1.1m
views
Deserialize JSON into C# dynamic object? [duplicate]
Is there a way to deserialize JSON content into a C# dynamic type? It would be nice to skip creating a bunch of classes in order to use the DataContractJsonSerializer.
1186
votes
15
answers
797k
views
Group By Multiple Columns
How can I do GroupBy multiple columns in LINQ
Something similar to this in SQL:
SELECT * FROM <TableName> GROUP BY <Column1>,<Column2>
How can I convert this to LINQ:
...
1182
votes
13
answers
380k
views
How to escape braces (curly brackets) in a format string in .NET
How can brackets be escaped in using string.Format?
For example:
String val = "1,2,3"
String.Format(" foo {{0}}", val);
This example doesn't throw an exception, but it outputs the ...
1160
votes
16
answers
1.5m
views
How to convert UTF-8 byte[] to string
I have a byte[] array that is loaded from a file that I happen to known contains UTF-8.
In some debugging code, I need to convert it to a string. Is there a one-liner that will do this?
Under the ...
1149
votes
12
answers
310k
views
Why would you use Expression<Func<T>> rather than Func<T>?
I understand lambdas and the Func and Action delegates. But expressions
stump me.
In what circumstances would you use an Expression<Func<T>> rather than a plain old Func<T>?
1149
votes
22
answers
1.1m
views
LINQ query on a DataTable
I'm trying to perform a LINQ query on a DataTable object and bizarrely I am finding that performing such queries on DataTables is not straightforward. For example:
var results = from myRow in ...
1149
votes
24
answers
1.5m
views
How to set the Content-Type header for an HttpClient request?
I'm trying to set the Content-Type header of an HttpClient object as required by an API I am calling.
I tried setting the Content-Type like below:
using (var httpClient = new HttpClient())
{
...
1137
votes
30
answers
1.3m
views
How can I get the application's path in a .NET console application?
How do I find the application's path in a console application?
In Windows Forms, I can use Application.StartupPath to find the current path, but this doesn't seem to be available in a console ...
1133
votes
32
answers
690k
views
Randomize a List<T>
What is the best way to randomize the order of a generic list in C#? I've got a finite set of 75 numbers in a list I would like to assign a random order to, in order to draw them for a lottery type ...
1131
votes
10
answers
438k
views
DateTime vs DateTimeOffset
What is the difference between a DateTime and a DateTimeOffset and when should one be used?
Currently, we have a standard way of dealing with .NET DateTimes in a TimeZone-aware way: Whenever we ...
1113
votes
19
answers
1.2m
views
Creating a byte array from a stream
What is the prefered method for creating a byte array from an input stream?
Here is my current solution with .NET 3.5.
Stream s;
byte[] b;
using (BinaryReader br = new BinaryReader(s))
{
b = ...
1066
votes
24
answers
954k
views
Creating a comma separated list from IList<string> or IEnumerable<string>
What is the cleanest way to create a comma-separated list of string values from an IList<string> or IEnumerable<string>?
String.Join(...) operates on a string[] so can be cumbersome to ...