340,756 questions
538
votes
8
answers
528k
views
.NET / C# - Convert char[] to string
What is the proper way to turn a char[] into a string?
The ToString() method from an array of characters doesn't do the trick.
527
votes
9
answers
384k
views
What is the best data type to use for money in C#?
What is the best data type to use for money in C#?
517
votes
14
answers
196k
views
Difference between Math.Floor() and Math.Truncate()
What is the difference between Math.Floor() and Math.Truncate() in .NET?
511
votes
11
answers
241k
views
Equivalent of Math.Min & Math.Max for Dates?
What's the quickest and easiest way to get the Min (or Max) value between two dates? Is there an equivalent to Math.Min (& Math.Max) for dates?
I want to do something like:
if (Math.Min(Date1, ...
511
votes
12
answers
492k
views
ArrayList vs List<> in C#
What is the difference between ArrayList and List<> in C#?
Is it only that List<> has a type while ArrayList doesn't?
503
votes
25
answers
417k
views
Better way to check if a Path is a File or a Directory?
I am processing a TreeView of directories and files. A user can select either a file or a directory and then do something with it. This requires me to have a method which performs different actions ...
500
votes
7
answers
259k
views
Internal vs. Private Access Modifiers
What is the difference between the internal and private access modifiers in C#?
497
votes
14
answers
954k
views
Easiest way to read from and write to files
There are a lot of different ways to read and write files (text files, not binary) in C#.
I just need something that is easy and uses the least amount of code, because I am going to be working with ...
496
votes
11
answers
464k
views
C# getting the path of %AppData%
C# 2008 SP1
I am using the code below:
dt.ReadXml("%AppData%\\DateLinks.xml");
However, I am getting an exception that points to the location of where my application is running from:
Could not ...
494
votes
15
answers
350k
views
Why does Math.Round(2.5) return 2 instead of 3?
In C#, the result of Math.Round(2.5) is 2.
It is supposed to be 3, isn't it? Why is it 2 instead in C#?
492
votes
17
answers
273k
views
Passing arguments to C# generic new() of templated type
I'm trying to create a new object of type T via its constructor when adding to the list.
I'm getting a compile error: The error message is:
'T': cannot provide arguments when creating an instance ...
490
votes
7
answers
509k
views
What is the string length of a GUID?
I want to create a varchar column in SQL that should contain N'guid' while guid is a generated GUID by .NET (Guid.NewGuid) - class System.Guid.
What is the length of the varchar I should expect from ...
488
votes
20
answers
217k
views
Why is there no ForEach extension method on IEnumerable?
Inspired by another question asking about the missing Zip function:
Why is there no ForEach extension method on the IEnumerable interface? Or anywhere? The only class that gets a ForEach method is ...
484
votes
21
answers
324k
views
Is there an easy way to return a string repeated X number of times?
I'm trying to insert a certain number of indentations before a string based on an items depth and I'm wondering if there is a way to return a string repeated X times. Example:
string indent = "---";
...
481
votes
7
answers
599k
views
How to get index using LINQ? [duplicate]
Given a datasource like that:
var c = new Car[]
{
new Car{ Color="Blue", Price=28000},
new Car{ Color="Red", Price=54000},
new Car{ Color="Pink", Price=9999},
// ..
};
How can I find the ...
478
votes
150
answers
135k
views
What are your favorite extension methods for C#? (codeplex.com/extensionoverflow)
Let's make a list of answers where you post your excellent and favorite extension methods.
The requirement is that the full code must be posted and a example and an explanation on how to use it.
...
476
votes
16
answers
318k
views
When should I use a List vs a LinkedList
When is it better to use a List vs a LinkedList?
473
votes
32
answers
859k
views
Find and extract a number from a string
I have a requirement to find and extract a number contained within a string.
For example, from these strings:
string test = "1 test"
string test1 = " 1 test"
string test2 = "test 99"
How can I do ...
465
votes
18
answers
1.6m
views
How do I make calls to a REST API using C#?
This is the code I have so far:
public class Class1
{
private const string URL = "https://sub.domain.com/objects.json?api_key=123";
private const string DATA = @"...
463
votes
3
answers
402k
views
How to get temporary folder for current user
Currently I am using following function to get the temporary folder path for current user:
string tempPath = System.IO.Path.GetTempPath();
On some machines it gives me temp folder path of current ...
458
votes
6
answers
233k
views
What's the difference between Invoke() and BeginInvoke()
Just wondering what the difference between BeginInvoke() and Invoke() are?
Mainly what each one would be used for.
EDIT: What is the difference between creating a threading object and calling ...
453
votes
41
answers
337k
views
In .NET, which loop runs faster, 'for' or 'foreach'?
In C#/VB.NET/.NET, which loop runs faster, for or foreach?
Ever since I read that a for loop works faster than a foreach loop a long time ago I assumed it stood true for all collections, generic ...
450
votes
5
answers
55k
views
How is Math.Pow() implemented in .NET Framework?
I was looking for an efficient approach for calculating ab (say a = 2 and b = 50). To start things up, I decided to take a look at the implementation of Math.Pow() function. But in .NET Reflector, all ...
448
votes
20
answers
420k
views
Introducing FOREIGN KEY constraint may cause cycles or multiple cascade paths - why?
I've been wrestling with this for a while and can't quite figure out what's happening. I have a Card entity which contains Sides (usually 2) - and both Cards and Sides have a Stage. I'm using EF ...
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
...