.net - How to achieve polymorphism in C# -


I'm new to C #. I was trying to check how polymorphism can be achieved in C # and I got confused that one method that is getting polymorphism I am using the code given below and the method of output A .

  class A {public void display () {Console.WriteLine ("A method of"); }} Class B: A {public zero performance () {Console.WriteLine ("Method of B"); }} Square polymorphism {public static zero principal (string [] args) (A = new B. (); One display (); Console.ReadKey (); }}   


But when I define the following pattern () method then the method of output B is called.

  class A {Public Virtual Zero Exposure () {Console.WriteLine ("Method of A"); }} Class B: A {public override zero performance () {Console.WriteLine ("Method of B"); }}   

So how am I achieving polymorphism and what is the difference between the two types and who should be better to overwrite. Any help would be appreciated.

Virtual methods provide polymorphism, in that subclass can override the behavior. If you do not use virtual methods then you have all the types that are defined in other types, but these inherited behaviors can not be replaced with new people.

In the first case, the code of A is called because you have a reference to A , and the non-virtual methods compile time > Is allowed to be referenced from the B variable as A variable because B is -a A . (Note that decreasing the object and calling the result will apply the method of B : ((b) a). Display (); .) < p> In the second case, the code of B is called because the method is virtual, and virtual methods in run time , actual type of object, in it The type of reference is stored.

Comments

Popular posts from this blog

php - PDO bindParam() fatal error -

logging - How can I log both the Request.InputStream and Response.OutputStream traffic in my ASP.NET MVC3 Application for specific Actions? -

java - Why my included JSP file won't get processed correctly? -