在C# # 中,能否像在C++ 中那样,在一种方法中声明一个类别或支架?
例如,C++:
void Method()
{
class NewClass
{
} newClassObject;
}
我试过了,但不允许我这样做。
在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##中,此时此刻,在方法上没有地方班级,但有变通办法:
使用预编译器将类描述移动到您的方法之外( Roslyn 在此会有所帮助)
如果您已经有一个接口, 您可以使用 NuGet 软件包 Impromptu Inpromptu Interface 在您的方法范围内创建本地类
使用本地方法模拟类 :
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
成员,当该类型在一种方法中被宣布为匿名类型时,将设置该成员。但该语言不支持在方法中宣布任何非图普尔和匿名类型。
可惜的是,因为当地方法类型在测试中非常有用,以便:
为实现上述三个目标中的两个目标,我做以下工作:
partial
.What is the use of default keyword in C#? Is it introduced in C# 3.0 ?
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. ...
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 ...
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 ...
I have two EF entities. One has a property called HouseNumber. The other has two properties, one called StartHouseNumber and one called EndHouseNumber. I want to create a many to many association ...
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, ...
Since I cannot order my dictionary, what is the best way of going about taking key value pairs and also maintaing an index?
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. ...