50,724 questions
574
votes
12
answers
383k
views
HashSet vs. List performance
It's clear that a search performance of the generic HashSet<T> class is higher than of the generic List<T> class. Just compare the hash-based key with the linear approach in the List<T&...
274
votes
12
answers
249k
views
How to check for file lock? [duplicate]
Is there any way to check whether a file is locked without using a try/catch block?
Right now, the only way I know of is to just open the file and catch any System.IO.IOException.
266
votes
9
answers
239k
views
Linq to Entities - SQL "IN" clause
In T-SQL you could have a query like:
SELECT * FROM Users WHERE User_Rights IN ("Admin", "User", "Limited")
How would you replicate that in a LINQ to Entities query? Is it even possible?
213
votes
6
answers
201k
views
How to handle AccessViolationException
I am using a COM object (MODI) from within my .net application. The method I am calling throws a System.AccessViolationException, which is intercepted by Visual Studio. The odd thing is that I have ...
635
votes
29
answers
525k
views
Copy the entire contents of a directory in C#
I want to copy the entire contents of a directory from one location to another in C#.
There doesn't appear to be a way to do this using System.IO classes without lots of recursion.
There is a method ...
295
votes
8
answers
81k
views
TransactionScope automatically escalating to MSDTC on some machines?
In our project we're using TransactionScope's to ensure our data access layer performs it's actions in a transaction. We're aiming to not require the MSDTC service to be enabled on our end-user's ...
118
votes
14
answers
74k
views
Random row from Linq to Sql
What is the best (and fastest) way to retrieve a random row using Linq to SQL when I have a condition, e.g. some field must be true?
1733
votes
30
answers
542k
views
What is the difference between const and readonly in C#?
What is the difference between const and readonly in C#?
When would you use one over the other?
364
votes
19
answers
601k
views
Get URL parameters from a string in .NET
I've got a string in .NET which is actually a URL. I want an easy way to get the value from a particular parameter.
Normally, I'd just use Request.Params["theThingIWant"], but this string ...
238
votes
24
answers
229k
views
String vs. StringBuilder
I understand the difference between String and StringBuilder (StringBuilder being mutable) but is there a large performance difference between the two?
The program I’m working on has a lot of case ...
968
votes
14
answers
810k
views
When to use .First and when to use .FirstOrDefault with LINQ?
I've searched around and haven't really found a clear answer as to when you'd want to use .First and when you'd want to use .FirstOrDefault with LINQ.
When would you want to use .First? Only when you'...
514
votes
32
answers
216k
views
How do you unit test private methods?
I'm building a class library that will have some public & private methods. I want to be able to unit test the private methods (mostly while developing, but also it could be useful for future ...
455
votes
22
answers
1.3m
views
Reading CSV file and storing values into an array
I am trying to read a *.csv-file.
The *.csv-file consist of two columns separated by semicolon (";").
I am able to read the *.csv-file using StreamReader and able to separate each line by using the ...
252
votes
9
answers
327k
views
Show/Hide the console window of a C# console application
I googled around for information on how to hide one’s own console window. Amazingly, the only solutions I could find were hacky solutions that involved FindWindow() to find the console window by its ...
171
votes
4
answers
66k
views
Should I avoid 'async void' event handlers?
I know it is considered generally a bad idea to use fire-and-forget async void methods to start tasks, because there is no track of the pending task and it is tricky to handle exceptions which might ...
88
votes
8
answers
96k
views
What is the best choice for .NET inter-process communication? [closed]
Should I use Named Pipes, or .NET Remoting to communicate with a running process on my machine?
497
votes
16
answers
79k
views
Is DateTime.Now the best way to measure a function's performance? [closed]
I need to find a bottleneck and need to accurately as possible measure time.
Is the following code snippet the best way to measure the performance?
DateTime startTime = DateTime.Now;
// Some ...
447
votes
3
answers
420k
views
Task vs Thread differences [duplicate]
There are two classes available in .NET: Task and Thread.
What is the difference between those classes?
When is it better to use Thread over Task (and vice-versa)?
386
votes
7
answers
268k
views
HttpClient.GetAsync(...) never returns when using await/async
Edit: This question looks like it might be the same problem, but has no responses...
Edit: In test case 5 the task appears to be stuck in WaitingForActivation state.
I've encountered some odd ...
253
votes
40
answers
719k
views
Invalid postback or callback argument. Event validation is enabled using '<pages enableEventValidation="true"/>'
I am getting the following error when I post back a page from the client-side. I have JavaScript code that modifies an asp:ListBox on the client side.
How do we fix this?
Error details below:
...
140
votes
8
answers
93k
views
Change default app.config at runtime
I have the following problem:
We have an application that loads modules (add ons). These modules might need entries in the app.config (e.g. WCF configuration). Because the modules are loaded ...
44
votes
3
answers
30k
views
Single objects still limited to 2 GB in size in CLR 4.0?
As I understand it there's a 2 GB limit on single instances in .NET. I haven't paid a lot of attention to that since I have mainly worked on 32 bit OS so far. On 32 but it is more or less an ...
429
votes
7
answers
450k
views
Calculate MD5 checksum for a file
I'm using iTextSharp to read the text from a PDF file. However, there are times I cannot extract text, because the PDF file is only containing images. I download the same PDF files everyday, and I ...
319
votes
20
answers
395k
views
Get the correct week number of a given date
I have Googled a lot and found a lot of solutions, but none of them give me the correct week number for the 2012-12-31. Even the example on MSDN (link) fails.
2012-12-31 is Monday, therefore it ...
295
votes
5
answers
79k
views
Why does .NET use banker's rounding as default?
According to the documentation, the decimal.Round method uses a round-to-even algorithm which is not common for most applications. So I always end up writing a custom function to do the more natural ...