Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Depending on the length of your method, this may be a situation for polymorphism:</p> <p>As noted in other answers, <code>string</code> and <code>StringBuilder</code> are not related to each other. Therefore, your only chance to use the same method on them is to create wrappers for these two types which are related.</p> <p>You could define a wrapper base class with your method like this:</p> <pre><code>public abstract class ScannableStringBase { public abstract int Length { get; } public abstract char this[int index] { get; } public int PositionOfTheSecondWord() { int pos = 0; int state = 0; char c; int length = this.Length; while (pos &lt;= length - 1) { c = this[pos]; if (c == ' ' || c == '\n') // space { if (state == 1) state = 2; // 2 means the space between the first and the second word has begun } else // a letter if (state == 0) state = 1; // 1 means the first word has begun if (state == 2) return pos; pos++; } return -1; } } </code></pre> <p>From that class, derive subclasses that handle the desired value types:</p> <pre><code>public class ScannableString : ScannableStringBase { public ScannableString(string value) { this.stringValue = value; } private readonly string stringValue; public override int Length { get { return stringValue.Length; } } public override char this[int index] { get { return stringValue[index]; } } } public class ScannableStringBuilder : ScannableStringBase { public ScannableString(stringBuilder value) { this.stringBuilder = value; } private readonly string stringBuilder; public override int Length { get { return stringBuilder.Length; } } public override char this[int index] { get { return stringBuilder[index]; } } } </code></pre> <p>Summarizing, you get:</p> <ul> <li>No code duplication, because <code>PositionOfTheSecondWord()</code> is only defined once, in the base class.</li> <li>Type-safety, because your <code>PositionOfTheSecondWord()</code> method cannot be called on anything other than <code>string</code> or <code>StringBuilder</code>.</li> <li>Extensibility, because if you ever discover you want to support a third type, you can simply derive yet another class from <code>ScannableStringBase</code>.</li> </ul> <p>One possible drawback may be that you have to distinguish between the type to analyze somewhere beforehand, so you can decide whether to instantiate a <code>ScannableString</code> or a <code>ScannableStringBuilder</code>.</p>
    singulars
    1. This table or related slice is empty.
    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.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      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