首页 > 解决方案 > 如何像 C++ 一样在 C# 中初始化

问题描述

如何在 C# 中初始化多个变量?

class file_list_output
{
   public file_list_output(ListView v, const int max) => veiw = v => max_ext_allowed = max;
}

想不通

标签: c#classinitialization

解决方案


你需要这样做:

所以你的类有两个属性:

public int Max_ext_allowed {get; private set;}
public ListView View {get; private set;}

//To do an expression bodied constructor
//The fat arrow => is replacing the typical { }
//And the next portion (View, Max_ext_allowed) is saying these are the properties being initialized
//Finally the last portion (v, max) is the set of values to set the properties to
public file_list_input(ListView v, int max) => (View, Max_ext_allowed) = (v, max); 

推荐阅读