Wednesday, February 18, 2009

Five favourite new features in VS 2008

1. Extension methods

This feature I like most.
Thanks that you can extend standard object methods

Lets look below example:



As you noticed only difference is “this” word near parameter.
Here you are how to use this:




Please also note that if you add extension to object type – all variable types will get access to this extension!

My favourite example (coming from Scott Gu’s blog http://weblogs.asp.net/scottgu/archive/2007/03/13/new-orcas-language-feature-extension-methods.aspx )





2. Object Initializers

This is feature I waited long. It gives you possibility to assign in one line public properties in braces after constructor, like this:




3. Automatic Properties

If you don’t like all extra effort you need to do to write fields and properties code you can use below snippet.



CLR 3.x (during compilation) will generate fields and property code for you.
Note: you will not be able to access generated field (only way is only through setters and getters)

4. Local variable type inference

This feature allows you define local variables without defining their types (similar to variant variables in some cases), so you can do this:




Compiler generates appropriate int, string and “SomeType” variables in early-bound strongly-typed fashion.

Personally, I’d prefer explicit types definitions – this gives more clarity in code.
But in some cases (mainly for shorten long code) I’m finding it usefull:



5. Lambdas

Described soon...




Other nice features:
- partial methods

Labels:

Wednesday, February 4, 2009

Enable or Disable ScreenSaver in c#

Self-Explaining code snippet:

Labels:

Monday, February 2, 2009

Best way to run external program

Sometime you might look the way to run external program from your C# application.
If so, you should interest Process.Start() method inside using System.Diagnostics namespace.

Below I'm presenting code snippets doing common task for us:

Labels: ,

MS SQL 2005: Previous row value

SQL 2005 can number rows by command:
ROW_NUMBER() OVER (ORDER BY field_x,y…)

Another usefull 2205's command allowing "aliasing SQL" is:
WITH table_name AS ( SELECT … ) SELECT table_name ….

Command can be used for creating "virtual" (calcluated) columns


Below, snippet presenting how to archive previous row values in one SQL query.


Labels:

Incremental sum in TSQL

I've required incremental sums in one SQL query.
There is my trick base on unique field which is ID in my case:

Labels: ,