in operator in C#
Solution 1
Try this:
if(new [] {"a", "b", "c"}.Contains(aString))
Console.Out.WriteLine("aString is 'a', 'b' or 'c'");
This uses the Contains
method to search the array for aString
.
Solution 2
Thanks to my Delphi days, I'm also used to its in keyword. For the generic case in C#, I'm using:
public static class Helper
{
public static bool In<T>(this T t, params T[] args)
{
return args.Contains(t);
}
}
}
which can be utilized as follows:
var color = Color.Aqua;
var b = color.In(Color.Black, Color.Blue);
b = "hello".In("hello", "world");
Solution 3
You could use an extension method if you want a slightly more fluent way of working so for this example of a customer and customer status:
public enum CustomerStatus
{
Active,
Inactive,
Deleted
}
public class Customer
{
public CustomerStatus Status { get; set; }
}
Use the following extension method:
public static class EnumExtensions
{
public static bool In(this Enum value, params Enum[] values)
{
return values.Contains(value);
}
}
to allow you to write code like this:
private void DoSomething()
{
var customer = new Customer
{
Status = CustomerStatus.Active
};
if (customer.Status.In(CustomerStatus.Active, CustomerStatus.Inactive))
{
// Do something.
}
}
Solution 4
No. You can come close:
if (new [] { "a", "b", "c" }.Contains(aString))
Console.Out.WriteLine("aString is 'a', 'b' or 'c'");
Solution 5
The equivelent in C# would be Contains()
(assuming you have a list or array of data)
var myStuff = new List<string> { "a", "b", "c" };
var aString = "a";
if(myStuff.Contains(aString)) {
//Do Stuff
}
As for the in
keyword, it has a different use:
var myStuff = new List<string> { "a", "b", "c" };
var aString = "a";
foreach(string str in myStuff) {
//Iteration 0 = a, 1 = b, 2 = c
}
Comments
-
brgerner almost 2 years
I think there was an in operator in Commodore 128 Basic.
Is there an in operator in c# too?I mean is there an operator of kind
if(aString in ["a", "b", "c"]) Console.Out.WriteLine("aString is 'a', 'b' or 'c'");
Edit1: Currently I need it to decide if an enum value is in a range of some enum values.
Edit2: Thank you all for the Contains() solutions. I will use it in the future. But currently I have a need for enum values. Can I replace the following statement with Contains() or other methods?
public enum MyEnum { A, B, C } class MyEnumHelper { void IsSpecialSet(MyEnum e) { return e in [MyEnum.A, MyEnum.C] } }
Edit3: Sorry it was not Basic. I just googled a while and found Turbo Pascal as a candidate where I could saw it. See http://en.wikipedia.org/wiki/Pascal_%28programming_language%29#Set_types
Edit4: Best answers up to now (end of 15 Feb 2012):
- For lists and arrays: accepted answer and all other answers with Contains() solutions
- For Enums: TheKaneda's answer with good list of pros/cons for different extension methods
-
brgerner over 12 yearsIs there a related solution for enums?
-
Brian Driscoll over 12 years@DavidStratton Actually I don't think that's the case.
-
Lzh over 12 yearsBut his question is written this way to demonstrate the way the inquirer wants to use the language, not the actual business purposes.
-
JimmiTh over 12 years@brgerner: For a bit of syntactic sugar (which has pros and cons), see stackoverflow.com/a/5320727/1169696 - allows you to do
if (MyEnumValue.In(MyEnum.First, MyEnum.Second, MyEnum.Third)) { ... }
-
brgerner over 12 years@the If it were possible to assign your extension method only to my enumeration it were perfect.
-
brgerner over 12 yearsThank you! By now the best answer.
-
brgerner over 12 yearsUpdate to my last comment: Trevor Pilley's answer solves that problem.
-
JimmiTh over 12 yearsNot my extension method, but it is possible. Adding an answer, since it takes space to demonstrate. Don't accept it, though - since we're far from your original question, that wouldn't be "fair".
-
Kevin Brock over 12 yearsA generic extension method would be more useful:
public static bool In<T>(this T value, params T[] values) { return values.Contains(value); }
. -
Trevor Pilley over 12 yearsYes that is true, I made it enum specific as that was what @brgerner asked about. Others have also posted examples using generics.
-
Огњен Шобајић over 8 yearsI am curious how it impacts performance? Does it create an array or something or it's just IEnumerable which should be optimal?