雑記 - otherwise

最近はDQ10しかやっていないダメ技術者がちまちまと綴る雑記帳

SQL の In 句の様な事を VB/C# で

つまり、まとめるとこう云う事ですね?

VB

Imports System.Runtime.CompilerServices

Module StringExtensions
  <Extension()> _
  Public Function [In](ByVal str As String, ParamArray ByVal params As String()) As Boolean
    Return params.Contains(str)
  End Function
End Module

Module Foo
  Sub Main()
    Dim str1 As String = "peach"

    If str1.In("banana", "melon", "peach") Then
      Console.WriteLine("見つかったヨ!")
    Else
      Console.WriteLine("入ってないヨ!")
    End If

  End Sub
End Module

C#

using System;
using System.Linq;

public static class StringExtensions {
  public static bool In(this string str, params string[] param) {
    return param.Contains(str);
  }
}

class Foo {
  static void Main() {
    var str = "peach";
    if (str.In("banana", "melon", "peach")) {
      Console.WriteLine("見つかったヨ!");
    } else {
      Console.WriteLine("入ってないヨ!");
    }
  }
}

……意外と使えるかもしれない。