DCSIMG
.NET 3.5 - Doron's .NET Space

Browse by Tags

All Tags » .NET 3.5 (RSS)

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)
תגים:, , , ,

My First C# 3.0 Program

I've finally decided to download the new "Orcas" CTP of Visual Studio , which includes support for the all new C# 3.0. Up until now I only read about it, so I thought it was about time I tried it myself. And it's awesome. So, here's my "hello world" program. public void MyFirstProgram(Func < string , string > doSomethingWithString) { var result = doSomethingWithString( " dlroW olleH " ); Console.WriteLine(result); } public void Main() { MyFirstProgram(str =>...
Posted by dorony | 2 comment(s)
תגים:, ,