是否有下限功能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) 如何检查System.ValueTuple是否为默认值?粗略的例子:
(string foo, string bar) MyMethod() => default;
// Later
var result = MyMethod();
if (result is default){ } // doesnt work
Run Code Online (Sandbox Code Playgroud)
我可以MyMethod使用defaultC#7.2的语法返回默认值.我无法检查默认情况?这些是我试过的:
result is default
result == default
result is default(string, string)
result == default(string, string)
Run Code Online (Sandbox Code Playgroud) 我的字典:
Dictionary<double, string> dic = new Dictionary<double, string>();
Run Code Online (Sandbox Code Playgroud)
如何返回字典中的最后一个元素?
我一直在为项目创建对象,有些实例我必须为这些对象创建一个深层副本我已经想到了使用C#的内置函数,即MemberwiseClone().困扰我的问题是每当我创建一个新类时,我都必须编写一个类似下面代码的函数来进行浅拷贝.有人请帮助我改进这部分并给我一个更好的浅拷贝比第二行代码.谢谢 :)
SHALLOW COPY:
public static RoomType CreateTwin(RoomType roomType)
{
return (roomType.MemberwiseClone() as RoomType);
}
Run Code Online (Sandbox Code Playgroud)
深度复制:
public static T CreateDeepClone<T>(T source)
{
if (!typeof(T).IsSerializable)
{
throw new ArgumentException("The type must be serializable.", "source");
}
if (Object.ReferenceEquals(source, null))
{
return default(T);
}
IFormatter formatter = new BinaryFormatter();
Stream stream = new MemoryStream();
using (stream)
{
formatter.Serialize(stream, source);
stream.Seek(0, SeekOrigin.Begin);
return (T)formatter.Deserialize(stream);
}
}
Run Code Online (Sandbox Code Playgroud) 我有一个非模态子表单,从父表单打开.我需要将子表单居中到其父表单.我已经设置了子表单的属性CenterParent并尝试了这个:
Form2 f = new Form2();
f.Show(this);
Run Code Online (Sandbox Code Playgroud)
但无济于事.这适用于模态形式,但非模态形式则不然.任何简单的解决方案,还是需要我通过所有数学计算来确定其位置为中心?
冒着成为村里白痴的风险,有人可以向我解释为什么仿制药被称为仿制药吗?我理解它们的用法和好处,但如果泛型的定义是"通用的"而泛型集合是类型安全的,那么为什么这不是用词不当呢?
例如,ArrayList可以保存任何对象:
ArrayList myObjects = new ArrayList();
myObjects.Add("one");
myObjects.Add(1);
Run Code Online (Sandbox Code Playgroud)
而字符串类型的泛型集合只能包含字符串:
var myStrings = new List<string>();
myStrings.Add("one");
myStrings.Add("1");
Run Code Online (Sandbox Code Playgroud)
我不清楚为什么它被称为"通用".如果答案是"......这使得设计类和方法可以推迟一个或多个类型的规范,直到通过客户端代码声明和实例化类或方法." 从这里开始,我认为这是有道理的.也许我有这种精神失误,因为我只是在Java引入泛型之后开始编程,所以我不记得他们之前的时间.但还是......
任何帮助表示赞赏.
我正在使用StringDictionary集合来收集Key Value Pairs.
例如:
StringDictionary KeyValue = new StringDictionary();
KeyValue.Add("A", "Load");
KeyValue.Add("C", "Save");
Run Code Online (Sandbox Code Playgroud)
在检索过程中,我必须形成两个foreach来获取键和值(即)
foreach(string key in KeyValue.Values)
{
...
}
foreach(string key in KeyValue.Keys)
{
...
}
Run Code Online (Sandbox Code Playgroud)
有没有办法让这对搭配单身foreach?
对于属性有GetGetMethod,GetSetMethod所以我可以这样做:
Getter = (Func<S, T>)Delegate.CreateDelegate(typeof(Func<S, T>),
propertyInfo.GetGetMethod());
Run Code Online (Sandbox Code Playgroud)
和
Setter = (Action<S, T>)Delegate.CreateDelegate(typeof(Action<S, T>),
propertyInfo.GetSetMethod());
Run Code Online (Sandbox Code Playgroud)
但我怎么去FieldInfos?
我不是在寻找代表GetValue和SetValue(这意味着我每次都会调用反射)
Getter = s => (T)fieldInfo.GetValue(s);
Setter = (s, t) => (T)fieldInfo.SetValue(s, t);
Run Code Online (Sandbox Code Playgroud)
但如果这里有CreateDelegate办法吗?我的意思是,因为赋值返回一个值,我可以将赋值视为一种方法吗?如果有的话有MethodInfo手柄吗?换句话说,我如何传递MethodInfo设置权并从成员字段获取值到CreateDelegate方法,以便我得到一个委托,我可以直接读取和写入字段?
Getter = (Func<S, T>)Delegate.CreateDelegate(typeof(Func<S, T>), fieldInfo.??);
Setter = (Action<S, T>)Delegate.CreateDelegate(typeof(Action<S, T>), fieldInfo.??);
Run Code Online (Sandbox Code Playgroud)
我可以构建表达式并编译它,但我正在寻找更简单的东西.最后,如果问题没有答案,我不介意去表达路线,如下所示:
var instExp = Expression.Parameter(typeof(S));
var fieldExp = Expression.Field(instExp, fieldInfo);
Getter = Expression.Lambda<Func<S, T>>(fieldExp, instExp).Compile(); …Run Code Online (Sandbox Code Playgroud) 这是对C#Sort和OrderBy这个优秀问题的跟进比较.我将使用相同的示例:
List<Person> persons = new List<Person>();
persons.Add(new Person("P005", "Janson"));
persons.Add(new Person("P002", "Aravind"));
persons.Add(new Person("P007", "Kazhal"));
Run Code Online (Sandbox Code Playgroud)
争论的方法是:
persons.Sort((p1, p2) => string.Compare(p1.Name, p2.Name, true));
//and
persons.OrderBy(n => n.Name);
Run Code Online (Sandbox Code Playgroud)
首先让我说,我理解没有任何重大的性能差异需要担心.但我很想知道为什么OrderBy表现得比这更好Sort.我正在使用@phoog在原始问题中发布的答案.
private void button1_Click(object sender, EventArgs e)
{
IEnumerable<Person> people;
BenchMark(persons => persons.Sort((p1, p2) => string.Compare(p1.Name, p2.Name, true)));
BenchMark(persons => people = persons.OrderBy(n => n.Name));
}
private static Random randomSeed = new Random();
public static string RandomString(int size, bool lowerCase)
{
var sb = new StringBuilder(size); …Run Code Online (Sandbox Code Playgroud) class Bla
{
public readonly int sum;
}
FieldInfo f = type.GetField("sum");
f.?? // what?
Run Code Online (Sandbox Code Playgroud)
如何查找是否sum只读?对于属性,我可以PropertyInfo.CanWrite查找成员是否具有写访问权限.
c# ×9
.net ×5
collections ×2
fieldinfo ×2
c#-7.2 ×1
centering ×1
cloning ×1
delegates ×1
dictionary ×1
forms ×1
generics ×1
linq ×1
oop ×1
parent-child ×1
readonly ×1
reflection ×1
semantics ×1
setvalue ×1
sortedlist ×1
sorting ×1
valuetuple ×1
winforms ×1