Skip to main content
How are we doing? Please help us improve Stack Overflow. Take our short survey

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.

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....
stackoverflowuser's user avatar
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 ...
pedrofernandes's user avatar
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 ...
cdonner's user avatar
  • 37.4k
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.
jswanson's user avatar
  • 16.4k
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: ...
Sreedhar's user avatar
  • 29.8k
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 ...
Pop Catalin's user avatar
  • 62.6k
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 ...
BCS's user avatar
  • 77.6k
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>?
Richard Nagle's user avatar
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 ...
Calanus's user avatar
  • 26.1k
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()) { ...
mynameiscoffey's user avatar
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 ...
JSmyth's user avatar
  • 12.1k
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 ...
mirezus's user avatar
  • 14.1k
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 ...
David Reis's user avatar
  • 12.8k
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 = ...
Bob's user avatar
  • 99k
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 ...
Daniel Fortunov's user avatar

15 30 50 per page
1
3 4
5
6 7
108012