0

Let's say I have a POCO class with only 2 properties; one is virtual, the other is not.

public class MyPocoClass
{
    public string SomeProperty { get; set }
    public virtual string SomePropertyTwo { get; set }
}

Does the virtual property (SomePropertyTwo) have any side effect? Does it reduce performance etc?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • No. I am not talking about collections. And it tells nothing about cons. It only says "not required". My question is - if I mistakenly mark some property (not collection) as virtual one, can i cause any side effects? – imsuktoday Dec 20 '22 at 19:59
  • Sorry, closed to quickly (as duplicate of [this question](https://stackoverflow.com/questions/41881169/navigation-property-should-be-virtual-not-required-in-ef-core?noredirect=1&lq=1)). But you better remove any hint of functioning in the context of EF if your question is purely about the effect of the `virtual` modifier in the C# language. Also, I'd be surprised if no other duplicates exist. – Gert Arnold Dec 20 '22 at 20:59
  • So if I understand correctly, "virtual" keyword has no side effects, but it's redundant keyword (in this context) and should be removed – imsuktoday Dec 20 '22 at 21:39
  • I didn't conclude anything, did I? "Side-effect" is a vague word here. It won't make code incorrect or slow, but it allows you to override the property which may certainly cause side-effects. I don't know if you want to override it. If not, don't make it virtual. At any rate, it has no bearing on lazy loading b/c scalar properties can't be lazy loading. – Gert Arnold Dec 20 '22 at 21:45

2 Answers2

0

as far as i know virtual property only used when you want to use lazy loading

Amin Memar
  • 13
  • 3
0

In this context, "virtual" keyword is only redundant keyword and should be removed.