方法名称中的 new() 是什么意思 C# 语法
What does new() in method name means C# syntax
我正在阅读库的代码,我看到了以下语法。我在 Google 上进行了很多安静的搜索以找出语法名称,但我什么也没找到。任何帮助将不胜感激。
/// <summary>
/// Returns a singleton object that is used to manage the creation and
/// execution of setup
/// </summary>
/// <typeparam name="TMvxSetupSingleton">The platform specific setup singleton type</typeparam>
/// <returns>A platform specific setup singleton</returns>
protected static TMvxSetupSingleton EnsureSingletonAvailable<TMvxSetupSingleton>()
where TMvxSetupSingleton : MvxSetupSingleton, new()
{
// Double null - check before creating the setup singleton object
if (Instance != null)
return Instance as TMvxSetupSingleton;
lock (LockObject)
{
if (Instance != null)
return Instance as TMvxSetupSingleton;
// Go ahead and create the setup singleton, and then
// create the setup instance.
// Note that the Instance property is set within the
// singleton constructor
var instance = new TMvxSetupSingleton();
instance.CreateSetup();
return Instance as TMvxSetupSingleton;
}
}
请注意,new(){。这是什么意思?
来自微软文档
where 子句还可以包含构造函数约束 new()。该约束使得使用 new 运算符创建类型参数的实例成为可能。 new() 约束让编译器知道提供的任何类型参数都必须具有可访问的无参数构造函数。例如:
public class MyGenericClass<T> where T : IComparable<T>, new()
{
// The following line is not possible without new() constraint:
T item = new T();
}
new() 约束出现在 where 子句的最后。 new() 约束不能与结构或非托管约束结合使用。满足这些约束的所有类型都必须具有可访问的无参数构造函数,从而使 new() 约束变得多余。
根据 MSDN:
The new
constraint specifies that a type argument in a generic class declaration must have a public parameterless constructor. To use the new
constraint, the type cannot be abstract.
Apply the new
constraint to a type parameter when a generic class
creates new instances of the type, as shown in the following example:
class ItemFactory<T> where T : new()
{
public T GetNewItem()
{
return new T();
}
}
When you use the new()
constraint with other constraints, it must be specified last:
public class ItemFactory2<T>
where T : IComparable, new()
{ }
我正在阅读库的代码,我看到了以下语法。我在 Google 上进行了很多安静的搜索以找出语法名称,但我什么也没找到。任何帮助将不胜感激。
/// <summary>
/// Returns a singleton object that is used to manage the creation and
/// execution of setup
/// </summary>
/// <typeparam name="TMvxSetupSingleton">The platform specific setup singleton type</typeparam>
/// <returns>A platform specific setup singleton</returns>
protected static TMvxSetupSingleton EnsureSingletonAvailable<TMvxSetupSingleton>()
where TMvxSetupSingleton : MvxSetupSingleton, new()
{
// Double null - check before creating the setup singleton object
if (Instance != null)
return Instance as TMvxSetupSingleton;
lock (LockObject)
{
if (Instance != null)
return Instance as TMvxSetupSingleton;
// Go ahead and create the setup singleton, and then
// create the setup instance.
// Note that the Instance property is set within the
// singleton constructor
var instance = new TMvxSetupSingleton();
instance.CreateSetup();
return Instance as TMvxSetupSingleton;
}
}
请注意,new(){。这是什么意思?
来自微软文档
where 子句还可以包含构造函数约束 new()。该约束使得使用 new 运算符创建类型参数的实例成为可能。 new() 约束让编译器知道提供的任何类型参数都必须具有可访问的无参数构造函数。例如:
public class MyGenericClass<T> where T : IComparable<T>, new()
{
// The following line is not possible without new() constraint:
T item = new T();
}
new() 约束出现在 where 子句的最后。 new() 约束不能与结构或非托管约束结合使用。满足这些约束的所有类型都必须具有可访问的无参数构造函数,从而使 new() 约束变得多余。
根据 MSDN:
The
new
constraint specifies that a type argument in a generic class declaration must have a public parameterless constructor. To use thenew
constraint, the type cannot be abstract.Apply the
new
constraint to a type parameter when a generic class creates new instances of the type, as shown in the following example:
class ItemFactory<T> where T : new()
{
public T GetNewItem()
{
return new T();
}
}
When you use the
new()
constraint with other constraints, it must be specified last:
public class ItemFactory2<T>
where T : IComparable, new()
{ }