Linked Questions

250 votes
10 answers
42k views

I read everywhere that ternary operator is supposed to be faster than, or at least the same as, its equivalent if-else block. However, I did the following test and found out it's not the case: ...
WSBT's user avatar
  • 36.8k
67 votes
7 answers
31k views

From the docs: The as operator is like a cast except that it yields null on conversion failure instead of raising an exception. More formally, an expression of the form: expression as type ...
patrick's user avatar
  • 17.1k
27 votes
7 answers
20k views

This article on MSDN states that you can use as many try catch blocks as you want and not incur any performance cost as long no actual exception is thrown. Since I always believed that a try-catch ...
Mez's user avatar
  • 2,867
13 votes
7 answers
35k views

what is the best practice to use try{} catch{} blocks regarding performance ? foreach (var one in all) { try { //do something } catch { } } Or try { foreach (var one in ...
Rafik Bari's user avatar
  • 5,037
15 votes
4 answers
1k views

I am using the following code to test how slow a try block is. To my surprise, the try block makes it faster. Why? public class Test { int value; public int getValue() { return value;...
Wei Liu's user avatar
  • 575
9 votes
8 answers
7k views

I have the following: string outOfRange = "2147483648"; // +1 over int.MaxValue Obviously if you have anything other than a number this will fail: var defaultValue = 0; int.TryParse(outOfRange, out ...
gleng's user avatar
  • 6,354
10 votes
4 answers
2k views

I have 2 int arrays. int[] data1 # int[] data2 # I want to create a 3rd int[] data3 which is the differences between the 2 other arrays. Let us take the 1st value in data1. The value is 15 (e.g.). ...
Andrew Simpson's user avatar
18 votes
1 answer
1k views

I am wondering how a piece of locked code can slow down my code even though the code is never executed. Here is an example below: public void Test_PerformanceUnit() { Stopwatch sw = new Stopwatch(...
pieroxy's user avatar
  • 871
4 votes
2 answers
330 views

At the moment I use List<int> ints = tuple.Item2.Select(s => s.Value).ToList() but this looks inefficient when tuple.Item2 has 1000's of items. Any better way to achieve this? except using a ...
Jack's user avatar
  • 7,607
0 votes
1 answer
2k views

In my VB .NET project, I have several functions that use unmanaged code from within winusb.dll. I tested the code on some machine which haven't drivers installed at all (winusb) and of course errors ...
user1797147's user avatar
-1 votes
6 answers
1k views

How does using Try-Catch blocks in C#/Java programs affect their execution speed? I'm in the development process (using Visual C#) of writing a database app and have been putting a try-catch block in ...
user3155955's user avatar
3 votes
2 answers
357 views

I would expect the following two implementations of MyMethod to behave exactly the same. Do they? If not, this could already be my wrong assumption: First: public int MyMethod(int x) { try { ...
Dennis's user avatar
  • 14.5k