所以,也许我很累,但为什么我不能创建一个新的MatchCollection
?无法创建新的MatchCollection - 没有定义构造函数
我有通过调用regex.Matches
返回MatchCollection
的方法:
public static MatchCollection GetStringsWithinBiggerString(string sourceString)
{
Regex regex = new Regex(@"\$\(.+?\)");
return regex.Matches(sourceString);
}
我想要做的是返回一个空集,如果该参数为空:
public static MatchCollection GetStringsWithinBiggerString(string sourceString)
{
if (sourceString == null)
{
return new MatchCollection();
}
Regex regex = new Regex(@"\$\(.+?\)");
return regex.Matches(sourceString);
}
但赢得” t因为这一行汇编:
return new MatchCollection();
错误:
The type 'System.Text.RegularExpressions.MatchCollection' has no constructors defined.
如何定义类型没有构造函数?如果没有明确定义构造函数,我认为会创建一个默认的构造函数。是否无法为我的方法返回MatchCollection
创建新实例?
其实如果你定义一个参数的构造函数,你不能使用默认的构造函数了,除非你明确的声明。 –
+1。 Ahhhh ......没有考虑到非公开的构造函数。哎呀。谢谢。所以我不能返回一个空的'MatchCollection',除非我在他的回答中提到的BrunoLM做一些变通? –