Question

Suppose we had a generated class with a lot of redundant accessors. They are just accessors, they are not fields. They are not called anywhere. They just sit there being redundant and ugly.

For example:

public class ContrivedExample
{
    public int ThisOneIsUsed { get; set; }

    public int ThisOneIsNeverCalled0 { get { /* Large amounts of logic go here, but is never called. */ } }
    public int ThisOneIsNeverCalled1 { get { /* Large amounts of logic go here, but is never called. */ } }
    public int ThisOneIsNeverCalled2 { get { /* Large amounts of logic go here, but is never called. */ } }
    //...
    public int ThisOneIsNeverCalled99 { get { /* Large amounts of logic go here, but is never called.*/ } }
}

ContrivedExample c = new ContrivedExample() { ThisOneIsUsed = 5; }

The only overhead I can think of is that it would make the .DLL larger. I expect that there would be zero runtime penalties.

Does this cause any other overhead? Even a tiny overhead of any kind?

Was it helpful?

Solution

It is unlikely to have any measurable run-time overhead. In any case since it is performance question - measure your usage and make decisions for your case if in doubt.

Unreferenced methods do not get compiled by JIT nor cause direct run-time overhead.

Metadata for the class will be bigger (along with size of assembly as you've mentioned).

You may get indirect impact if the class is used in some code that involves a lot of reflection, also if code repeatedly reflects over the same class it is likely wrong by itself.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top