178,284 questions
1866
votes
26
answers
2.5m
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?
848
votes
15
answers
253k
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:
...
1315
votes
10
answers
375k
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 ...
294
votes
11
answers
83k
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(() =&...
1670
votes
53
answers
1.3m
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?
1148
votes
32
answers
725k
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 ...
148
votes
11
answers
299k
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",
"...
811
votes
43
answers
357k
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) { }
...
1600
votes
33
answers
765k
views
What is the difference between a field and a property?
In C#, what makes a field different from a property, and when should a field be used instead of a property?
121
votes
7
answers
69k
views
How to configure an app to run correctly on a machine with a high DPI setting (e.g. 150%)?
I've created a simple Winforms application in C#. When I run the application on a machine with high DPI settings (e.g. 150%), the application gets scaled up. So far so good!
But instead of rendering ...
115
votes
4
answers
161k
views
How to detect click/touch events on UI and GameObjects
How to detect UI object on Canvas on Touch in android?
For example, I have a canvas that have 5 objects such as Image, RawImage, Buttons, InputField and so on.
When I touch on Button UI object Then ...
2191
votes
49
answers
1.4m
views
How do I create an Excel (.XLS and .XLSX) file in C# without installing Microsoft Office?
How can I create an Excel spreadsheet with C# without requiring Excel to be installed on the machine that's running the code?
74
votes
4
answers
187k
views
What are good ways to prevent SQL injection? [duplicate]
I have to program an application management system for my OJT company. The front end will be done in C# and the back end in SQL.
Now I have never done a project of this scope before; in school we had ...
293
votes
9
answers
226k
views
Use of Application.DoEvents()
Can Application.DoEvents() be used in C#?
Is this function a way to allow the GUI to catch up with the rest of the app, in much the same way that VB6's DoEvents does?
84
votes
4
answers
58k
views
How to append whole set of model to formdata and obtain it in MVC
How do I pass a whole set model object through formdata and convert it to model type in the controller?
Below is what I've tried!
JavaScript part:
model = {
EventFromDate: fromDate,
...
66
votes
15
answers
556k
views
What is the best way to parse html in C#? [closed]
I'm looking for a library/method to parse an html file with more html specific features than generic xml parsing libraries.
1215
votes
24
answers
1.2m
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 ...
1424
votes
26
answers
1.3m
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'...
399
votes
8
answers
1.7m
views
CS0120: An object reference is required for the nonstatic field, method, or property 'foo'
Consider:
namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(...
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?
154
votes
21
answers
77k
views
Natural Sort Order in C#
Anyone have a good resource or provide a sample of a natural order sort in C# for an FileInfo array? I am implementing the IComparer interface in my sorts.
1812
votes
15
answers
726k
views
What does the [Flags] Enum Attribute mean in C#?
From time to time I see an enum like the following:
[Flags]
public enum Options
{
None = 0,
Option1 = 1,
Option2 = 2,
Option3 = 4,
Option4 = 8
}
I don't understand what ...
675
votes
25
answers
1.1m
views
LEFT OUTER JOIN in LINQ
How to perform left outer join in C# LINQ to objects without using join-on-equals-into clauses? Is there any way to do that with where clause?
Correct problem:
For inner join is easy and I have a ...
713
votes
10
answers
714k
views
How do you do a deep copy of an object in .NET? [duplicate]
I want a true deep copy. In Java, this was easy, but how do you do it in C#?
48
votes
3
answers
7k
views
How can I parse a JSON string that would cause illegal C# identifiers?
I have been using NewtonSoft JSON Convert library to parse and convert JSON string to C# objects. But now I have came across a really awkward JSON string and I am unable to convert it into C# object ...