简体   繁体   中英

Object class with virtual members or not?

Let say I created a SomeObject class which has 100 properties or members like this,

Public Class SomeObject
{
  Public string Property1 {get; set;}
  Public string Property2 {get; set;}
  Public string Property3 {get; set;}
  Public string Property4 {get; set;}
  .... so on
  Public string Property100 {get; set;}
}

Now on some methods I am populating 2,3 properties and passing object and somewhere in code I am sending 50 out of 50.

Would it be any good if I change these properties into virtual properties in this case ?

EDIT:

Only thing I need to know is, If I pass above object like this to a method,

SomeObject so1 = new SomeObject();
so1.Property1 = "something";
so1.Property2 = "something";
so1.Property3 = "something";

MethodA(so1);

Now somewhere else in project I am doing this,

SomeObject so2 = new SomeObject();
so1.Property1 = "something";
so1.Property2 = "something";
so1.Property3 = "something";
....
so1.Property100 = "something";
MethodB(so2);

Is there any wrong with this approach :)

Edit 2:

Why i said I want to make properties virtual ? Because I was going through ASP.NET MVC training on Microsoft Virtual Training academy and somewhere in 6 hours videos guy said we will make few properties virtual as they didn't needed them everytime - "MOST Likely I understood it wrong and THOUGHT that virtual properties maybe something "ON DEMAND" objects"

So it's my fault I believe, I will go thorugh videos again at some point.

NOTE - Based on the comments above you are probably looking for an array , a generic (such as a list) , a dictionary or you may even want to implement a property bag . The rest of this answer explains when you would use the keyword virtual .


The primary purpose of declaring something as virtual is so you can write a basic implementation and then allow subclasses to provide specific implementations. Take the following as an example:

public class Car {
    public virtual string Make {
        get { return "Generic Make"; }
    }
}

public class Camaro : Car {
    public override string Make {
        get { return "Chevy"; }
    }
}

Now the specific implementation returns "Chevy" but if a developer forgets to override that it will just return "Generic Make".

There are (at least) two alternatives here:

  1. Use the new keyword to hide the base class implementation:

     public class Car { public string Make { get { return "Generic Make"; } } } public class Camaro : Car { public new string Make { get { return "Chevy"; } } } 

    The only issue with this is you have to cast as the subclass type in order to get the specific implementation. In other words, if you have a Car object you will always get "Generic Make" as the make for that car. You would have to cast as a Camaro in order to get the make of "Chevy" back!

  2. The other option is to use a protected property in the base class, then that gets set in the subclass. This looks like:

     public class Car { protected string _carMake = "Generic Make"; public string Make { get { return _carMake; } } } public class Camaro : Car { public Camaro() : base() { _carMake = "Chevy"; } } 

    Now using the Make property on the base class will return the appropriate value (generic vs chevy).

There are probably lots of good resources available online for learning about classes, inheritance and overriding behavior. Hopefully this at least gets you started!

Declare a method or property as virtual if you anticipate that inheritors of a class will override a member or instead, use the inherited implementation.


Any class that has 100 properties enumerated Property1 to Property100 is poorly designed. This characteristic has no bearing on whether those properties should be declared with the virtual keyword.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM