Linked Questions
12 questions linked to/from Try-catch speeding up my code?
250 votes
10 answers
42k views
Ternary operator is twice as slow as an if-else block?
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: ...
67 votes
7 answers
31k views
What's the point of "As" keyword in C#
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 ...
27 votes
7 answers
20k views
try catch performance
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 ...
13 votes
7 answers
35k views
Which is the best practice to use try - catch blocks with foreach loop? [closed]
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 ...
15 votes
4 answers
1k views
Why adding a try block makes the program faster?
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;...
9 votes
8 answers
7k views
Tell if string to double/float/int/short/byte is out of range
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 ...
10 votes
4 answers
2k views
Compare 2 byte arrays
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.). ...
18 votes
1 answer
1k views
How to avoid slowdown due to locked code?
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(...
4 votes
2 answers
330 views
Any easy way to cast List<int?> to List<int>
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 ...
0 votes
1 answer
2k views
VB .NET, check if dll exists
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 ...
-1 votes
6 answers
1k views
Does Try-Catch in C#/Java affect execution speed [closed]
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 ...
3 votes
2 answers
357 views
C# Compiler Optimization - Catch only containing throw
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 { ...