官术网_书友最值得收藏!

Type pattern

The type pattern can be used with an object to verify whether it matches the type or suffices the expression based on the conditions specified. Suppose we need to check whether the PersonID is int; assign that ID to another variable, i, and use it in the program, otherwise return:

if (!(person.ID is int i)) return; 
 
Console.WriteLine($"Person ID is {i}"); 

We can also use multiple logical operators to evaluate more conditions, as follows:

if (!(person.ID is int i) && !(person.DOB>DateTime.Now.AddYears(-20))) return;   

The preceding statement checks whether the Person.ID is null or not and whether the person is older than 20.

主站蜘蛛池模板: 永定县| 翁牛特旗| 故城县| 邓州市| 左权县| 保亭| 佛山市| 桂东县| 吴江市| 普安县| 中西区| 墨脱县| 赤水市| 清远市| 利辛县| 山东省| 九台市| 任丘市| 东乡族自治县| 巢湖市| 阜新市| 宁夏| 鹤庆县| 玛多县| 唐海县| 夏河县| 明光市| 庆云县| 五指山市| 陵水| 蓬安县| 常德市| 汝南县| 镇赉县| 南京市| 辉县市| 浦北县| 福州市| 遵化市| 如皋市| 勐海县|