locked
What is the use of Virtual in Classes RRS feed

  • Question

  • User-1499457942 posted

    Hi

      I have the below class. What is the purpose of below lines & how it helps . 

    public virtual BookCategory BookCategory { get; set; }
    public virtual Author Author { get; set; }

    public class BookMaster
        {
            
            public string ReferenceId { get; set; }
            public int AuthorId { get; set; }
            public int BookCategoryId { get; set; }
    
            public virtual BookCategory BookCategory { get; set; }
            public virtual Author Author { get; set; }
        }

    Thanks

    Tuesday, January 31, 2017 3:57 PM

All replies

  • User197322208 posted

    Virtual: https://msdn.microsoft.com/en-us/library/9fkccyh4.aspx

    For your question , it may become relevant also Lazy Loading from https://msdn.microsoft.com/en-us/library/jj574232(v=vs.113).aspx 

    "When using POCO entity types, lazy loading is achieved by creating instances of derived proxy types and then overriding virtual properties to add the loading hook."

    Tuesday, January 31, 2017 4:35 PM
  • User-707554951 posted

    Hi JagjitSingh,

    The virtual keyword is used to modify a method, property, indexer, or event declaration and allow for it to be overridden in a derived class. For example, this method can be overridden by any class that inherits it:

    http://stackoverflow.com/questions/3838021/c-sharp-virtual-keyword

    Best Regards

    Cathy

    Thursday, February 2, 2017 3:14 AM
  • User347430248 posted

    Hi JagjitSingh,

    In object-oriented programming, a virtual property is a property whose behavior can be overridden within an inheriting class. This concept is an important part of the polymorphism portion of object-oriented programming (OOP).

    example:

    public class BaseClass
    {
    
        public int Id { get; set; }
        public virtual string Name { get; set; }
    
    }
    
    public class DerivedClass : BaseClass
    {
        public override string Name
        {
            get
            {
                return base.Name;
            }
    
            set
            {
                base.Name = "demo";
            }
        }
    }

    The virtual keyword is used to modify a method, property, indexer or event declaration, and allow it to be overridden in a derived class. For example, this method can be overridden by any class that inherits it: Use the new modifier to explicitly hide a member inherited from a base class. To hide an inherited member, declare it in the derived class using the same name, and modify it with the new modifier.

    This is all to do with polymorphism. When a virtual method is called on a reference, the actual type of the object that the reference refers to is used to decide which method implementation to use. When a method of a base class is overridden in a derived class, the version in the derived class is used, even if the calling code didn't "know" that the object was an instance of the derived class.

    Regards

    Deepak

    Saturday, February 11, 2017 1:25 AM
  • User880531280 posted

    HI Jagjit,

    This means those 2 properties BookCatergoy and Author can be overridden when you inherit this BooksMaster class in some other classes in future.

    Tuesday, February 28, 2017 5:52 AM
  • User768703680 posted

    Please, refer the code below

     

    class A

        {

           

    public virtual string A1 { get; set; }

           

    public virtual string A2 { get; set; }

           

    public virtual int A3 { get; set; }

        }

       

    class B : A

        {

           

    public override string A1

    {

               

    get

    {

                   

    return base.A1;

                }

               

    set

                {

                   

    base.A1 = "Inside A and A1";

                }

            }

           

    public override string A2

            {

               

    get

                {

                   

    return base.A2;

                }

               

    set

                {

                   

    base.A2 = "Inside A and A2";

                }

            }

           

    public override int A3

            {

               

    get

                {

                   

    return base.A3;

                }

               

    set

                {

                   

    base.A3 = 1;

                }

            }

        }

       

    class C : A

        {

           

    public override string A1

            {

               

    get

                {

                   

    return base.A1;

                }

               

    set

                {

                   

    base.A1 = "Inside C and A1";

                }

            }

           

    public override string A2

            {

               

    get

                {

                   

    return base.A2;

                }

               

    set

                {

                   

    base.A2 = "Inside C and A2";

                }

            }

           

    public override int A3

            {

               

    get

                {

                   

    return base.A3;

                }

               

    set

                {

                   

    base.A3 = 2;

                }

            }

        }

    Thursday, March 9, 2017 5:49 AM