178,284 questions
700
votes
28
answers
1.3m
views
Return multiple values to a method caller
I read the C++ version of this question but didn't really understand it.
Can someone please explain clearly if it can be done in C#, and how?
248
votes
31
answers
241k
views
When do you use the "this" keyword? [closed]
I was curious about how other people use the this keyword. I tend to use it in constructors, but I may also use it throughout the class in other methods. Some examples:
In a constructor:
public ...
99
votes
20
answers
444k
views
Send values from one form to another form
I want to pass values between two Forms (c#). How can I do it?
I have two forms: Form1 and Form2.
Form1 contains one button. When I click on that button, Form2 should open and Form1 should be in ...
17
votes
2
answers
24k
views
What is the best way to save game state?
I find the best way to save game data in Unity3D Game engine.
At first, I serialize objects using BinaryFormatter.
But I heard this way has some issues and is not suitable for save.
So, What is the ...
1472
votes
296
answers
762k
views
Hidden Features of C#? [closed]
This came to my mind after I learned the following from this question:
where T : struct
We, C# developers, all know the basics of C#. I mean declarations, conditionals, loops, operators, etc.
Some ...
259
votes
19
answers
188k
views
LINQ - Full Outer Join
I have a list of people's ID and their first name, and a list of people's ID and their surname. Some people don't have a first name and some don't have a surname; I'd like to do a full outer join on ...
75
votes
12
answers
56k
views
How to stop BackgroundWorker on Form's Closing event?
I have a form that spawns a BackgroundWorker, that should update form's own textbox (on main thread), hence Invoke((Action) (...)); call.
If in HandleClosingEvent I just do bgWorker.CancelAsync() then ...
222
votes
13
answers
421k
views
Reading CSV files using C#
I'm writing a simple import application and need to read a CSV file, show result in a DataGrid and show corrupted lines of the CSV file in another grid. For example, show the lines that are shorter ...
730
votes
20
answers
685k
views
Best way to parse command line arguments in C#? [closed]
When building console applications that take parameters, you can use the arguments passed to Main(string[] args).
In the past I've simply indexed/looped that array and done a few regular expressions ...
107
votes
11
answers
479k
views
How to make the script wait/sleep in a simple way in unity
How can I put a sleep function between the TextUI.text = ...., to wait 3 seconds between each phrase?
public Text GuessUI;
public Text TextUI;
[...truncated...]
TextUI.text = "Welcome to Number ...
1178
votes
11
answers
496k
views
DateTime vs DateTimeOffset
What is the difference between a DateTime and a DateTimeOffset and when should one be used?
Currently, we have a standard way of dealing with .NET DateTimes in a TimeZone-aware way: Whenever we ...
75
votes
6
answers
68k
views
How do parameterized queries help against SQL injection?
In both queries 1 and 2, the text from the textbox is inserted into the database. What's the significance of the parameterized query here?
Passing txtTagNumber as a query parameter
SqlCommand cmd = ...
19
votes
3
answers
32k
views
Why is floating point arithmetic in C# imprecise?
Why does the following program print what it prints?
class Program
{
static void Main(string[] args)
{
float f1 = 0.09f*100f;
float f2 = 0.09f*99.999999f;
Console....
741
votes
36
answers
450k
views
How do you create a dropdownlist from an enum in ASP.NET MVC?
I'm trying to use the Html.DropDownList extension method but can't figure out how to use it with an enumeration.
Let's say I have an enumeration like this:
public enum ItemTypes
{
Movie = 1,
...
181
votes
7
answers
252k
views
Elevating process privilege programmatically?
I'm trying to install a service using InstallUtil.exe but invoked through Process.Start. Here's the code:
ProcessStartInfo startInfo = new ProcessStartInfo (m_strInstallUtil, strExePath);
System....
599
votes
15
answers
590k
views
Validate a username and password against Active Directory?
How can I validate a username and password against Active Directory? I simply want to check if a username and password are correct.
576
votes
13
answers
393k
views
How do I copy the contents of one stream to another?
What is the best way to copy the contents of one stream to another? Is there a standard utility method for this?
264
votes
7
answers
174k
views
What is the difference between i++ and ++i in C#?
I've seen them both being used in numerous pieces of C# code, and I'd like to know when to use i++ and when to use ++i?
(i being a number variable like int, float, double, etc).
538
votes
18
answers
192k
views
Why is lock(this) {...} bad?
The MSDN documentation says that
public class SomeObject
{
public void SomeOperation()
{
lock(this)
{
//Access instance variables
}
}
}
is "a problem if the instance can ...
188
votes
8
answers
147k
views
Filtering on Include in EF Core
I'm trying to filter on the initial query. I have nested include leafs off a model. I'm trying to filter based on a property on one of the includes. For example:
using (var context = new ...
316
votes
21
answers
330k
views
How can I detect the encoding/codepage of a text file?
In our application, we receive text files (.txt, .csv, etc.) from diverse sources. When reading, these files sometimes contain garbage, because the files where created in a different/unknown codepage.
...
939
votes
62
answers
1.4m
views
The located assembly's manifest definition does not match the assembly reference
I am trying to run some unit tests in a C# Windows Forms application (Visual Studio 2005), and I get the following error:
System.IO.FileLoadException: Could not load file or assembly 'Utility, ...
393
votes
9
answers
350k
views
Set object property using reflection
Is there a way in C# where I can use reflection to set an object property?
Ex:
MyObject obj = new MyObject();
obj.Name = "Value";
I want to set obj.Name with reflection. Something like:
Reflection....
632
votes
28
answers
593k
views
How to remove elements from a generic list while iterating over it?
I am looking for a better pattern for working with a list of elements which each need processed and then depending on the outcome are removed from the list.
You can't use .Remove(element) inside a ...
430
votes
12
answers
250k
views
Do HttpClient and HttpClientHandler have to be disposed between requests?
System.Net.Http.HttpClient and System.Net.Http.HttpClientHandler in .NET Framework 4.5 implement IDisposable (via System.Net.Http.HttpMessageInvoker).
The using statement documentation says:
As a ...