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.
174,413
questions
1867
votes
26
answers
2.3m
views
What is a NullReferenceException, and how do I fix it?
I have some code and when it executes, it throws a NullReferenceException, saying:
Object reference not set to an instance of an object.
What does this mean, and what can I do to fix this error?
231
votes
5
answers
357k
views
What is an IndexOutOfRangeException / ArgumentOutOfRangeException and how do I fix it?
I have some code and when it executes, it throws a IndexOutOfRangeException, saying,
Index was outside the bounds of the array.
What does this mean, and what can I do about it?
Depending on ...
844
votes
15
answers
250k
views
Random number generator only generating one random number
I have the following function:
//Function to get random number
public static int RandomNumber(int min, int max)
{
Random random = new Random();
return random.Next(min, max);
}
How I call it:
...
1599
votes
47
answers
843k
views
How do I update the GUI from another thread?
Which is the simplest way to update a Label from another Thread?
I have a Form running on thread1, and from that I'm starting another thread (thread2).
While thread2 is processing some files I would ...
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 ...
83
votes
2
answers
14k
views
Post an HTML Table to ADO.NET DataTable
I have a HTML table as below in my View:
<table id="tblCurrentYear">
<tr>
<td>Leave Type</td>
<td>Leave Taken</td>
<td>Leave ...
288
votes
11
answers
78k
views
Captured variable in a loop in C#
I met an interesting issue about C#. I have code like below.
List<Func<int>> actions = new List<Func<int>>();
int variable = 0;
while (variable < 5)
{
actions.Add(() =&...
681
votes
22
answers
514k
views
Cross-thread operation not valid: Control accessed from a thread other than the thread it was created on
I have a scenario. (Windows Forms, C#, .NET)
There is a main form which hosts some user control.
The user control does some heavy data operation, such that if I directly call the UserControl_Load ...
2647
votes
59
answers
1.0m
views
Deep cloning objects
I want to do something like:
MyObject myObj = GetMyObj(); // Create and fill a new object
MyObject newObj = myObj.Clone();
And then make changes to the new object that are not reflected in the ...
1650
votes
53
answers
1.2m
views
How do you convert a byte array to a hexadecimal string, and vice versa?
How can you convert a byte array to a hexadecimal string and vice versa?
1134
votes
32
answers
691k
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 ...
565
votes
19
answers
1.5m
views
How can I deserialize JSON with C#?
I have the following code:
var user = (Dictionary<string, object>)serializer.DeserializeObject(responsecontent);
The input in responsecontent is JSON, but it is not properly deserialized into ...
144
votes
11
answers
289k
views
Serialize and Deserialize Json and Json Array in Unity
I have a list of items send from a PHP file to unity using WWW.
The WWW.text looks like:
[
{
"playerId": "1",
"playerLoc": "Powai"
},
{
"playerId": "2",
"...
807
votes
43
answers
351k
views
How do I properly clean up Excel interop objects?
I'm using the Excel interop in C# (ApplicationClass) and have placed the following code in my finally clause:
while (System.Runtime.InteropServices.Marshal.ReleaseComObject(excelSheet) != 0) { }
...
557
votes
16
answers
109k
views
Why are mutable structs “evil”?
Following the discussions here on SO I already read several times the remark that mutable structs are “evil” (like in the answer to this question).
What's the actual problem with mutability and ...