📌  相关文章
📜  用于检查指定类型的 C# 程序是否嵌套

📅  最后修改于: 2022-05-13 01:55:49.802000             🧑  作者: Mango

用于检查指定类型的 C# 程序是否嵌套

在编程语言中,嵌套意味着一个类或方法或循环或结构存在于另一个类或方法或循环或结构中。在 C# 中,我们可以使用 Type 类的IsNested属性检查指定的类型是否嵌套。此属性返回一个值,该值表示指定的类型(即类、结构等)定义是否嵌套在另一个类型定义中。如果指定的类型是嵌套的,它将返回 true ,否则返回 false。

句法:

public bool IsNested { get; }

示例 1:

C#
// C# program to check a specified 
// type is nested or not
using System;
using System.Reflection;
  
// Create a structure
struct Geeks
{
      
    // Create a nested structure named Gfg2
    // with hello() method
    public struct Gfg2
    {
        void hello() 
        { 
            Console.WriteLine("hello geeks!"); 
        }
    }
}
  
class GFG{
      
// Driver code
static void Main()
{
      
    // Check the type is nested or not
    Console.WriteLine(typeof(Geeks.Gfg2).IsNested);
}
}


C#
// C# program to check a specified
// type is nested or not
using System;
using System.Reflection;
  
// Create a class
public class Geeks
{
      
    // Create a nested class
    // with hello() method
    public class Gfg2
    {
        void myfun() 
        { 
            Console.WriteLine("hello geeks!"); 
        }
    }
}
  
class GFG{
      
// Driver code
static void Main()
{
      
    // Check the type is nested or not
    if (typeof(Geeks.Gfg2).IsNested == true)
    {
        Console.WriteLine("Gfg2 class is nested class");
    }
    else
    {
        Console.WriteLine("Gfg2 class is not nested class");
          
    }
}
}


输出:

True

示例 2:

C#

// C# program to check a specified
// type is nested or not
using System;
using System.Reflection;
  
// Create a class
public class Geeks
{
      
    // Create a nested class
    // with hello() method
    public class Gfg2
    {
        void myfun() 
        { 
            Console.WriteLine("hello geeks!"); 
        }
    }
}
  
class GFG{
      
// Driver code
static void Main()
{
      
    // Check the type is nested or not
    if (typeof(Geeks.Gfg2).IsNested == true)
    {
        Console.WriteLine("Gfg2 class is nested class");
    }
    else
    {
        Console.WriteLine("Gfg2 class is not nested class");
          
    }
}
}

输出:

Gfg2 class is nested class