In a statement in C #

I think the Commodore 128 Basic had a statement.
Does the operator in C # also?

I mean, there is an operator of the form

if(aString in ["a", "b", "c"]) Console.Out.WriteLine("aString is 'a', 'b' or 'c'"); 

Edit1: Currently, I need to determine if the enum value has a range of enum values.

Edit2: Thanks to everyone for the solutions to Contains (). I will use it in the future. But currently I have a need for enumeration values. Is it possible to 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, this was not Basic. I was just looking for hiking and found Turbo Pascal as a candidate where I could see him. See http://en.wikipedia.org/wiki/Pascal_%28programming_language%29#Set_types

Edit4: The best answers to date (end February 15, 2012):

  • For lists and arrays: accepted answer and all other answers with Contains () solutions
  • For Enums: Kaneda's answer with a good list of pros and cons for various extension methods
+6
source share
12 answers

Try the following:

 if(new [] {"a", "b", "c"}.Contains(aString)) Console.Out.WriteLine("aString is 'a', 'b' or 'c'"); 

The Contains method is used to find the array for aString .

+11
source

Not. You can get closer:

 if (new [] { "a", "b", "c" }.Contains(aString)) Console.Out.WriteLine("aString is 'a', 'b' or 'c'"); 
+3
source

the equivalent in C # would be Contains() (assuming you have a list or data array)

 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 purpose:

 var myStuff = new List<string> { "a", "b", "c" }; var aString = "a"; foreach(string str in myStuff) { //Iteration 0 = a, 1 = b, 2 = c } 
+3
source

You can use the extension method if you want a slightly freer way to work for this example client and client state:

 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); } } 

so you can write code like this:

 private void DoSomething() { var customer = new Customer { Status = CustomerStatus.Active }; if (customer.Status.In(CustomerStatus.Active, CustomerStatus.Inactive)) { // Do something. } } 
+3
source

Thanks to my Delphi days, I'm also used to the keyword at . For the general case in C #, I use:

 public static class Helper { public static bool In<T>(this T t, params T[] args) { return args.Contains(t); } } } 

which can be used as follows:

  var color = Color.Aqua; var b = color.In(Color.Black, Color.Blue); b = "hello".In("hello", "world"); 
+3
source

Try the following:

 if ("abc".Contains(aString[0])) Console.WriteLine("aString is 'a', 'b' or 'c'"); 
+2
source

Most collections will have a .Contains method, and LINQ also has a Contains method, so anything enumerated that does not yet have its own method contains a LINQ method.

+1
source

Based on your code, you want to see if aString is equal to "a", "b" or "c", but does not contain "a", "b", or "c". If I read the question correctly, then:

Not. Instead, you should use the switch statement

 switch(aString) { case "a:": case "b:": case "b:": Console.Out.WriteLine("aString is 'a', 'b' or 'c'"); default: Console.Out.WriteLine("aString is NOT 'a', 'b' or 'c'"); } 
+1
source

Use the contains method:

 string[] values = { "A", "B", "C" }; if (values.Contains("A")) //True MessageBox.Show("A is there"); if (values.Contains("b")) //false, strings are case sensitive MessageBox.Show("b is there"); 
+1
source

The in keyword is used only for foreach. And no, there is no such operator designed for this purpose. But you can use the built-in methods of array type or list type, as shown below:

  string aString = "a"; string[] strings = new string[] { "a", "b", "c" }; if (strings.Contains(aString)) //Contains here is a Linq extension Console.WriteLine("aString is either a, b, or c"); List<string> stringList = new List<string>() { "a", "b", "c" }; if(stringList.Contains(aString)) //Contains here is a member method Console.WriteLine("aString is either a, b, or c"); if(stringList.IndexOf(aString) != -1) Console.WriteLine("aString is either a, b, or c"); 
+1
source

To answer the following question about Andrew Har's answer, yes, you can restrict the extension method In single enumeration. Do not accept this answer, as Andrei and Trevor answered the revised question (and most answered the original ... anyway).

To repeat, this is what I find most useful: fooobar.com/questions/256557 / ...

But if you really want to limit it, it is just a matter of using an Enum type as a parameter instead of a general (or Enum) one:

 // This is the one we want to use our In extension method on: public enum FriendlyEnum { A, B, C } public enum EnemyEnum { A, B, C } // The extension method: public static class FriendlyEnumExtensions { public static bool In(this FriendlyEnum value, params FriendlyEnum[] list) { return list.Contains(value); } } class Program { static void Main(string[] args) { FriendlyEnum friendlyValue = FriendlyEnum.A; EnemyEnum enemyValue = EnemyEnum.A; // Outputs "True": Console.WriteLine(friendlyValue.In(FriendlyEnum.A, FriendlyEnum.C)); // Outputs "False": Console.WriteLine(friendlyValue.In(FriendlyEnum.B, FriendlyEnum.C)); // All of these will result in compiler errors, // because EnemyEnum is invading some way or another: Console.WriteLine(friendlyValue.In(EnemyEnum.A, EnemyEnum.B)); Console.WriteLine(enemyValue.In(FriendlyEnum.A, FriendlyEnum.B)); Console.WriteLine(enemyValue.In(EnemyEnum.A, EnemyEnum.B)); } } 

I find it less useful than the general one that I linked, or Trevor's approach, but there you go.

Update

The difference between the approaches:

  • Using the Trevor method, all three enemy and friend mixes in the code above will be accepted by the compiler (the output will be "True", "False", "False", "False", "True" ").

  • Using a general approach, the latter will be accepted (and "Truth") because generics only ensure that all parameters (including this ) are of the same type. Ie, it will not accept mixing different enumerations in one call.

  • In the one above, again, only one enumeration that you have developed for the extension method will be accepted.

+1
source

I am surprised that no one suggested switch :

 class MyEnumHelper { void IsSpecialSet(MyEnum e) { return e in [MyEnum.A, MyEnum.C] switch (e) { case MyEnum.A: case MyEnum.C: return true; default: return false; } } } 
0
source

Source: https://habr.com/ru/post/908550/


All Articles