Skip to main content

Posts

Showing posts from October, 2010

Silverlight vs HTML 5 Debate – A Dilemma for CTOs, A Concern For .NET Developers, and May be a pointer towards Microsoft’s Internal Politics

Alright, a lot of arguments are already going on in the twitter sphere about Microsoft positioning HTML 5 on top of Silver light. A lot of opinions are floating around in the blogosphere too.  My 2 cents here.If you are not aware about the story, then I suggest you to read Mary Jo’s Initial post that says Microsoft: Our strategy with Silverlight has shifted based on her conversation with Bob Muglia - that kicked off the entire debate, and also read this post from Mashable that says “Microsoft Shifts from Silverlight to HTML”I’m not a Silverlight ‘only’ developer, though I'm a Microsoft MVP in Client Application Development.  But I loved Silverlight for its potential - I used to discuss with my friends how big Silverlight is, and how it may become a platform for developing “write once, run anywhere” apps for web, desktop, mobile & handheld devices, IPTVs etc – especially with cloud in the background.So, let us take a step back and have a look at what exactly is the strategy sh…

C# 5.0 Asynchrony – A Simple Intro and A Quick Look at async/await Concepts in the Async CTP

Recently, in Microsoft PDC 2010, Anders announced the new asynchronous features that’ll be introduced in the next version of C# (5.0?). This post is a quick and simple introduction towards using the async and await keywords. If you want to play with the async/await, you may need to download and install the Async CTP from the MSDN Site.. In short, “async” modifier marks method or lambda as asynchronous. “await” operator yields control until awaited task completes.Writing some actual CodeMight be helpful if you read Tasks 1-2-3 Why What How before this, if you are not familiar with Tasks.Let us try some simple ‘working’ code first, then we’ll discuss the concepts involved. Consider the following code, where we start a hotel and call our Cook/Chefs to start cooking various dishes. Each cook will take a random time to cook his dish. See the implementation below. static Random rnd = new Random(); static void Main(string[] args) { //Do some oth…

5 Interesting C# Articles – Fluent Interfaces, Anon Types, ‘dynamic’ keyword, Lambda Expressions, Type Inference etc

In this round up post, I’ll list down some of my “Back To Basics” C# Articles.1 – Creating Fluent Interfaces in C#This post demonstrates various possibilities of Fluent programming in C#. Have a look at how to train a bunch of dogs using Fluent interfacesRead it here2 – Revisiting Delegates, Anon Methods & LambdasThis post briefs Delegates, Anonymous Methods and Lambda expressions, and how these features got evolved with various versions of C#. Also, this post has few interesting uses of Lambda expressions as wellRead it here3 – A closer look at Anonymous Types in C#If you want to examine Anonymous types closely, have a look at this post. This post briefs the object initializer syntax, and projections via Anonymous types. Read it here4 – C# ‘dynamic’ Keyword – Under the hoodWant to know more about the Dynamic features of C#, and how the ‘dynamic’ keyword is getting compiled? See this post, where we have a look at the disassembled code to examine how ‘dynamic’ keyword actually work…

Poor Man's Singleton Methods via Dynamic Wrappers in C#

"The static world you see there is not real, you are programmed to think so. There is a true world out there, which is very very dynamic" - Do I sound like Morpheus in Matrix?
Justin recently wrote an emotional post about Ruby, and as a primary C# guy, I found it quite interesting. Though I’ve never paid some serious attention to Ruby earlier, I was exploring the dynamic features in C# for some time (See my posts on dynamic and also have a look at this ElasticObject implementation). So, I started digging into Ruby a bit after reading Justin’s post, I found a lot of interesting concepts, for example, the Singleton methods.Singleton MethodsSo, what are Singleton methods?The behavior of an instance is determined by its class, but there may be times we know that a particular instance should have special behavior. In most languages, we must go to the trouble of defining another class, which would then only be instantiated once. In ruby we can give any object its own methods.And he…

C# Enums - A bit of Extra Caution when working with Enums

Straight to a question for you. Consider the following code, where you accept a caller key and a token request from a caller, to issue a security key for further requests? Note that we also have a minimal exclusion check, where we prevent certain callers from getting the admin permission. Now, the question. What is wrong with the code below?public enum SecurityToken { Admin, Registered, Anon } public class SecurityGateway { public string GetSecurityKey(string callerKey,SecurityToken token) { //Prevent caller2 from getting the admin token if (callerKey.Equals("secretcallerkey2") && token == SecurityToken.Admin) return "Error: You can't request an admin token"; //Issue the token switch (token) { case SecurityToken.Anon: return "PermissionKeyForAnonymous"; …

C# Type Inference In Generic Methods + Leveraging that from Extension Methods – A Quick Note

This is a back to basics post. As you may already know, C# compiler don’t support type inference for generic classes, but it supports type inference for generic methods. To illustrate the point, let us consider a very simple example.class Program { //Our generic IsGreaterThan method public static bool IsGreaterThan<T>(T x, T y) where T : IComparable<T> { return (x.CompareTo(y) > 0); } static void Main(string[] args) { //You don't need to explicitly specify IsGreaterThan<int> var result = IsGreaterThan(20,10); Console.WriteLine(result); } }As you can see, we are not specifying the type explicitly, while calling our IsGreaterThan method - it is inferred automatically by the compiler. That is simple, isn’t it? Now, things become a a bit more interesting when you combine Generic method type inference with extension methods. Have a look at this code, w…

3 Gems in Mono for .NET Programmers – The hidden potential of Mono.CSharp, Mono.Cecil And Mono.TextTemplating

Mono is getting more mature. I should say, Mono guys are even outshining their Microsoft counterparts in some areas. For example, Mono’s C# compiler already supports Compiler as a Service. In this post, we’ll have a quick look at some interesting Mono libraries that you can use in your .NET projects, to take advantage of some of their functionalities, that is not present in the .NET stack.1 - Mono.CSharp – For Compiler as a Service and C# as a scripting language Anders in his C# Futures Talk mentioned about C# Compiler as a Service and demonstrated an REPL (Read Evaluation Print Loop) implementation. And few months back, I got pretty surprised when I found that Mono announced Compiler as a Service support. This will enable you to evaluate C# code on the fly, and more importantly, to use C# as a scripting language in your applications. Here is a step by step guide to this feature.Hosting Mono’s C# Compiler As a Service in .NET AppsDynamic Filtering and Querying in .NET ApplicationsUsin…

An Asynchronous Delegate Command for your WPF MVVM Apps - AsyncDelegateCommand

This post assumes you are familiar with basic MVVM concepts and RelayCommand/DelegateCommandIn a lot of scenarios, you need to perform operations in an asynchronous way in your View Model. And most of the time, BackgroundWorker class comes handy. So, here is a quick implementation of an Asynchronous version of DelegateCommand, that wraps the BackgroundWorker under the hoods. /// <summary> /// An async version for delegate command /// </summary> public class AsyncDelegateCommand : ICommand { BackgroundWorker _worker=new BackgroundWorker(); Func<bool> _canExecute; /// <summary> /// The constructor /// </summary> /// <param name="action">The action to be executed</param> /// <param name="canExecute">Will be used to determine if the action can be executed</param> /// <param name="completed">Will be invoked when th…

Introducing ‘Vingy’ Add-in for VS2010 – Search StackOverflow, CodeProject, DotNetKick etc easily from with in Visual Studio

For some time, I was thinking about developing a web search plug-in for Visual Studio, so that I can search the web (mainly sites like StackOverflow, CodeProject, MSDN etc) in a non intrusive way, with out leaving the IDE. Here we go, meet Vingy 1.0 – A simple, but effective add in for Visual Studio 2010 so that you can search the web in a non intrusive way, and can filter results based on sources.Getting used to VingyYou can bring up Vingy either by clicking View->Other Windows –> Vingy Search Window from the Visual Studio IDE, or just by high lighting some text in the document and then clicking Tools –> Search Selected Text (Ctrl + 1).Searching with Vingy is pretty straight forward. You can initiate a Search in two ways.By typing the text in the Vingy search box and pressing ‘Enter’ or by clicking the ‘Go’ buttonBy highlighting some text in the editor when you type in Visual Studio, and then pressing Ctrl + 1 So, after installing Vingy, high light some text in your editor …