340,756 questions
7595
votes
67
answers
1.4m
views
What is the difference between String and string in C#?
What are the differences between these two, and which one should I use?
string s = "Hello world!";
String s = "Hello world!";
2923
votes
13
answers
441k
views
What are the correct version numbers for C#?
What are the correct version numbers for C#? What came out when? Why can't I find any answers about C# 3.5?
This question is primarily to aid those who are searching for an answer using an incorrect ...
2631
votes
29
answers
909k
views
Catch multiple exceptions at once?
It is discouraged to catch System.Exception errors. Instead, only the "known" exceptions should be caught.
This sometimes leads to unnecessary repetitive code, for example:
try
{
WebId = ...
2440
votes
15
answers
293k
views
Should 'using' directives be inside or outside the namespace in C#?
I have been running StyleCop over some C# code, and it keeps reporting that my using directives should be inside the namespace.
Is there a technical reason for putting the using directives inside ...
2271
votes
76
answers
860k
views
How do I calculate someone's age based on a DateTime type birthday?
Given a DateTime representing a person's birthday, how do I calculate their age in years?
2136
votes
117
answers
1.9m
views
How do I remedy "The breakpoint will not currently be hit. No symbols have been loaded for this document." warning?
A C# desktop application (on the Visual Studio Express edition) worked, but then it didn't work 5 seconds later.
I tried the following:
Ensure debug configuration, debug flag, and full debug ...
1881
votes
10
answers
1.5m
views
Calling the base constructor in C#
If I inherit from a base class and want to pass something from the constructor of the inherited class to the constructor of the base class, how do I do that?
For example, if I inherit from the ...
1786
votes
8
answers
1.0m
views
How to loop through all enum values in C#? [duplicate]
public enum Foos
{
A,
B,
C
}
Is there a way to loop through the possible values of Foos?
Basically?
foreach(Foo in Foos)
1733
votes
30
answers
542k
views
What is the difference between const and readonly in C#?
What is the difference between const and readonly in C#?
When would you use one over the other?
1637
votes
6
answers
122k
views
Try-catch speeding up my code?
I wrote some code for testing the impact of try-catch, but seeing some surprising results.
static void Main(string[] args)
{
Thread.CurrentThread.Priority = ThreadPriority.Highest;
Process....
1583
votes
19
answers
630k
views
Why is Dictionary preferred over Hashtable in C#?
In most programming languages, dictionaries are preferred over hashtables.
What are the reasons behind that?
1500
votes
17
answers
2.6m
views
Send HTTP POST request in .NET
How can I make an HTTP POST request and send data in the body?
1355
votes
61
answers
889k
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 ...
1341
votes
39
answers
1.3m
views
How can I generate random alphanumeric strings?
How can I generate a random 8 character alphanumeric string in C#?
1288
votes
15
answers
2.3m
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 ...
1199
votes
15
answers
817k
views
How to group by multiple columns using LINQ
How can I do group by multiple columns in LINQ?
Something similar to this in SQL:
SELECT * FROM <TableName> GROUP BY <Column1>,<Column2>
How can I convert this to LINQ:
...
1186
votes
16
answers
1.6m
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 ...
1154
votes
22
answers
1.2m
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 ...
1089
votes
13
answers
646k
views
Html.Partial vs Html.RenderPartial & Html.Action vs Html.RenderAction
In ASP.NET MVC, what is the difference between:
Html.Partial and Html.RenderPartial
Html.Action and Html.RenderAction
1067
votes
1
answer
315k
views
Escape curly brace '{' in String.Format [duplicate]
How do I display a literal curly brace character when using the String.Format method?
Example:
sb.AppendLine(String.Format("public {0} {1} { get; private set; }",
prop.Type, prop.Name));
I would ...
1028
votes
14
answers
247k
views
What is the difference between .NET Core and .NET Standard Class Library project types?
In Visual Studio, there are at least three different types of class libraries you can create:
Class Library (.NET Framework)
Class Library (.NET Standard)
Class Library (.NET Core)
While the first ...
1006
votes
21
answers
747k
views
Is there a way to check if a file is in use?
I'm writing a program in C# that needs to repeatedly access 1 image file. Most of the time it works, but if my computer's running fast, it will try to access the file before it's been saved back to ...
989
votes
17
answers
785k
views
split a string on newlines in .NET
I need to split a string into newlines in .NET and the only way I know of to split strings is with the Split method. However that will not allow me to (easily) split on a newline, so what is the best ...
971
votes
28
answers
1.4m
views
Reading settings from app.config or web.config in .NET
I'm working on a C# class library that needs to be able to read settings from the web.config or app.config file (depending on whether the DLL is referenced from an ASP.NET web application or a Windows ...
971
votes
27
answers
680k
views
Sending email in .NET through Gmail
Instead of relying on my host to send an email, I was thinking of sending the email messages using my Gmail account. The emails are personalized emails to the bands I play on my show.
Is it possible ...