Browsing the archives for the C# tag.


Free C# 3.0 EBook

Education, Software

You can download the C# 3.0 Pocket Reference, written by Joseph and Ben Albahari, for free courtesy of Red Gate Software.  Normally this book goes for $11.99 USD.

http://www.red-gate.com/products/ants_performance_profiler/be_ahead_of_the_game_ebook.htm?utm_source=simpletalk&utm_medium=email&utm_content=nlv_aheadofgame-ebook&utm_campaign=antsperformanceprofiler

They’re trying to get you interested in the ANTS Performance Profiler, so you might want to consider the free 14-day trial of that product.

No Comments

Byte-To-Hex Conversion in C# – Gotcha

Software

I was working on some password hashing stuff and ran into a gotcha I thought I’d pass along.

When you have code like the following:

Byte[] data = Encoding.ASCII.GetBytes(pass);
SHA1Managed sham = new SHA1Managed();
Byte[] hash = sham.ComputeHash(data);
string result = "";
foreach (Byte b in hash)
{
result += b.ToString("X"); // convert byte to hex
}
return result;

You’ll get some odd errors. The reason is that statement with the ToString(”X”) call in it. I found out the hard way that this only returns a single character when the value should have a leading zero in it: “D” instead of “0D”.

Use ToString(”X2″) to get the properly-formatted value.

(h/t Anthony Ogden for the sample source code)

Blogged with Flock

Tags: , , ,

1 Comment

C# Wishlist: Post-Constructor Method

Software

I would love it if C# had a predefined method that would be guaranteed to be called after every constructor is done firing. I have some checking code that I need to call, but depending on which constructors are called, it could need to be fired in a few different places. If I had a PostConstructor() method or something, I could centralize the checking code there.

It would also be cool if you could provide a method that would be called before or after every method call, like the SetUp() and TearDown() special methods in NUnit, but without all the reflection overhead at runtime.

3 Comments

C# 2.0 NULL Coalescing Operator

Software

OK, I’m a little behind the times, but I just found out there was a neat way to sidestep 90% of the null checks I do in my ternary operators:

output = (value == null) ? "NULL" : value;

becomes

output = (value ?? "NULL");

(h/t R. Aaron Zupancic)

No Comments