DCSIMG
C# - Doron's .NET Space

Browse by Tags

All Tags » C# (RSS)

ThreadAbortException is Special

I guess you could say that every exception is special and important in its own way, but ThreadAbortException is really special. In what way, you ask? Well, let me tell you a story. I wanted to implement a hard timeout for our system. That is, if a query to our service takes too long, force kill it, not matter what it is doing at the moment. You may claim that this is not a best practice, but it was the only way to ensure that even if a horrible bug that is causing an infinite loop in some very rare...
Posted by dorony | 2 comment(s)
תגים:

How to Save a Unicode Text File That Excel Can Read

The other day I had to create a .CSV file with some funky Unicode characters. Not only that, but Excel had to be able to open and edit it. When using .NET’s StreamWriter default constructor , it uses a default encoding of UTF-8 without BOM ( byte order mark ), which Excel can’t read. Well, actually, it can read it, but it doesn’t realize that this is a unicode file and special characters (such as this lovely one - Ž ) have a tendency to look like someone just puked a letter on the screen. The solution...
Posted by dorony | with no comments
תגים:

Sets of Mutable Objects is a Bad Idea

I guess this is something that is obvious to many, but it really had me stumped yesterday. I was looking at code that was joining two HashSets, that both had the same item. Not the same reference, but they had the same hash code, and Equals between the items returned true. And yet, Set1.UnionWith(Set2) changed Set1 to have two items. That seemed insane. Isn’t it in the contract of HashSet to not contain the same item twice? How could it be? It took a while, but I figured out the issue. Let’s try...
Posted by dorony | 1 comment(s)
תגים:

Improving Upon LINQ’s Distinct

Suppose you have a collection of objects, and you want only the distinct values. You could easily use Linq for this, like that: var items = new [] { "BMW" , "Fiat" , "Ferrari" , "Fiat" }; var distinctItems = items.Distinct(); This uses the default comparer for the objects in order to see if they’re equal. There’s also another overload, which lets you specify your own comparer. This is its signature: public static IEnumerable<TSource> Distinct<TSource>...
Posted by dorony | 2 comment(s)
תגים:,

Pretty Quick Sort With C# 3.0

Jafar Husain writes about the prettiness of F#'s type inference, but what I really liked in his post was his C# 3.0 implementation for Quick Sort: 1 public static IEnumerable < T > QuickSort < T > ( this IEnumerable < T > list) 2 where T : IComparable 3 { 4 if ( ! list.Any()) 5 { 6 return Enumerable.Empty < T > (); 7 } 8 var pivot = list.First(); 9 var smaller = list.Where(item => item.CompareTo(pivot) <= 0 ).QuickSort(); 10 var larger = list.Where(item => item...
Posted by dorony | 6 comment(s)
תגים:

Interesting Behavior with the New Modifier and Interfaces

Consider the following code. What will be the result of calling Test.TestInterface()? public interface IInterface { void DoSomething (); } public class Father : IInterface { public void DoSomething () { Console . WriteLine ( "Father Called" ); } } public class Son : Father { public new void DoSomething () { Console . WriteLine ( "Son Called" ); } } public class Test { public void TestInterface () { IInterface tester = new Son (); tester . DoSomething (); } } Note that Father is...
Posted by dorony | 1 comment(s)
תגים:

Don't Seal That Door

I've been going over this week's updates, and ran across this post in CodeBetter , that talks about why classes should be sealed by default, only to be unsealed by the 'unsealed' keyword. I couldn't disagree more. I think that you need a hell of a good reason to seal a class, so claiming that most classes should be sealed is a bit seems outrageous to me. I believe it's absurd to prevent extensibility for your classes, and if classes are sealed by default, the result will be...
Posted by dorony | 2 comment(s)
תגים:,

C# 3.0 New Feature for the Week #6: Partial Methods

This is a feature I was completely unaware of until I started digging into Linq-to-SQL recently. Upon using the Linq class designer on the Northwind database, I looked into the generated code, and saw something like this: public partial class Category { partial void OnCreated(); public Category() { // Do stuff OnCreated(); } } This is generated code for the Category entity, and you can see it includes a method definition for OnCreated, which does not have any body and is marked with the partial keyword...
Posted by dorony | 3 comment(s)
תגים:,

String-less ArgumentNullException

The post I wrote yesterday about Expression Trees, and Jafar Husain's work , have inspired me to find some more cool usages for this feature. Consider this code: public class PersonRepository { public void Add(Person person, Context context) { if (person == null ) throw new ArgumentNullException( " person " ); if (context == null ) throw new ArgumentNullException( " context " ); // ...Do the actual stuff } } The main annoying thing about this code is having to write the parameter...
Posted by dorony | 3 comment(s)
תגים:,

C# 3.0 New Feature for the Week #5: Lambdas and Expression Trees

This week I'll talk about what seems to me like the coolest features of C# 3.0: lambda expressions and expression trees. Lambda Expressions We'll start with the part that is easier to understand. Remember anonymous methods from C# 2.0? Useful little bastards they are, only sometimes not so pleasant on the eyes. public void SortListIgnoreCase() { List < string > list = new List < string > { " abc " , " ADE " , " dol " }; list.Sort( delegate ( string...
Posted by dorony | 1 comment(s)
תגים:,

C# 3.0 New Feature for the Week #4: Extension Methods (and a PowerCollections Bonus!)

Consider the following. You need to sort an IEnumerable<T>. By default, Sort is only available for T[] (arrays) and List<T> (generic list). So you write something like this (probably not the most efficient implementation, this is just an example): namespace Utils { public static class CollectionUtils { public static IEnumerable < T > Sort < T > (IEnumerable < T > collection) { List < T > toSort = new List < T > (collection); toSort.Sort(); return toSort;...

C# 3.0 New Feature of the Week #3: Anonymous Types

This week's C# 3.0 New Features article will discuss a neat feature called anonymous types. You'll use it like this: var me = new {Name = " Doron " , Age = 24 }; The result is an anonymous type created behind the scenes, with two properties: Name and Age. We assign it to a local variable with the "var" keyword, since we can't really mention the type (because of all the anonymity). To know more about the var keyword, read my first article about C# 3.0 . Since it is...
Posted by dorony | 5 comment(s)
תגים:,

C# 3.0 New Feature for the Week #2: Object and Collection Initializers

Last week I started my series of posts about new C# 3.0 features, and my second post will be about object and collection initializers. I will also include a bonus - an easy way of having a collection initializer in C# 2.0. Object Initializers Say we have declared the following class (with the automatic properties syntax we learned about last week ): public class Person { public string FirstName { get ; set ;} public string LastName { get ; set ; } } We can now initiate a Person in the following way...
Posted by dorony | with no comments
תגים:,

C# 3.0 New Feature for the Week #1: Automatic Properties

I've decided that I really need to get in the program and start learning C# 3.0 and Linq seriously. I've been planning to this for a long time, and even started playing with it a little, but never dug in really deep. So my new "C# 3.0 New Feature for the Week" series of posts is about getting myself to commit to studying and blogging about it. Every week I will present one new feature and talk about what is it good for and how it is implemented. So here we go. The first feature...
Posted by dorony | 2 comment(s)
תגים:,

WPF Binding, INotifyPropertyChanged and Linq

This is the first of a couple of tips I would like to share reguarding WPF. WPF binding is extremely powerful, but you are bound to run into a few issues, especially if, like myself, you have no WinForms experience. As I was writing my small LiveSpaceToBlogML GUI , I used binding in order to populate an object called ConversionOptions, which pretty much held all the data on the form. The form look something like this (a pretty simplified version, in order to focus on what matters): < Window x...
Posted by dorony | 9 comment(s)
תגים:, , , ,
More Posts Next page »