"eat" C # multiple options

return (
      Page is WebAdminPriceRanges ||
      Page is WebAdminRatingQuestions
);

Is there any way to do this like:

return (
    Page is WebAdminPriceRanges || WebAdminRatingQuestions
);
+5
source share
6 answers

No. The first way you have indicated is the only reasonable way to do this.

+1
source

No, this syntax is not possible. The operator requires 2 operands, the first an instance of the object, and the second type.

You can use GetType():

return new[] { typeof(WebAdminPriceRanges), typeof(WebAdminRatingQuestions) }.Contains(Page.GetType());
+3
source

. Type , , is; , is , , .

:

var types = new[] {typeof(WebAdminPriceRanges), typeof(WebAdminRatingQuestions)};

// this will return false if Page is e.g. a WebAdminBase
var is1 = types.Any(t => t == Page.GetType());

// while this will return true
var is2 = Page is WebAdminPriceRanges || Page is WebAdminRatingQuestions;
+3

, # , .

+1

, .

, , WebAdminPriceRanges WebAdminRatingQuestions, if.

:

if(Page is WebAdminPriceRanges || Page is WebAdminRatingQuestions)
   return Page;
return null;

Assuming Page is a reference type or a type with a null value

0
source

Other answers are correct, but so far I’m not sure where the operator’s priority fits. If the is operator is lower than the logical or operator, you would separate the two classes, which makes no sense.

0
source

All Articles