Note that there are some explanatory texts on larger screens.

plurals
  1. POOptimally and elegantly performing certain behaviors based on types in a hierarchy
    primarykey
    data
    text
    <p>I am trying to optimize a certain part of my code, which happens to be in a tight performance loop. Mainly I am trying to learn new things which I can apply in the future. My implementation is very lengthy, so I will give a general example of what I am trying to achieve. </p> <p>My question relates to this: <a href="https://stackoverflow.com/questions/686412/c-sharp-is-operator-performance">C# &#39;is&#39; operator performance</a>, and especially to the chosen answer.</p> <p>Say I have a class A. I also have a class B, which is derived from A. I have a list of type A (which contains a mix of A and B types). In a method where I process these items, I would like to achieve a certain behaviour based on the actual type of the object (not sure if this is the correct way of saying it. Please correct me wherever I say something wrong).</p> <pre><code>void Process(A item) { if (item is A) { DoBehavior((A)item); //I know the cast is redundant here, I'm just leaving //it here for my explanation. } else if (item is B) { DoBehavior((B)item); } } void DoBehaviour(A item) { //perform necessary behaviour for type A } void DoBehaviour(B item) { //perform necessary behaviour for type B } </code></pre> <p>This is the way I currently do it. Note that I iterate over a list of type A, which contains A's and B's. Also, if you feel I did not provide enough code to clarify the situation, I'll gladly expand.</p> <p>In the question I posted above: <a href="https://stackoverflow.com/questions/686412/c-sharp-is-operator-performance">C# &#39;is&#39; operator performance</a>, I have learnt that I can rather change the structure to use an "as" operator, and completely get rid of the explicit cast.</p> <pre><code>B bItem = item as B; if (bItem != null) { DoBehavior(bItem); } </code></pre> <p>This is all good, however, in actuality I do not just have an A and a B, I also have a C, a D, and so on, all deriving from base class A. This will lead to many of these if statements, and they would have to be nested for best performance:</p> <pre><code>B bItem = item as B; if (bItem != null) { DoBehavior(bItem); } else { C cItem = item as C; if (cItem != null) { DoBehavior(cItem); } else { //and so on. } } </code></pre> <p>Now this is ugly. I like writing neat, elegant code, yet I am exceptionally bad at doing it (which often leads me to wasting time trying to just make things look a little better).</p> <p>I hope this question is not to broad, but firstly I would like to know if there is a more optimal and clean solution at getting the type so that the relevant behavior is performed. If not, is there a cleaner way to use these 'as' operators than nesting it like this?</p> <p>I suppose one alternative would be to move the behavior into the base class A, and then overriding it for each derived class. However, in a higher thinking sense, the behavior in this particular case of mine is not a behavior of the class A (or it's children), rather, it is some external class acting/behaving on it (which will behave differently for each type). If there is no better way to do it, I will strongly consider implementing it as I have explained now - but I would like some expert opinions on this.</p> <p>I tried to keep this short, and may have left too much detail out. Let me know if this is the case.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload