"in" operator

  • Thread starter Thread starter jodleren
  • Start date Start date
J

jodleren

Is there such a tning as an "in" operator?

if somechar in ['a', 'b', 'c'] then ....
if(in_array(somechar, ['a', 'b', 'c']) ) ....
 
Is there such a tning as an "in" operator?

if somechar in ['a', 'b', 'c'] then ....
if(in_array(somechar, ['a', 'b', 'c']) ) ....

This looks a bit like a combination of PHP and Python.

The data structure can be mapped both to array and
a collection in C#/.NET.

char[] and List<char>

Which one is best depends on the context. But if
in doubt then choose List<char>.

Demo of how to do the required operation on both
char[] and List<char>.

using System;
using System.Collections.Generic;
using System.Linq;

namespace E
{
public class Program
{
public static void Main(string[] args)
{
char[] ar = { 'a', 'b', 'c' };
Console.WriteLine(Array.Exists(ar, elm => elm=='b'));
Console.WriteLine(Array.Exists(ar, elm => elm=='d'));
Console.WriteLine(ar.Contains('b'));
Console.WriteLine(ar.Contains('d'));
List<char> lst = new List<char>(ar);
Console.WriteLine(lst.Contains('b'));
Console.WriteLine(lst.Contains('d'));
Console.ReadKey();
}
}
}

Arne
 
Back
Top