热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

C#:'is'关键字并检查Not-C#:'is'keywordandcheckingforNot

Thisisasillyquestion,butyoucanusethiscodetocheckifsomethingisaparticulartype这是

This is a silly question, but you can use this code to check if something is a particular type...

这是一个愚蠢的问题,但您可以使用此代码来检查某些内容是否属于特定类型...

if (child is IContainer) { //....

Is there a more elegant way to check for the "NOT" instance?

是否有更优雅的方法来检查“NOT”实例?

if (!(child is IContainer)) { //A little ugly... silly, yes I know...

//these don't work :)
if (child !is IContainer) {
if (child isnt IContainer) { 
if (child aint IContainer) { 
if (child isnotafreaking IContainer) { 

Yes, yes... silly question....

是的,是的......愚蠢的问题....

Because there is some question on what the code looks like, it's just a simple return at the start of a method.

因为对代码的外观存在一些疑问,所以它只是在方法开始时的简单返回。

public void Update(DocumentPart part) {
    part.Update();
    if (!(DocumentPart is IContainer)) { return; }
    foreach(DocumentPart child in ((IContainer)part).Children) {
       //...etc...

10 个解决方案

#1


if(!(child is IContainer))

is the only operator to go (there's no IsNot operator).

是唯一的运营商(没有IsNot运营商)。

You can build an extension method that does it:

您可以构建一个执行它的扩展方法:

public static bool IsA(this object obj) {
    return obj is T;
}

and then use it to:

然后用它来:

if (!child.IsA())

And you could follow on your theme:

你可以跟随你的主题:

public static bool IsNotAFreaking(this object obj) {
    return !(obj is T);
}

if (child.IsNotAFreaking()) { // ...

Update (considering the OP's code snippet):

Since you're actually casting the value afterward, you could just use as instead:

由于您之后实际上正在构建值,因此您可以使用as:

public void Update(DocumentPart part) {
    part.Update();
    IContainer cOntainerPart= part as IContainer;
    if(cOntainerPart== null) return;
    foreach(DocumentPart child in containerPart.Children) { // omit the cast.
       //...etc...

#2


You can do it this way:

你可以这样做:

object a = new StreamWriter("c:\\temp\\test.txt");

if (a is TextReader == false)
{
   Console.WriteLine("failed");
}

#3


Why not just use the else ?

为什么不使用其他?

if (child is IContainer)
{
  //
}
else
{
  // Do what you want here
}

Its neat it familiar and simple ?

它整洁而熟悉又简单?

#4


The way you have it is fine but you could create a set of extension methods to make "a more elegant way to check for the 'NOT' instance."

你拥有它的方式很好,但你可以创建一组扩展方法,使“更优雅的方式来检查'NOT'实例。”

public static bool Is(this object myObject)
{
    return (myObject is T);
}

public static bool IsNot(this object myObject)
{
    return !(myObject is T);
}

Then you could write:

然后你可以写:

if (child.IsNot())
{
    // child is not an IContainer
}

#5


Ugly? I disagree. The only other way (I personally think this is "uglier"):

丑陋?我不同意。唯一的另一种方式(我个人认为这是“丑陋的”):

var obj = child as IContainer;
if(obj == null)
{
   //child "aint" IContainer
}

#6


The is operator evaluates to a boolean result, so you can do anything you would otherwise be able to do on a bool. To negate it use the ! operator. Why would you want to have a different operator just for this?

is运算符求值为布尔结果,因此您可以执行任何您在bool上可以执行的操作。否定它使用!运营商。你为什么要为此拥有一个不同的运算符?

#7


While the IS operator is normally the best way, there is an alternative that you can use in some cirumstances. You can use the as operator and test for null.

虽然IS运营商通常是最好的方式,但有一种替代方案可以在某些情况下使用。您可以使用as运算符并测试null。

MyClass mc = foo as MyClass;
if ( mc == null ) { }
else {}

#8


The extension method IsNot is a nice way to extend the syntax. Keep in mind

扩展方法IsNot 是一种扩展语法的好方法。记住

var cOntainer= child as IContainer;
if(container != null)
{
  // do something w/ contianer
}

performs better than doing something like

比做某事更好

if(child is IContainer)
{
  var cOntainer= child as IContainer;
  // do something w/ container
}

In your case, it doesn't matter as you are returning from the method. In other words, be careful to not do both the check for type and then the type conversion immediately after.

在您的情况下,从方法返回时无关紧要。换句话说,请注意不要同时检查类型,然后立即进行类型转换。

#9


While this doesn't avoid the problem of parentheses, for the sake of people getting here via Google, it should be mentioned that newer syntax exists (as of C# 7) to make the rest of your code a little cleaner:

虽然这不能避免括号问题,但是为了让人们通过谷歌来到这里,应该提到的是存在更新的语法(从C#7开始),以使代码的其余部分更清晰:

if (!(DocumentPart is IContainer container)) { return; }
foreach(DocumentPart child in container.Children) {
    ...

This avoids the double-cast, the null-check, and having a variable available in scopes where it could be null.

这避免了双重转换,空检查,并且在范围中可以使用变量,它可以为null。

#10


if (child is IContainer ? false : true)

推荐阅读
  • 本文介绍了在实现了System.Collections.Generic.IDictionary接口的泛型字典类中如何使用foreach循环来枚举字典中的键值对。同时还讨论了非泛型字典类和泛型字典类在foreach循环中使用的不同类型,以及使用KeyValuePair类型在foreach循环中枚举泛型字典类的优势。阅读本文可以帮助您更好地理解泛型字典类的使用和性能优化。 ... [详细]
  • 向QTextEdit拖放文件的方法及实现步骤
    本文介绍了在使用QTextEdit时如何实现拖放文件的功能,包括相关的方法和实现步骤。通过重写dragEnterEvent和dropEvent函数,并结合QMimeData和QUrl等类,可以轻松实现向QTextEdit拖放文件的功能。详细的代码实现和说明可以参考本文提供的示例代码。 ... [详细]
  • 本文分享了一个关于在C#中使用异步代码的问题,作者在控制台中运行时代码正常工作,但在Windows窗体中却无法正常工作。作者尝试搜索局域网上的主机,但在窗体中计数器没有减少。文章提供了相关的代码和解决思路。 ... [详细]
  • Linux重启网络命令实例及关机和重启示例教程
    本文介绍了Linux系统中重启网络命令的实例,以及使用不同方式关机和重启系统的示例教程。包括使用图形界面和控制台访问系统的方法,以及使用shutdown命令进行系统关机和重启的句法和用法。 ... [详细]
  • 本文介绍了C#中数据集DataSet对象的使用及相关方法详解,包括DataSet对象的概述、与数据关系对象的互联、Rows集合和Columns集合的组成,以及DataSet对象常用的方法之一——Merge方法的使用。通过本文的阅读,读者可以了解到DataSet对象在C#中的重要性和使用方法。 ... [详细]
  • 如何基于ggplot2构建相关系数矩阵热图以及一个友情故事
    本文介绍了如何在rstudio中安装ggplot2,并使用ggplot2构建相关系数矩阵热图。同时,通过一个友情故事,讲述了真爱难觅的故事背后的数据量化和皮尔逊相关系数的概念。故事中的小伙伴们在本科时参加各种考试,其中有些沉迷网络游戏,有些热爱体育,通过他们的故事,展示了不同兴趣和特长对学习和成绩的影响。 ... [详细]
  • 展开全部下面的代码是创建一个立方体Thisexamplescreatesanddisplaysasimplebox.#Thefirstlineloadstheinit_disp ... [详细]
  • Spring学习(4):Spring管理对象之间的关联关系
    本文是关于Spring学习的第四篇文章,讲述了Spring框架中管理对象之间的关联关系。文章介绍了MessageService类和MessagePrinter类的实现,并解释了它们之间的关联关系。通过学习本文,读者可以了解Spring框架中对象之间的关联关系的概念和实现方式。 ... [详细]
  • 本文介绍了在wepy中运用小顺序页面受权的计划,包含了用户点击作废后的从新受权计划。 ... [详细]
  • IOS开发之短信发送与拨打电话的方法详解
    本文详细介绍了在IOS开发中实现短信发送和拨打电话的两种方式,一种是使用系统底层发送,虽然无法自定义短信内容和返回原应用,但是简单方便;另一种是使用第三方框架发送,需要导入MessageUI头文件,并遵守MFMessageComposeViewControllerDelegate协议,可以实现自定义短信内容和返回原应用的功能。 ... [详细]
  • 使用圣杯布局模式实现网站首页的内容布局
    本文介绍了使用圣杯布局模式实现网站首页的内容布局的方法,包括HTML部分代码和实例。同时还提供了公司新闻、最新产品、关于我们、联系我们等页面的布局示例。商品展示区包括了车里子和农家生态土鸡蛋等产品的价格信息。 ... [详细]
  • 本文介绍了如何使用n3-charts绘制以日期为x轴的数据,并提供了相应的代码示例。通过设置x轴的类型为日期,可以实现对日期数据的正确显示和处理。同时,还介绍了如何设置y轴的类型和其他相关参数。通过本文的学习,读者可以掌握使用n3-charts绘制日期数据的方法。 ... [详细]
  • 本文讨论了在使用Git进行版本控制时,如何提供类似CVS中自动增加版本号的功能。作者介绍了Git中的其他版本表示方式,如git describe命令,并提供了使用这些表示方式来确定文件更新情况的示例。此外,文章还介绍了启用$Id:$功能的方法,并讨论了一些开发者在使用Git时的需求和使用场景。 ... [详细]
  • 抽空写了一个ICON图标的转换程序
    抽空写了一个ICON图标的转换程序,支持png\jpe\bmp格式到ico的转换。具体的程序就在下面,如果看的人多,过两天再把思路写一下。 ... [详细]
  • css div中文字位置_超赞的 CSS 阴影技巧与细节
    本文的题目是CSS阴影技巧与细节。CSS阴影,却不一定是box-shadow与filter:drop-shadow,为啥?因为使用其他属性 ... [详细]
author-avatar
w3shuajiang2
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有