如果我有一个需要参数的方法,
Count房产这个参数的类型应该是什么?我会IList<T>在.NET 4.5之前选择,因为没有其他可索引的集合接口,并且数组实现它,这是一个很大的优点.
但.NET 4.5引入了新的IReadOnlyList<T>界面,我也希望我的方法能够支持它.我如何编写这种方法来支持这两种方法,IList<T>而IReadOnlyList<T>不是违反像DRY这样的基本原则?
编辑:丹尼尔的回答给了我一些想法:
public void Foo<T>(IList<T> list)
=> Foo(list, list.Count, (c, i) => c[i]);
public void Foo<T>(IReadOnlyList<T> list)
=> Foo(list, list.Count, (c, i) => c[i]);
private void Foo<TList, TItem>(
TList list, int count, Func<TList, int, TItem> indexer)
where TList : IEnumerable<TItem>
{
// Stuff
}
Run Code Online (Sandbox Code Playgroud)
编辑2:或者我可以接受一个IReadOnlyList<T>并提供这样的帮助:
public static class CollectionEx
{
public static IReadOnlyList<T> AsReadOnly<T>(this IList<T> list)
{
if (list == …Run Code Online (Sandbox Code Playgroud) 假设我Product在购物网站的数据库中有一张桌子,用于保存商店产品的描述,价格等.使我的客户能够重新订购这些产品的最有效方法是什么?
我创建了一个Order用于排序记录的列(整数),但由于我在实际需要更改之后用于更改每个记录顺序的原始方法,这让我对性能产生了一些麻烦.一个例子:
Id Order
5 3
8 1
26 2
32 5
120 4
Run Code Online (Sandbox Code Playgroud)
现在我该怎么做才能将记录的顺序更改ID=26为3?
我所做的是创建一个程序,检查目标订单中是否有记录(3),如果没有,则更新行的顺序(ID = 26).如果目标顺序中有记录,则该过程将自行发送该行的ID target order + 1作为参数.
这导致在我想要改变以便腾出空间之后更新每一条记录:
Id Order
5 4
8 1
26 3
32 6
120 5
Run Code Online (Sandbox Code Playgroud)
那么聪明人会做些什么呢?
编辑:
我需要一个项目的订单列足以进行排序,而不涉及二级密钥.单独的订单列必须为其记录指定唯一的位置.
除此之外,我想知道我是否可以实现链接列表之类的内容:"下一个"列而不是"订单"列以保留下一个项目ID.但我不知道如何编写以正确顺序检索记录的查询.如果有人也对这种方法有所了解,请分享.
在Async/Await FAQ中,Stephen Toub说:
一个awaitable是任何类型的,它公开
GetAwaiter它返回一个有效的方法awaiter.
...
一个awaiter是任何从一个返回类型awaitable的GetAwaiter方法和符合特定的图案.
因此,为了成为awaiter,类型应该:
INotifyCompletion接口.IsCompleted.GetResult返回void或无参数的方法TResult.(我现在忽略ICriticalNotifyCompletion了.)
我知道我提到的页面有一个示例,显示编译器如何转换等待操作,但我很难理解.
当我等待的awaitable,
IsCompleted检查?我应该在哪里设置它?OnCompleted召唤?OnCompleted?OnCompleted和Task.Run(continuation)在不同示例中使用的示例,我应该选择哪个以及为什么?我需要一个线程安全的集合来保存没有重复的项目.ConcurrentBag<T>允许非唯一项,HashSet<T>并且不是线程安全的.在.NET Framework 4.5中是否有这样的集合?
考虑以下课程:
class Program
{
static void Test()
{
TestDelegate<string, int>(s => s.Length);
TestExpressionTree<string, int>(s => s.Length);
}
static void TestDelegate<T1, T2>(Func<T1, T2> del) { /*...*/ }
static void TestExpressionTree<T1, T2>(Expression<Func<T1, T2>> exp) { /*...*/ }
}
Run Code Online (Sandbox Code Playgroud)
这是编译器生成的内容(以稍微不易读的方式):
class Program
{
static void Test()
{
// The delegate call:
TestDelegate(Cache.Func ?? (Cache.Func = Cache.Instance.FuncImpl));
// The expression call:
var paramExp = Expression.Parameter(typeof(string), "s");
var propExp = Expression.Property(paramExp, "Length");
var lambdaExp = Expression.Lambda<Func<string, int>>(propExp, paramExp);
TestExpressionTree(lambdaExp);
}
static void TestDelegate<T1, …Run Code Online (Sandbox Code Playgroud) 如何在CSS文件中使用Razor?
我正在使用Razor View Engine一段时间,我很好奇在样式表上使用它.我可以在.cshtml文件<style>块中使用Razor,但我想知道我是否也可以在外部.css文件中使用它(希望有.cscss格式).所以我用Google搜索并发现了两件事:
第一个是LESS:"动态样式表语言".它似乎易于使用且功能强大,具有所有功能,但我需要Razor-C#,真的.
第二个是动态CSS使用Razor引擎,一个CodeProject文章更像我想要的但它没有缓存或预编译支持("不支持"我的意思是作者没有提到这些方面).我也希望在Visual Studio中有一些语法高亮,但这是次要的.
那么,我如何以最低的性能成本在CSS文件中编写Razor,最好是语法高亮?
作为旁注:
我找到了一个名为RazorJS的项目.它就像我想要的CSS的Javascript版本以及它的缓存支持.我提到这只是为了澄清我的需求.我现在不需要在Javascript中使用Razor,但我想如果我用CSS制作它,用Javascript做同样的事情也不会太难.
使用XML文档的示例方法:
// summary and param tags are here when you're not looking.
/// <exception cref="ArgumentNullException>
/// <paramref name="text" /> is null.
/// </exception>
public void Write(string text)
{
if (text == null)
throw new ArgumentNullException("text", "Text must not be null.");
// sync stuff...
}
Run Code Online (Sandbox Code Playgroud)
Write(null)按预期抛出异常.这是一个异步方法:
public async Task WriteAsync(string text)
{
if (text == null)
throw new ArgumentNullException("text", "Text must not be null.");
// async stuff...
}
Run Code Online (Sandbox Code Playgroud)
WriteAsync(null),等待之前不会抛出异常.我应该ArgumentNullException在exception标签中指定吗?我认为这会让消费者认为调用WriteAsync可能会抛出ArgumentNullException并写下这样的东西:
Task t; …Run Code Online (Sandbox Code Playgroud) 在派生时ServiceBase,我是否也应该调用基类的方法?
protected override void OnStart(string[] args)
{
//
// The stuff I do when the service starts.
//
base.OnStart(args); // Do I need to call this?
}
Run Code Online (Sandbox Code Playgroud) var dict = new Dictionary<int, string>();
for (int i = 0; i < 200000; i++)
dict[i] = "test " + i;
Run Code Online (Sandbox Code Playgroud)
我使用下面的代码迭代这本字典:
foreach (var pair in dict)
Console.WriteLine(pair.Value);
Run Code Online (Sandbox Code Playgroud)
然后,我用这个迭代它:
foreach (var key in dict.Keys)
Console.WriteLine(dict[key]);
Run Code Online (Sandbox Code Playgroud)
第二次迭代减少了约3秒.我可以通过两种方法获得键和值.我想知道第二种方法是否有缺点.由于我能找到的评价最高的问题不包括这种迭代字典的方式,我想知道为什么没有人使用它以及它如何更快地工作.
我有一个只有一个字段的不可变结构:
struct MyStruct
{
private readonly double number;
public MyStruct(double number)
=> this.number = number;
}
Run Code Online (Sandbox Code Playgroud)
我希望能够通过以下方式进行序列化/反序列化:
所以结构变成了这样:
[Serializable]
struct MyStruct : ISerializable, IXmlSerializable
{
private readonly double number;
public MyStruct(double number)
=> this.number = number;
private MyStruct(SerializationInfo info, StreamingContext context)
=> this.number = info.GetDouble(nameof(this.number));
void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context)
=> info.AddValue(nameof(this.number), this.number);
XmlSchema IXmlSerializable.GetSchema() => null;
void IXmlSerializable.ReadXml(XmlReader reader)
{
// Necessary evil
reader.Read();
this = new MyStruct(double.Parse(reader.Value, CultureInfo.InvariantCulture));
}
void IXmlSerializable.WriteXml(XmlWriter writer)
=> writer.WriteString(this.number.ToString(CultureInfo.InvariantCulture)); …Run Code Online (Sandbox Code Playgroud) c# ×8
.net ×6
.net-4.5 ×2
asynchronous ×2
collections ×2
async-await ×1
c#-7.2 ×1
compilation ×1
concurrency ×1
css ×1
database ×1
delegates ×1
dictionary ×1
interface ×1
iteration ×1
json.net ×1
lambda ×1
razor ×1
server-side ×1
service ×1
sorting ×1
sql-server ×1