Saturday, July 5, 2008

Foundation of Programming free e-book

Last week while watching Channel 9 video, I came across this good free e-book (Foundation of Programming) from Karl Seguin.

Download it and read it, really good book. I have not covered even 50 % of it, but I love the things that he has explained like ... mock objects,TDD, Dependency injection etc.

Initially its bit tough but very useful in long run.

Grab it!!!!

http://codebetter.com/files/folders/codebetter_downloads/entry179694.aspx

Thanks,
Yash

Monday, June 30, 2008

Lambda Expression

Lambda Expression -- introduced by Microsoft in C# 3.0 is a very fascinating topic. It has been extensively used in LINQ(Language Intergrated Query).

So, what is Lambda Expression?

Lambda Expression is basically nothing but an anonymous function.

In .Net 1.1 we used to create delegates using delegate type. Delegate is a type safe datatype which contains a reference to a function.
Eg:


.Net 2.0 introduced the concept of Anonymous delegates. what is means is you can write the whole function inline. This makes the code more readable and you don't have to define instance or static methods.
Eg:


As you can see the code is readable and concise.

In C# 3.0 the concept of anonymous delegates has been taken to the next level by adding lambda expression.

Lambda expression uses an operator ( =>) which should be read as "goes to".
Eg:


As you can see the code is very elegant and the IL(intermediate language) generated by all the 3 ways of using delegates is the same. So the point to understand here is that the complier will generate exactly the same IL for all the 3 implementations so we don't need a new runtime for executing this code. .Net 2.0 runtime will be able to execute this code for us.

Lambda's can be expression or statements. Lambda statement are nothing but a group of c# statements clubbed together.

IL generate by instance methods:

IL generated by anonymous delegates (.Net 2.0)


IL generated by Lambda Expression


Click here to download the SourceCode:


I hope this will help you understand what Lambda expressions are.

Thanks,
Yash

Sunday, June 29, 2008

How to Read custom entries from the Web.Config/App.Config file

Today I am going to blog about "How to read custom entries from the Web.Config/App.Config file."

Working as a .Net Developer, there will be many occasions when we want to store some data (Maybe Key/Value Pair) in the configuration file but we dont want to add to the AppSettings. In such situations, I have always found is very easy to store my Config entires in the Custom Config sections in Web.Config files.

I want to read the custom section shown below.




The idea behind reading or writing to the config files is that -- "You should tell the framework how you want your data to be Serialized/Deserialized.

In .Net 2.0 made it very easy to Read/Write config data into the configuration file by providing the functionality in System.Configuration.dll.



Add the entry shown above, in the ConfigSection so that framework can create an instance of the class at runtime using reflection.

I have attached the SourceCode for this application.

Source Code Loc:

http://dotnetdevblog.googlepages.com/ReadCustomConfigSections.zip

If you like/dislike something, feel free to leave comments.

Thanks,
Yash