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

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.

主站蜘蛛池模板: 温泉县| 丰镇市| 周口市| 永嘉县| 太谷县| 宣汉县| 蛟河市| 龙里县| 汝南县| 靖西县| 突泉县| 成武县| 延吉市| 裕民县| 陈巴尔虎旗| 长葛市| 区。| 鞍山市| 淮安市| 当雄县| 柳河县| 闽侯县| 兴隆县| 九江县| 松滋市| 尚义县| 博客| 永丰县| 临沂市| 缙云县| 孟连| 芒康县| 洞口县| 东山县| 嵊州市| 永清县| 尉犁县| 大方县| 达孜县| 内黄县| 法库县|