是否可以在不指定大小的情况下创建空数组?
例如,我创建了:
String[] a = new String[5];
Run Code Online (Sandbox Code Playgroud)
我们可以创建没有大小的上面的字符串数组吗?
public class EnumRouteConstraint<T> : IRouteConstraint
where T : struct
{
private static readonly Lazy<HashSet<string>> _enumNames; // <--
static EnumRouteConstraint()
{
if (!typeof(T).IsEnum)
{
throw new ArgumentException(
Resources.Error.EnumRouteConstraint.FormatWith(typeof(T).FullName));
}
string[] names = Enum.GetNames(typeof(T));
_enumNames = new Lazy<HashSet<string>>(() => new HashSet<string>
(
names.Select(name => name), StringComparer.InvariantCultureIgnoreCase
));
}
public bool Match(HttpContextBase httpContext, Route route,
string parameterName, RouteValueDictionary values,
RouteDirection routeDirection)
{
bool match = _enumNames.Value.Contains(values[parameterName].ToString());
return match;
}
}
Run Code Online (Sandbox Code Playgroud)
这是错的吗?我会假设这实际上有一个static readonly字段用于EnumRouteConstraint<T>我碰巧实例化的每一个.
我的属性绑定数据访问类有一个小问题(更像是一个烦恼)。问题是当读取器中不存在类中相应属性的列时,映射会失败。
这是映射器类:
// Map our datareader object to a strongly typed list
private static IList<T> Map<T>(DbDataReader dr) where T : new()
{
try
{
// initialize our returnable list
List<T> list = new List<T>();
// fire up the lamda mapping
var converter = new Converter<T>();
while (dr.Read())
{
// read in each row, and properly map it to our T object
var obj = converter.CreateItemFromRow(dr);
// add it to our list
list.Add(obj);
}
// reutrn it
return list;
} …Run Code Online (Sandbox Code Playgroud) c# reflection expression-trees dbdatareader linq-expressions
我看到以下语法:
var comparer = Comparer<TItem>.Default;
Run Code Online (Sandbox Code Playgroud)
这种语法如何工作?
我Comparer原本以为必须是新手
c# ×4
generics ×2
.net ×1
arrays ×1
c#-4.0 ×1
dbdatareader ×1
reflection ×1
resharper ×1
static ×1