Geeks With Blogs

News

Steve Clements .Net and then some....

Knowledge nugget...catchy heh?!?

I come across this some time ago and have since used it pretty heavily - its nothing new, but certainly a useful feature.

Its used when overloading constructors in your classes, so you have for example...

    public class Foo
    {
        public Foo(string arg1)
        {
            // Implementation for arg1
        }

        public Foo(string arg1, string arg2)
            : this(arg1)
        { 
            // Implementation for arg2
        }
    }

So the top constructor with one argument is called when using the second constructor, so you only need the implementation code for arg1 once.

You can also do this when inheriting from another class...

    public class Bar : Foo
    {
        public Bar(string arg1, string arg2, string arg3)
            : base(arg1, arg2)
        { 
            // Implementaion for arg3
        }
    }

This will call the constructor in the base called which will have the implementation to handle arg1 and arg2.

Enjoy!

 

Posted on Friday, January 25, 2008 12:43 PM .NET | Back to top


Comments on this post: Knowledge nugget: the 'this' constructor clause

No comments posted yet.
Your comment:
 (will show your gravatar)


Copyright © Steve Clements | Powered by: GeeksWithBlogs.net