Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 2008 > C# 2008 aka C# 3.0
|
C# 2008 aka C# 3.0 Discuss the Visual C# 2008 (aka C# 3.0) language
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# 2008 aka C# 3.0 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old February 3rd, 2010, 11:34 AM
Authorized User
 
Join Date: Apr 2006
Posts: 17
Thanks: 2
Thanked 0 Times in 0 Posts
Default Extension methods vs inheritance

Greetings,

This may be a naive question, but with extension methods in C# 3.0 why would one want to use inheritance?
Sam
__________________
Sam Stamport
 
Old February 3rd, 2010, 11:42 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

With extension methods you only have access to the public properties and methods of the class you are extending.

With inheritance you have access to protected properties and methods, and you can also do things like override virtual methods etc.

Also, with inheritance all your code for a particular class will be in the same place. Whereas with extension methods your code will relating to a particular class will be in another, none obvious location.
__________________
/- Sam Judson : Wrox Technical Editor -/

Think before you post: What have you tried?
The Following User Says Thank You to samjudson For This Useful Post:
perstam (February 3rd, 2010)
 
Old February 3rd, 2010, 12:28 PM
Authorized User
 
Join Date: Apr 2006
Posts: 17
Thanks: 2
Thanked 0 Times in 0 Posts
Default

Please click one of the Quick Reply icons in the posts above to activate Quick Reply.
 
Old March 2nd, 2010, 06:35 PM
Friend of Wrox
 
Join Date: Dec 2008
Posts: 238
Thanks: 2
Thanked 20 Times in 19 Posts
Default

Here is one situation that inheritance can benefit you. First let’s take a look at the extension way:
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace SomeNamespace
{
   class Program
   {
       static void Main(string[] args)
       {
           var p = new Program();
           p.foo();
       }
 
       /*public void foo()
       {
           Console.WriteLine("1");
       }*/
   }
 
   static class Extended
   {
       public static void foo(this Program p)
       {
           Console.WriteLine("2");
       }
   }
}

Assume you don’t have control over class “Program” and may even don’t have access to source code of it. Run this program, it prints 2; uncomment what I commented out, and run it again, you got 1 – the behavior changed, there is no warning and no error, you might end up spend hours to debug before you find out what’s going on.

Now look at the inheritance way:
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace SomeNamespace
{
   class Program
   {
       static void Main(string[] args)
       {
           var p = new Extended();
           p.foo();
       }
 
       public void foo()
       {
           Console.WriteLine("1");
       }
   }
 
   class Extended : Program
   {
       public void foo()
       {
           Console.WriteLine("2");
       }
   }
}
Visual Studio gives you a warning saying “’SomeNamespace.Extended.foo()’ hides inherited member ‘SomeNamespace.Program.foo()’. Use the new keyword if hiding is intended”. This warning could save you hours of debugging time. (Add the new keyword as suggested will remove the warning.)

Last edited by PeterPeiGuo; March 2nd, 2010 at 06:37 PM..
The Following User Says Thank You to PeterPeiGuo For This Useful Post:
perstam (March 3rd, 2010)





Similar Threads
Thread Thread Starter Forum Replies Last Post
inheritance phuc_tran BOOK: Ivor Horton's Beginning Visual C++ 2008 ISBN: 978-0-470-22590-5 2 December 16th, 2008 08:14 PM
Inheritance magagulad Visual Basic 2005 Basics 1 May 12th, 2008 07:51 AM
Is it possible via Inheritance!!! mike_remember ASP.NET 2.0 Professional 0 November 26th, 2007 10:25 AM
METHODS magagulad Java GUI 3 May 15th, 2007 12:53 PM
c# inheritance bhohman C# 2 March 26th, 2004 01:47 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.