我总是假设如果我Select(x=> ...)在LINQ对象的上下文中使用,那么新的集合将立即创建并保持静态.我不太清楚为什么我这么想,这是一个非常糟糕的假设,但我做到了.我经常.ToList()在其他地方使用,但在这种情况下通常不会.
此代码演示即使是简单的"选择"也会延迟执行:
var random = new Random();
var animals = new[] { "cat", "dog", "mouse" };
var randomNumberOfAnimals = animals.Select(x => Math.Floor(random.NextDouble() * 100) + " " + x + "s");
foreach (var i in randomNumberOfAnimals)
{
testContextInstance.WriteLine("There are " + i);
}
foreach (var i in randomNumberOfAnimals)
{
testContextInstance.WriteLine("And now, there are " + i);
}
Run Code Online (Sandbox Code Playgroud)
这将输出以下内容(每次迭代集合时都会调用随机函数):
There are 75 cats
There are 28 dogs
There are 62 mouses
And now, there are 78 …Run Code Online (Sandbox Code Playgroud) 毫无疑问,我还没有受到任何阅读速度瓶颈的影响.我要知道; 如果经常阅读app.config是一个糟糕的编程选择.我知道数据库操作变得昂贵.
在我的情况下,我不是在阅读我自己的应用程序的app.config,而是另一个项目的,如下所示:
private string GetAppConfigValue(string key)
{
ExeConfigurationFileMap fileMap = new ExeConfigurationFileMap();
fileMap.ExeConfigFilename = GetConfigFilePath();
Configuration appConfig = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);
return appConfig.AppSettings.Settings[key].Value;
}
Run Code Online (Sandbox Code Playgroud)
场景:我有一个管理器类(只有一个这样的类),我必须从物理路径指定的配置文件中读取几个值(3到4),但很多次.需要我有几个成员变量来存储app.config文件中的值吗?什么是最好的方法.谢谢.
params在C#中可以有多个参数吗?像这样的东西:
void foobar(params int[] foo, params string[] bar)
Run Code Online (Sandbox Code Playgroud)
但我不确定这是否可行.如果是,编译器将如何决定在哪里拆分参数?
我目前正在尝试从中创建委托MethodInfo.我的总体目标是查看类中的方法并为标记有特定属性的方法创建委托.我正在尝试使用,CreateDelegate但我收到以下错误.
无法绑定到目标方法,因为其签名或安全透明性与委托类型的签名或安全透明性不兼容.
这是我的代码
public class TestClass
{
public delegate void TestDelagate(string test);
private List<TestDelagate> delagates = new List<TestDelagate>();
public TestClass()
{
foreach (MethodInfo method in this.GetType().GetMethods())
{
if (TestAttribute.IsTest(method))
{
TestDelegate newDelegate = (TestDelagate)Delegate.CreateDelegate(typeof(TestDelagate), method);
delegates.Add(newDelegate);
}
}
}
[Test]
public void TestFunction(string test)
{
}
}
public class TestAttribute : Attribute
{
public static bool IsTest(MemberInfo member)
{
bool isTestAttribute = false;
foreach (object attribute in member.GetCustomAttributes(true))
{
if (attribute is TestAttribute)
isTestAttribute = true; …Run Code Online (Sandbox Code Playgroud) 这是我直到今天才注意到的事情.显然,当执行基于相等的操作时Tuple<T>,很多使用的元组类(Tuple<T1, T2>等)的.NET实现会导致值类型的装箱惩罚.
以下是该类在框架中的实现方式(来自ILSpy的源代码):
public class Tuple<T1, T2> : IStructuralEquatable
{
public T1 Item1 { get; private set; }
public T2 Item2 { get; private set; }
public Tuple(T1 item1, T2 item2)
{
this.Item1 = item1;
this.Item2 = item2;
}
public override bool Equals(object obj)
{
return this.Equals(obj, EqualityComparer<object>.Default);
}
public override int GetHashCode()
{
return this.GetHashCode(EqualityComparer<object>.Default);
}
public bool Equals(object obj, IEqualityComparer comparer)
{
if (obj == null)
{
return false;
}
var tuple …Run Code Online (Sandbox Code Playgroud) 我遇到了以下代码:
var collection = new Collection<string>();
Run Code Online (Sandbox Code Playgroud)
我没有看到Collection类使用太多,也找不到太多关于它的用途的信息.查看.NET Framework源代码,它几乎只是List的包装器,因为它存储了List成员字段.它的构造函数如下:
public Collection()
{
this.items = (IList<T>) new List<T>();
}
Run Code Online (Sandbox Code Playgroud)
它还实现了IList.所以你可以将Collection声明为:
IList<string> collection = new Collection<string>();
Run Code Online (Sandbox Code Playgroud)
对我来说,在功能上等同于创建一个List:
IList<string> collection = new List<string>();
Run Code Online (Sandbox Code Playgroud)
那么你什么时候想在你自己的代码中使用它?我看到它是其他.NET集合的基类,但为什么它们将它作为公共具体(而不是内部和/或抽象)包含在内?
关于可能重复的注释 - 相关问题的答案似乎表明Collection类应该用作基类.我真正要求的不同之处是:
有人可以告诉我为什么编译器认为在以下代码break之后有必要yield return吗?
foreach (DesignerNode node in nodeProvider.GetNodes(span, node => node.NodeType != NDjango.Interfaces.NodeType.ParsingContext))
{
switch (node.ErrorMessage.Severity)
{
case -1:
case 0:
continue;
case 1:
yield return new TagSpan<ErrorTag>(node.SnapshotSpan, new ErrorTag(PredefinedErrorTypeNames.Warning));
break;
default:
yield return new TagSpan<ErrorTag>(node.SnapshotSpan, new ErrorTag(PredefinedErrorTypeNames.SyntaxError));
break;
}
}
Run Code Online (Sandbox Code Playgroud) 在网上搜索针对.NET的轻量级快速ORM时,我遇到了BLToolKit,它在性能,开放性,可维护性和灵活性方面看起来绝对令人惊叹.
但是,我也注意到它很少受到社区的喜爱,只有9个问题.任何人都可以对此有所了解吗?
人们只是推销他们自己的DAL,爱上Entity Framework,还是使用其他一些ORM?
是否有下限功能SortedList<K ,V>?该函数应返回等于或大于指定键的第一个元素.还有其他类支持这个吗?
伙计们 - 请再次阅读这个问题.如果它存在,我不需要返回键的函数.当没有确切的密钥匹配时,我对场景感兴趣.
我对O(log n)时间感兴趣.这意味着我没有foreach循环的问题,而是希望有一个有效的方法来做到这一点.
我对此做了一些测试.
Linq语句既不是编译器也不是运行时机器优化的,因此它们遍历所有集合元素并且速度慢O(n).根据Mehrdad Afshari的回答,这里是一个二进制搜索,它在Keys集合的O(log n)中工作:
public static int FindFirstIndexGreaterThanOrEqualTo<T>(
this IList<T> sortedCollection, T key
) where T : IComparable<T> {
int begin = 0;
int end = sortedCollection.Count;
while (end > begin) {
int index = (begin + end) / 2;
T el = sortedCollection[index];
if (el.CompareTo(key) >= 0)
end = index;
else
begin = index + 1;
}
return end;
}
Run Code Online (Sandbox Code Playgroud) c# ×8
.net ×4
linq ×2
performance ×2
app-config ×1
attributes ×1
bltoolkit ×1
boxing ×1
break ×1
coding-style ×1
collections ×1
delegates ×1
list ×1
methodinfo ×1
orm ×1
parameters ×1
params ×1
popularity ×1
sortedlist ×1
tuples ×1
yield-return ×1