作者:w3shuajiang2 | 来源:互联网 | 2023-05-16 14:10
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 个解决方案