Click here to Skip to main content
16,018,249 members
Home / Discussions / C#
   

C#

 
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 
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 
Indeed, you didn't do that right. Not sure what you mean by "polymorphism it is", but I hope you're not clamining that the code you posted results in polymorphic behavior. It doesn't, as you can verify for yourself using this code:

public class A 
{
   public void M1() { Debug.WriteLine("A.M1()"); }
   virtual public void M2() { Debug.WriteLine("A.M2()"); } 
}

public class B : A
{
   new public void M1() { Debug.WriteLine("B.M1()"); }      // Hiding - not polymorphic
   virtual public void M2() { Debug.WriteLine("B.M2()"); }  // Virtual - polymorphic
}

class Program
{
   static public void Main(string[] args)
   {
      B b = new B();
      A b_declared_as_a = b; 
      
      b.M1();
      b.M2();
      b_declared_as_a.M1();
      b_declared_as_a.M2();
   }
}


Now, we have two references to a single object instance. The non-virtual method M1() is called based on the *declared* type of the object - thus A.M1() executes when we invoke b_declared_as_a.M1() - because that reference is declared as type A.

The virtual method M2() however always results in running B.M2(), because that's the run-time type of the object and the declared type is irrelevant for virtual method calls.

ToString() is indeed virtual, and that's great since it lets you do things like string formatting more easily. The default implementation returns the type name, so if you call ToString() on b above you get the name. If you override the method you get whatever you return in the overridden method. Obviously there's no code in object.ToString() that somehow obtains a reference to the instance of type B and calls ToString() on it. The reason it works is because the code generated for ToString() isn't a normal method call, but a lookup in the VMT for the type (B in this example) and then a dynamic invokation. This allows you to write code today that call methods you only create in the future, perhaps in another assembly.

Method hiding however is really not much more than having two types that both have a method with the same name. Poke tongue | ;-P
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 
AnswerRe: Penel in Status Bar Pin
DaveyM698-Apr-09 22:09
professionalDaveyM698-Apr-09 22:09 
GeneralRe: Penel in Status Bar Pin
Sajjad Leo8-Apr-09 23:19
Sajjad Leo8-Apr-09 23:19 

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.