Skip to main content
The 2024 Developer Survey results are live! See the results

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.

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 ...
Adriano Repetti's user avatar
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: ...
Ivan Prodanov's user avatar
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 ...
CruelIO's user avatar
  • 18.5k
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
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 ...
RKh's user avatar
  • 14k
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(() =&...
Morgan Cheng's user avatar
  • 75.4k
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 ...
Prerak K's user avatar
  • 11.1k
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 ...
NakedBrunch's user avatar
  • 49.1k
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 ...
mirezus's user avatar
  • 14.2k
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 ...
user avatar
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", "...
dil33pm's user avatar
  • 1,613
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) { } ...
HAdes's user avatar
  • 16.9k
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 ...
Dirk Vollmar's user avatar

15 30 50 per page
1
2 3 4 5
11628