English 中文(简体)
声明一种方法中的等级或支架
原标题:Declare a class or struct inside a method
  • 时间:2012-05-23 11:04:00
  •  标签:
  • c#
  • .net

在C# # 中,能否像在C++ 中那样,在一种方法中声明一个类别或支架?

例如,C++:

void Method()
{
   class NewClass
   {
   } newClassObject;
}

我试过了,但不允许我这样做。

问题回答

您可以创建匿名型号, 如 :

var x = new { x = 10, y = 20 };

但除此以外:没有。

是, 可以在 < code> class 中声明 class 中有一个 class , 这些被称为 < code> Inner class

public class Foo
{
    public class Bar
    { 

    }
 }

和此您如何创建一个实例

Foo foo = new Foo();
Foo.Bar bar = new Foo.Bar();

在一种方法中,您可以创建一个 匿名 类型的对象

void Fn()
{
 var anonymous= new { Name="name" , ID=2 };
 Console.WriteLine(anonymous.Name+"  "+anonymous.ID);
}

您可以按照您的问题在“坚固”等级中声明它们,但不能按照您的问题标题在“坚固”方法中声明它们。

public class MyClass
{
    public class MyClassAgain
    {
    }

    public struct MyStruct
    {
    }
}

在C##中,此时此刻,在方法上没有地方班级,但有变通办法:

  1. 使用预编译器将类描述移动到您的方法之外( Roslyn 在此会有所帮助)

  2. 如果您已经有一个接口, 您可以使用 NuGet 软件包 Impromptu Inpromptu Interface 在您的方法范围内创建本地类

  3. 使用本地方法模拟类 :

     class Program
     {
         static void Main(string[] args)
         {
             dynamic newImpl()
             {
                 int f1 = 5;
                 return new { 
                     M1 = (Func<int, int, int>)((c, d) => c + d + f1), 
                     setF1 = (Func<int,int>)( p => { var old = f1; f1 = p; return old; 
                            }) };
             }
             var i1Impl = newImpl();
             var i2Impl = newImpl();
             int res;
             res = i1Impl.M1(5, 6);
             Console.WriteLine(res);
    
             i1Impl.setF1(10);
    
             res = i1Impl.M1(5, 6);
             Console.WriteLine(res);
    
             res = i2Impl.M1(2, 3);
             Console.WriteLine(res);
    
             res = i1Impl.M1(1, 2);
             Console.WriteLine(res);
         }
     }
    

上述指纹:16,21,10,13。

Today You can use tuple fields names https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/builtin-types/value-tuples#tuple-field-names

类似

void GetData() {
List <(string h,int hideClm, double cw, HtColumns t)> k1 = new List<(string h, int hideClm, double cw, HtColumns t)>();
        k1.Add((h: "",0,cw:50, t: new HtColumns() { type = "text" }));
        k1.Add((h: "Record time",0, cw: 190, t: new HtColumns() { type = "text" }));
        k1.Add((h: "Partner",0, cw: 290, t: new HtColumns() { type = "text" }));
}

有趣的是,运行时间确实有必要支持本地类型: System.Type 类包含一个 Declaring Method 成员,当该类型在一种方法中被宣布为匿名类型时,将设置该成员。但该语言不支持在方法中宣布任何非图普尔和匿名类型。

可惜的是,因为当地方法类型在测试中非常有用,以便:

  1. Keep each type close to where it is used
  2. Avoid polluting the namespace of the test class
  3. Prevent one test method from accidentally using types meant to be used by another test method.

为实现上述三个目标中的两个目标,我做以下工作:

  1. I declare my test class as partial.
  2. I put each test method in a separate part of the test class. (All parts in the same file.)
  3. The types that would have been declared within test methods if the language allowed it, can then be declared between the parts of the test class, in essence right before each method that they belong to.




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签