Click here to Skip to main content
15,892,005 members
Home / Discussions / C#
   

C#

 
GeneralRe: Assignability of output parameters in C# Pin
S. Senthil Kumar1-Jul-09 5:55
S. Senthil Kumar1-Jul-09 5:55 
GeneralRe: Assignability of output parameters in C# Pin
dojohansen1-Jul-09 6:05
dojohansen1-Jul-09 6:05 
GeneralRe: Assignability of output parameters in C# Pin
S. Senthil Kumar1-Jul-09 18:04
S. Senthil Kumar1-Jul-09 18:04 
GeneralRe: Assignability of output parameters in C# Pin
dojohansen1-Jul-09 22:25
dojohansen1-Jul-09 22:25 
GeneralRe: Assignability of output parameters in C# Pin
S. Senthil Kumar1-Jul-09 23:02
S. Senthil Kumar1-Jul-09 23:02 
GeneralDiscussion : Can we say Overloading as polymorphism Pin
Cracked-Down8-Apr-09 22:19
Cracked-Down8-Apr-09 22:19 
GeneralRe: Discussion : Can we say Overloading as polymorphism Pin
Nagy Vilmos8-Apr-09 22:49
professionalNagy Vilmos8-Apr-09 22:49 
GeneralRe: Discussion : Can we say Overloading as polymorphism Pin
dojohansen8-Apr-09 23:31
dojohansen8-Apr-09 23:31 
Well that's not actually an override. What you did is method hiding, and the compiler will warn you and say you should put the "new" keyword on the method declaration if the hiding was intended.

Polymorphism is incredibly powerful and can greatly simplify the design of many a thing. It is, in my view, the single most important concept of OOP, even more important than encapsulation (though that is certainly important too).

Virtual methods are said to be "late bound" and non-virtual methods "early bound". What this means is that when the compiler creates the CIL (formerly MSIL) code for your C# or other .net code, for non-virtual methods it will create code that invokes a specific method. Which method is called is determined by looking at the declared type of the reference to the object (or the type indicated for static methods, which obviously can never be virtual or behave polymorphically).

The above is NOT polymorphic. Given this code:

EggSample obj = new HenOther();
obj.Method("param");


We are allowed to do this of course, because HenOther extends EggSample. However, the compiler will create code to invoke EggSample.Method(), not HenOther.Method(), because "obj" (my reference) has the declared type EggSample, even though it's run-time type is in fact HenOther.

Virtual methods on the other hand are handled differently. The compiler will generate code to look up the method to call at run-time from what's called the type's VMT - Virtual Method Table. This means there's a small performance hit involved in invoking virtual methods, but that the method called is determined by the run-time (actual) type of the object rather than the declared type.

This is extremely useful in lots of situations, because it allows us to divide and conquer in ways we never could without it. Imagine you want to create a data transformation system that can read input files, process the input and compute statistics or whatever, and transform flat files to xml and lots of stuff. You'd have a gazillion ways to do this of course, but one approach could be to build a tree structure where each node in the tree represents some operation on the data and the structure of the tree creates a breakdown of the work. You could then use polymorphy to great effect. You could use either an interface (which is of course inherently polymorphic) or a simple base class, like this:

abstract public class ProcessingNode
{
   public List<processingnode> Children = new List<processingnode>();

   virtual public void PreProcess(Data d) {}
   virtual public void PostProcess(Data d) {}
  
   public void Execute(Data d)
   {
      PreProcess(d);
      foreach (ProcessingNode node in Children) node.Execute(d);
      PostProcess(d);
   }
}


Purists might argue that the Pre- and Post-process methods should be abstract, but I prefer providing a default implementation that does nothing. Then, if I derive a node that only does postprocessing (that is, processing that occurs AFTER the subtree has finished processing) I don't need to override the PreProcess method (I would have to if it was abstract in the base class.

Now you'd have a very extensible model where you can derive lots of different processing nodes that perform various operations. For example, you could create a Selection node that selects some subset of data, and an Aggregate node that computes an aggregate on the selected data. The Selection node would do something like d.Select(...); on preprocess and d.Unselect(); in the postprocess method. The subtree inserted into the selection node would process only the current selection rather than all the data.

As a final example, you could create an "UpperCase" processing node as easily as this:
public class Upper : ProcessingNode
{
   override public void PreProcess(Data d)
   {
      d.Selection.Text = d.Selection.Text.ToUpper();
   }
}

assuming, for brevity, that the Selection has a text representation you may modify. As you can see, the base class can make use of new processing nodes you add without any of this code knowing about the types. All it knows is that the object has a capability to preprocess and postprocess, and there is no need to write a bunch of code to call this method if the operation is "Upper" and another if it's something else. You could go ahead and add encryption nodes, image decoders/converters, translation nodes, indexing nodes, sniffers looking for product names or companies, and a million other things. And if you released your system as an assembly, as long as the base class is public, other people could add new types of processing nodes as well.

And that's the power of polymorphism!
GeneralRe: Discussion : Can we say Overloading as polymorphism Pin
Nagy Vilmos9-Apr-09 1:46
professionalNagy Vilmos9-Apr-09 1:46 
GeneralRe: Discussion : Can we say Overloading as polymorphism Pin
dojohansen9-Apr-09 2:46
dojohansen9-Apr-09 2:46 
GeneralRe: Discussion : Can we say Overloading as polymorphism Pin
Rob Philpott8-Apr-09 22:55
Rob Philpott8-Apr-09 22:55 
GeneralRe: Discussion : Can we say Overloading as polymorphism Pin
Cracked-Down8-Apr-09 23:20
Cracked-Down8-Apr-09 23:20 
GeneralRe: Discussion : Can we say Overloading as polymorphism Pin
dojohansen9-Apr-09 3:04
dojohansen9-Apr-09 3:04 
QuestionString manipulation with large strings [modified] Pin
Harvey Saayman8-Apr-09 22:17
Harvey Saayman8-Apr-09 22:17 
AnswerRe: String manipulation with large strings Pin
Rob Philpott8-Apr-09 22:52
Rob Philpott8-Apr-09 22:52 
GeneralRe: String manipulation with large strings Pin
Harvey Saayman8-Apr-09 23:13
Harvey Saayman8-Apr-09 23:13 
GeneralRe: String manipulation with large strings Pin
Rob Philpott8-Apr-09 23:24
Rob Philpott8-Apr-09 23:24 
GeneralRe: String manipulation with large strings Pin
Harvey Saayman8-Apr-09 23:38
Harvey Saayman8-Apr-09 23:38 
GeneralRe: String manipulation with large strings Pin
dojohansen8-Apr-09 23:58
dojohansen8-Apr-09 23:58 
GeneralRe: String manipulation with large strings Pin
Harvey Saayman9-Apr-09 0:30
Harvey Saayman9-Apr-09 0:30 
GeneralRe: String manipulation with large strings Pin
dojohansen9-Apr-09 1:14
dojohansen9-Apr-09 1:14 
GeneralRe: String manipulation with large strings Pin
dojohansen9-Apr-09 1:25
dojohansen9-Apr-09 1:25 
JokeRe: String manipulation with large strings Pin
dojohansen9-Apr-09 1:37
dojohansen9-Apr-09 1:37 
Questionhow to select the text from the dropdownlist Pin
mdazeemuddin8-Apr-09 22:03
mdazeemuddin8-Apr-09 22:03 
QuestionPenel in Status Bar [modified] Pin
Sajjad Leo8-Apr-09 21:16
Sajjad Leo8-Apr-09 21:16 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.