Hot answers tagged

91 votes
Accepted

Exporting doc types using queues and multithreading

Rather than reviewing your specific code -- which at first glance seems very, very buggy -- let me answer your question directly: What are the best practices with multithreading in C#? Don't. ...
user avatar
  • 14.6k
43 votes
Accepted

Extensible code to support different HR rules

I coded keeping SOLID principle in mind and here is my code. What did I miss? Let's see... SRP - Single Responsibility Principle A class should have only one reason to change. Violated. The name ...
user avatar
  • 43.8k
40 votes

Replace each whitespace in a string with "%20"

This one really fits well for a regular expression: public static string ReplaceAllSpaces(string str) { return Regex.Replace(str, @"\s+","%20"); } The ...
user avatar
  • 6,960
40 votes
Accepted

Benchmarking things in C#

Small issues before I get into the big one: Please make those setters private. The caller of this code has no business changing any of those values. Don't use ...
user avatar
  • 14.6k
37 votes

Benchmarking things in C#

The problem with an approach like that is that you you don't take into account that switching processors/cores is taking some time which will fake the results. This can be avoided by setting the ...
user avatar
  • 50k
35 votes

Regex validation for Email Address

I remember having read somewhere (possibly in another Code Review answer) that for an e-mail address, the simplest and most effective validation you can do is to make sure it contains an ...
user avatar
33 votes
Accepted

Method that returns description attribute of enum value

Type type = input.GetType(); MemberInfo[] memInfo = type.GetMember(input.ToString()); You should use more meaningful variable names like ...
user avatar
  • 43.8k
32 votes

Extension method replacing elements from an IEnumerable<T> collection

Why didn't Microsoft include this in the .NET framework? Probably because it can be realized in one line with Select: ...
user avatar
  • 8,498
25 votes
Accepted

Extension method replacing elements from an IEnumerable<T> collection

I cannot comment about the speed because I didn't benchmark it but there are two obvious improvements. add an IEqualityComparer<T> as a third parameter so ...
user avatar
  • 43.8k
23 votes
Accepted

BinaryTree<T> written in C#

public class BinaryTree<T> where T : IComparable, new() I suspect that you are unaware of the possibility of writing ...
user avatar
20 votes

Regex validation for Email Address

As @Simon pointed out, your regular expression might consider some valid addresses as invalid. Per this source which I've found somewhere on this Stack Overflow page, this would be RFC5322-compliant: ...
user avatar
20 votes
Accepted

Creating a thread-safe list using a Lock object

Your new approach is much better than the previous one. Instead of deriving from List and using new to hide the base class ...
user avatar
  • 20.1k
20 votes
Accepted

One-shot events in C#

Other answers are great, but this is really re-inventing the wheel. What you are looking for is the observer pattern. .NET has great in-built capabilities with observer through ...
user avatar
  • 3,753
18 votes
Accepted

Populating a ListView with Items

Yeah, sure, I could just get rid of the if's and else's and replace ((Item)list...) with Item item = list... and wrap it all in a single Try{}Catch{}, but I really do hate using Try{}Catch{} unless ...
user avatar
  • 50k
18 votes
Accepted

Matrix class in C#

I like that your braces and indentation is beautifully consistent. But, this is C#, and your Java is showing ;-) - Kudos for the mostly-consistent PascalCase type ...
user avatar
17 votes

Exporting doc types using queues and multithreading

You can use Parallel class: ...
user avatar
  • 13k
17 votes
Accepted

Change desktop background

These using directives are not needed and can be safely removed: ...
user avatar
17 votes

TCP Client Class implementation in C#

It looks like you should be implementing IDisposable here. See on MSDN. Then, in your Dispose(bool) method, you can call ...
user avatar
17 votes

Extensible code to support different HR rules

public class EmployeeLeave : ILeaveRequest ... var leaveRequest = new EmployeeLeaveDetail(); This is confusing. Going by name, ...
user avatar
  • 2,286
17 votes

Concurrent HashSet

General Stuff Would be nice to have inline documentation (///) on classes and public members, but everything is pretty simple and understandable. Some might ...
user avatar
  • 7,096
16 votes

Reusable Unit Of Work Interface / Factory

Leaky Abstractions public interface IUnitOfWork : IDisposable { void Commit(); } I like that there's only a Commit method ...
user avatar
15 votes

Fluent LinkedIn REST API client interface design

As @Jeff pointed out, this code is ...beautiful. Well done! I like the fluent interface a lot, but the code itself is at first glance... wow. Naming All identifiers follow conventional casing (<...
user avatar
15 votes
Accepted

Format a timespan with up to two fields

What you want to do, is use early-returns (FTW). Also, let the formatter do the string work for you. Right tool for the job. Consider your code, rewritten in Ideone...: ...
user avatar
  • 96.7k
15 votes
Accepted

Throwing exceptions when validation fails

You're throwing System.Exception. Don't do that. If you're going to have to throw an exception for a validation exception, throw a custom ...
user avatar
15 votes
Accepted

Checking if a text contains N consecutive repeating characters

Edit - there is a bug with this: see my other answer This is very easy with Regular Expressions: ...
user avatar
  • 16.6k
15 votes

Counting elements meeting a predicate in an enumerable without iterating the entire sequence

You can use the Linq Take to accomplish the same thing ...
user avatar
  • 4,098
14 votes

Benchmarking things in C#

You've got some pretty poor variable names there. For example, what's m2 supposed to mean? I don't like the fact, that result generates itself. I would rather have ...
user avatar
  • 13k
14 votes

Extensible code to support different HR rules

catch (Exception) { throw; } Well this won't buy you anything but a few more lines of code. Just remove the try..catch ...
user avatar
  • 50k
14 votes

Classifying image by file extension within a PDF creator

Naturally, you can use a dictionary to map extensions to file types: "jpg" -> ImageFileType.Jpeg "jpeg" -> ImageFileType.Jpeg "...
user avatar
  • 107k

Only top scored, non community-wiki answers of a minimum length are eligible