Skip to main content

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.

1437 votes
13 answers
1.1m views

Multiline string literal in C#

Is there an easy way to create a multiline string literal in C#? Here's what I have now: string query = "SELECT foo, bar" + " FROM table" + " WHERE id = 42"; I know PHP has <<<BLOCK ...
Chet's user avatar
  • 21.9k
1414 votes
26 answers
1.2m views

How and when to use ‘async’ and ‘await’

From my understanding one of the main things that async and await do is to make code easy to write and read - but is using them equal to spawning background threads to perform long duration logic? I'...
Dan Dinu's user avatar
  • 33.2k
1391 votes
16 answers
1.3m views

How to call asynchronous method from synchronous method in C#?

I have a public async Task Foo() method that I want to call from a synchronous method. So far all I have seen from MSDN documentation is calling async methods via async methods, but my whole program ...
Tower's user avatar
  • 101k
1373 votes
21 answers
774k views

Difference Between Select and SelectMany

I've been searching the difference between Select and SelectMany but I haven't been able to find a suitable answer. I need to learn the difference when using LINQ To SQL but all I've found are ...
Tarik's user avatar
  • 81.1k
1353 votes
3 answers
1.5m views

How do I encode and decode a base64 string?

How do I return a base64 encoded string given a string? How do I decode a base64 encoded string into a string?
Kevin Driedger's user avatar
1345 votes
4 answers
108k views

\d less efficient than [0-9]

I made a comment yesterday on an answer where someone had used [0123456789] in a regex rather than [0-9] or \d. I said it was probably more efficient to use a range or digit specifier than a character ...
weston's user avatar
  • 54.6k
1335 votes
11 answers
680k views

Call one constructor from another

I have two constructors which feed values to readonly fields. public class Sample { public Sample(string theIntAsString) { int i = int.Parse(theIntAsString); _intField = i; ...
Avi's user avatar
  • 16k
1321 votes
39 answers
1.2m views

How can I generate random alphanumeric strings?

How can I generate a random 8 character alphanumeric string in C#?
KingNestor's user avatar
  • 67.3k
1321 votes
37 answers
1.6m views

How do you get the index of the current iteration of a foreach loop?

Is there some rare language construct I haven't encountered (like the few I've learned recently, some on Stack Overflow) in C# to get a value representing the current iteration of a foreach loop? For ...
Matt Mitchell's user avatar
1316 votes
29 answers
1.3m views

Convert a string to an enum in C#

What's the best way to convert a string to an enumeration value in C#? I have an HTML select tag containing the values of an enumeration. When the page is posted, I want to pick up the value (which ...
Ben Mills's user avatar
  • 28.2k
1315 votes
61 answers
831k views

Could not find a part of the path ... bin\roslyn\csc.exe

I am trying to run an ASP.NET MVC (model-view-controller) project retrieved from TFS (Team Foundation Server) source control. I have added all assembly references and I am able to build and compile ...
Eyad's user avatar
  • 14k
1309 votes
5 answers
443k views

How to mark a method as obsolete or deprecated?

How do I mark a method as obsolete or deprecated using C#?
1301 votes
9 answers
365k views

How do I call a generic method using a Type variable?

What's the best way to call a generic method when the type parameter isn't known at compile time, but instead is obtained dynamically at runtime? Consider the following sample code - inside the ...
Bevan's user avatar
  • 44.1k
1292 votes
10 answers
1.9m views

Group by in LINQ

Let's suppose if we have a class like: class Person { internal int PersonID; internal string car; } I have a list of this class: List<Person> persons; And this list can have ...
test123's user avatar
  • 14.1k
1252 votes
15 answers
2.2m views

How do I turn a C# object into a JSON string in .NET?

I have classes like these: class MyDate { int year, month, day; } class Lad { string firstName; string lastName; MyDate dateOfBirth; } And I would like to turn a Lad object into a ...
Hui's user avatar
  • 14.2k

15 30 50 per page