Far Away Developer

Sebastien Lachance

Archive for April, 2008

Top 5 most influential peoples on my career

Posted by Sebastien Lachance on April 30, 2008

I was thinking about this for a while. If I have to choose five peoples who have influenced me the most concerning my career, who would that be?

1. My first supervisor when I was a wielder.

This guy once said to me that I would never do anything more than wielding in my life and if it wasn’t for my father, he would have fired me for nothing. I promised him that It will not happen. Four years later, I was the leader of a small team in the company. Take that…

2. Bernard Voyer.

A famous explorator that I met when I was in school. He believed in his dream and have conquered the Everest, South Pole, North Pole and a lot of hard to reach places. He his just a normal person who gave everything he had to make his dream true.

3. The ASP.NET guy.

I don’t remember his name , but it’s shortly after I finished school. He had just completed a Microsoft Certification in ASP.NET. This is where I first heard the term MCSD and I promised myself I would be one (even though I had no idea what it was at that time). 2 years later, February 7 2006 at almost 9:00 PM, I was one. I learned all by myself with the help of some books and the requirement list on MSDN. It just felt incredible. Even if I’m no longer in the world of certification, I’m still very proud of it.

4. Scott Hanselman.

First blog ever that I have put in my RSS aggregator. I’m following him since a long time and he inspired me a lot. It’s because of him that I am a blogger. He’s coming to the .NET User Group of Québec next week and I can’t wait to meet him.

5. Jean-Paul Boodhoo

Never met the man. I first heard of him in an episode on DnrTv about design patterns. It seem he has a lot passion… If you have never heard of him, head here. I wish I was as passionate as him. I am, but he his more :).

This was my top 5 of the peoples who had the most impact on me. But I want to talk about a 6th person. He probably don’t want me to talk about him so I will not give any name. Before meeting him, I was already into Agile development. But he taught me a lot about other aspect of the methodology. Everything that can’t be learn in a book to be more precise. But thing haven’t got very well at some point and after a lot of different events we lost touch. Anyway, I am very recognizing to have worked with him.

 

Posted in General | No Comments »

var keyword (or Implicitly Typed Local Variables)

Posted by Sebastien Lachance on April 17, 2008

If you are like me and installed the nightly build of Resharper 4, you might have seen the recommendation it made to use the keyword “var” almost everywhere. But why?

Here is my answer :

First of all, you must know that the keyword var is not like “Variant”. It is not late-bound or loosely typed. The compiler will be able to infer the type from the right side of the expression. In many case, it’s optional, but sometime you don’t have a choice. It’s the case with Anonymous Types :

var customer = new {ID = 1, Name = "Sebastien", CreditCardNumber = "You can dream!"};
 
It is also often seen with LINQ.
 
Should you use it everywhere you can? Well, it’s a matter of preferences. Mine is to use it only when necessary. If the code is harder to understand when using var, then declare the variable explicitly.
 
Technorati Tags: ,,

Posted in .NET | No Comments »

Exploring Anonymous Methods

Posted by Sebastien Lachance on April 16, 2008

My desire to explore Delegates and Anonymous Methods came from my next exploration, which will be the the Lambda feature. So, I will probably do an article on it in a near future.

So, what exactly is an anonymous method? It’s a C# 2.0 feature. Whenever you encounter the need to pass something as a delegate parameter, you can use an anonymous method.

Example :

This class has a method that expect a delegate that has no parameter and no return value.

public class Action
{
    public delegate void MyActionDelegate();

    public void Execute(MyActionDelegate myActionDelegate)
    {
        myActionDelegate.Invoke();
    }
}

Previously, you would have declared a method that has no parameter and no return value and passed it to the Execute method. Like this :

public static void DoSomething()
{
    Console.WriteLine("This is not an anonymous method.");
}

static void Main(string[] args)
{
    Action action = new Action();

    action.Execute(DoSomething);
}

An the output would be :

DelegateOutput

With an anonymous method, we reduce the code and by the same occasion, augment code readability.

static void Main(string[] args)
{
    Action action = new Action();
    action.Execute(delegate() { Console.WriteLine("This is inside an anonymous method."); });
}

 

AnonymousOutput

If the delegate has expected a parameter you could have done it like :

action.Execute(delegate(string s) { Console.WriteLine(s); });

And if the delegate have an return value, make sure you return one of the correct type :

action.Execute(delegate(string s) { return s; });

 

Accessing variables located outside the anonymous block :

A feature of an anonymous method is the ability to access variables located outside the anonymous scope (anonymous-method-block). Those variables called “outer variables”. It is better explained by an example :

string myString = "Outside the anonymous method.";
action.Execute(delegate(string s) { Console.WriteLine(myString); });

AnonymousScope

The exception to this rule are the ref and out parameters, which cannot be accessed by the anonymous method. You also can’t put unsafe code in the anonymous method.

Real life example

Maybe not (it’s always hard to come with great example), but it illustrate how you can use an anonymous method to find all string matching “String5″ inside a collection of string. Just imagine doing the same thing in your collection of orders or products.

List<string> myListOfString = new List<string>();

for (int x = 0; x < 10; x++)
{
    myListOfString.Add("String" + x);
}

List<String> returnedStrings = myListOfString.FindAll(delegate(string s) { return s == "String5"; });

 

Conclusion

Mastering Anonymous Methods is a great skill to have in you toolbox. But be careful, if you need to reuse the same code again and again, you better have to create a method and put the code in it. Don’t try to impress your friends too much. :)

 

Posted in .NET | No Comments »

Refreshing my memory : Delegates in C#

Posted by Sebastien Lachance on April 9, 2008

I haven’t really used delegates since I was programming with the .NET Framework 1.0. Since I have some spare time left, I decided to ‘”refresh my memory”. I have prepared a little cheat-sheet that you can use as a reminder for the delegates functionalities in the .NET Framework 3.0.

 

1. What is a delegate?

A delegate is a data structure that refers to one or multiples static methods and/or instance methods. You can use this when you have a dynamic list of methods to be called that you don’t know ahead of time.

2. How to create a delegate?

public class TestClass
{
    private delegate void myDelegate(string name);
}

The signature of the declaration is very important! The methods that will be contained in the delegate must perfectly match the declaration. In this case, the methods that will be allowed must be void and take a single string as a parameter.

When using the delegate keyword, you are actually creating a System.MulticastDelegate.

 

3. How do I instantiate a delegate?

public void TestMethod()
{
    ClassTest test = new ClassTest();

    //Instantiating a delegate with an instance method
    var del1 = new myDelegate(test.InstanceMethod);

    //Instantiating a delegate with a static method
    var del2 = new myDelegate(ClassTest.StaticMethod)
}

 

4. How do I call the method contained in the delegate?

del1.Invoke("This is a test.");

The Invoke method will actually have the same signature as your delegate signature. So you benefit of a strongly-typed way to use the delegate feature. There is also a BeginInvoke and an EndInvoke method to program asynchronously.

 

5. But you said that you can have more than one method in the delegate?

Actually yes. There is several ways to add more methods, but the easiest way is :

del1 += ClassTest.StaticMethod;

When calling Invoke (or BeginInvoke) each one will be executed in the order that they were added to the delegate. If an exception occurs at any point, the execution is canceled and the remaining methods will not be called. And you will get an exception of course.

 

5. Anything else?

Yes, a delegate is immutable. Each time you add a method entry point, a new delegate is created for you.

Posted in .NET | No Comments »

A new job

Posted by Sebastien Lachance on April 5, 2008

Last week, I have left my job at bxsystems. I had an opportunity that I could not refuse. I have worked for 3 years and 8 months with the most talented peoples I ever encountered. They are great professionals and I am sure they will continue to be successful in their careers. This is where all started for me, where I started to get passionate about programming. I had a lot of freedom and a lot of learning opportunities.

I am very grateful for everything. For a beginner, it could not be a better place. Thank you!

Posted in General | 2 Comments »