178,284 questions
210
votes
9
answers
181k
views
Automating the InvokeRequired code pattern
I have become painfully aware of just how often one needs to write the following code pattern in event-driven GUI code, where
private void DoGUISwitch() {
// cruisin for a bruisin' through ...
23
votes
1
answer
17k
views
Using SetWindowPos with multiple monitors
Using user32.dll and C# I wrote the method that you see below. Using a process handle for a window, it will set the window position at a provided (x, y) location.
However, in a multi-monitored ...
322
votes
7
answers
207k
views
What does the => operator mean in a property or method?
I came across some code that said
public int MaxHealth =>
Memory[Address].IsValid ?
Memory[Address].Read<int>(Offs.Life.MaxHp) :
0;
Now I am somewhat familiar ...
174
votes
4
answers
12k
views
Why covariance and contravariance do not support value type
IEnumerable<T> is co-variant but it does not support value type, just only reference type. The below simple code is compiled successfully:
IEnumerable<string> strList = new List<string&...
419
votes
10
answers
448k
views
Process.start: how to get the output?
I would like to run an external command line program from my Mono/.NET app.
For example, I would like to run mencoder. Is it possible:
To get the command line shell output, and write it on my text ...
142
votes
18
answers
97k
views
Is polymorphic deserialization possible in System.Text.Json?
I try to migrate from Newtonsoft.Json to System.Text.Json.
I want to deserialize abstract class. Newtonsoft.Json has TypeNameHandling for this.
Is there any way to deserialize abstract class via ...
107
votes
8
answers
132k
views
How do I match an entire string with a regex?
I need a regex that will only find matches where the entire string matches my query.
For instance if I do a search for movies with the name "Red October" I only want to match on that exact title (...
17
votes
2
answers
13k
views
How to post messages to an STA thread running a message pump?
So, following this, I decided to explicitly instantiate a COM object on a dedicated STA thread. Experiments showed that the COM object needed a message pump, which I created by calling Application.Run(...
968
votes
27
answers
1.6m
views
Identify if a string is a number
If I have these strings:
"abc" = false
"123" = true
"ab2" = false
Is there a command, like IsNumeric() or something else, that can identify if a string is a valid number?
420
votes
13
answers
207k
views
Use of Finalize/Dispose method in C#
C# 2008
I have been working on this for a while now, and I am still confused about the use of finalize and dispose methods in code. My questions are below:
I know that we only need a finalizer while ...
317
votes
29
answers
645k
views
Convert generic List/Enumerable to DataTable?
I have few methods that returns different Generic Lists.
Exists in .net any class static method or whatever to convert any list into a datatable? The only thing that i can imagine is use Reflection ...
131
votes
6
answers
313k
views
A field initializer cannot reference the nonstatic field, method, or property
I have a class and when I try to use it in another class I receive the error below.
using System;
using System.Collections.Generic;
using System.Linq;
namespace MySite
{
public class Reminders
...
333
votes
9
answers
402k
views
What is the C# Using block and why should I use it? [duplicate]
What is the purpose of the Using block in C#? How is it different from a local variable?
421
votes
24
answers
394k
views
How to recursively list all the files in a directory in C#?
How to recursively list all the files in a directory and child directories in C#?
111
votes
18
answers
95k
views
Is there a string math evaluator in .NET?
If I have a string with a valid math expression such as:
String s = "1 + 2 * 7";
Is there a built in library/function in .NET that will parse and evaluate that expression for me and return the result?...
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 ...
447
votes
18
answers
575k
views
How to remove all event handlers from an event
To create a new event handler on a control you can do this
c.Click += new EventHandler(mainFormButton_Click);
or this
c.Click += mainFormButton_Click;
and to remove an event handler you can do this
...
270
votes
16
answers
341k
views
How to dynamically create a class?
I have a class which looks like this:
public class Field
{
public string FieldName;
public string FieldType;
}
And an object List<Field> with values:
{"EmployeeID","int"},
{"...
4419
votes
35
answers
1.1m
views
How can I enumerate an enum?
How can you enumerate an enum in C#?
E.g., the following code does not compile:
public enum Suit
{
Spades,
Hearts,
Clubs,
Diamonds
}
public void EnumerateAllSuitsDemoMethod()
{
...
586
votes
7
answers
439k
views
Can I escape a double quote in a verbatim string literal?
In a verbatim string literal (@"foo") in C#, backslashes aren't treated as escapes, so doing \" to get a double quote doesn't work. Is there any way to get a double quote in a verbatim string literal?
...
412
votes
14
answers
150k
views
Can't operator == be applied to generic types in C#?
According to the documentation of the == operator in MSDN,
For predefined value types, the
equality operator (==) returns true if
the values of its operands are equal,
false otherwise. For ...
124
votes
13
answers
228k
views
Evaluating string "3*(4+2)" yield int 18 [duplicate]
Is there a function the .NET framework that can evaluate a numeric expression contained in a string and return the result? F.e.:
string mystring = "3*(2+4)";
int result = EvaluateExpression(mystring);...
59
votes
3
answers
46k
views
ASP.NET MVC 5 culture in route and url
I've translated my mvc website, which is working great. If I select another language (Dutch or English) the content gets translated.
This works because I set the culture in the session.
Now I want to ...
675
votes
19
answers
416k
views
Getting all types that implement an interface
Using reflection, how can I get all types that implement an interface with C# 3.0/.NET 3.5 with the least code, and minimizing iterations?
This is what I want to re-write:
foreach (Type t in this....
302
votes
13
answers
873k
views
How can I convert a JSON object to a custom C# object?
How can I populate my C# object with the JSON object passed via AJAX?
This is the JSON object passed to a C# web method from the page using JSON.stringify:
{
"user": {
"name&...