【问题标题】:how to initialize in C# like c++ [duplicate]如何在 C# 中初始化,如 c++ [重复]
【发布时间】:2018-06-06 13:36:37
【问题描述】:

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

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

想不通

【问题讨论】:

  • 什么是你想不通的?
  • 这在 c# 中如何有效 public file_list_output(ListView v) => veiw = v;
  • 你所拥有的是无效的,我在下面放置了一个答案,显示了如何使用表达式体构造函数。

标签: c# class initialization


【解决方案1】:

你需要这样做:

所以你的类有两个属性:

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); 

【讨论】:

  • 感谢 Ryan,我不确定我是否是 c# 的忠实粉丝,时间会证明一切
  • @joeblogs 不客气。我更新了这个以用更多的 cmets 更好地解释它。如果您对我的回答感到满意,请将其标记为已接受的答案。 :)
  • 为此欢呼,public int max_ext_allowed { get;私人套装; }。没有关于那个
  • @joeblogs 是的,它基本上是拥有一个只有 get 方法的私有变量的简写,该值只能在初始化时设置。
  • 这个错误可能来自哪里。 System>ValueTuple2 未定义或导入 ------(v, max);显示为罪魁祸首
猜你喜欢
  • 2012-02-12
  • 1970-01-01
  • 1970-01-01
  • 2012-12-08
  • 1970-01-01
  • 2012-10-06
  • 1970-01-01
  • 2016-02-09
  • 2014-03-23
相关资源
最近更新 更多