从MSDN上的Dictionary.TryGetValue方法入口:
此方法结合了ContainsKey方法和Item属性的功能.
如果未找到密钥,则value参数将获取值类型TValue的相应默认值; 例如,0(零)表示整数类型,false表示布尔类型,null表示引用类型.
如果您的代码经常尝试访问不在字典中的键,请使用TryGetValue方法.使用此方法比捕获Item属性抛出的KeyNotFoundException更有效.
该方法接近O(1)操作.
从描述中,不清楚它是否比调用ContainsKey然后进行查找更有效或更方便.TryGetValue通过执行单个查找,只调用ContainsKey然后调用Item或实际上是否更有效?
换句话说,什么是更有效(即哪一个执行更少的查找):
Dictionary<int,int> dict;
//...//
int ival;
if(dict.ContainsKey(ikey))
{
ival = dict[ikey];
}
else
{
ival = default(int);
}
Run Code Online (Sandbox Code Playgroud)
要么
Dictionary<int,int> dict;
//...//
int ival;
dict.TryGetValue(ikey, out ival);
Run Code Online (Sandbox Code Playgroud)
注意:我不是在寻找基准!
我编写了一个自定义序列化程序,通过反射设置对象属性.可序列化类用serializable属性标记,所有可序列化属性也被标记.例如,以下类是可序列化的:
[Serializable]
public class Foo
{
[SerializableProperty]
public string SomethingSerializable {get; set;}
public string SometthingNotSerializable {get; set;}
}
Run Code Online (Sandbox Code Playgroud)
当系列化程序被要求反序列化时SomethingSerializable,它获取属性的set方法并使用它来设置它,如下所示:
PropertyInfo propertyInfo; //the property info of the property to set
//...//
if (propertyInfo.CanWrite && propertyInfo.GetSetMethod() != null)
{
propertyInfo.GetSetMethod().Invoke(obj, new object[]{val});
}
Run Code Online (Sandbox Code Playgroud)
这工作正常,但是,如何才能使属性设置器只对序列化器可访问?如果setter是私有的:
public string SomethingSerializable {get; private set;}
Run Code Online (Sandbox Code Playgroud)
然后调用propertyInfo.GetSetMethod()在序列化器中返回null.有没有办法访问私有setter或任何其他方式,以确保只有序列化程序可以访问setter?不保证序列化程序在同一个程序集中.
有没有人知道是否有特定原因或设计决定不在C#中包含反向调查员?如果有一个与C++ reverse_iterator相同的东西,就像Enumerator相当于C++ 那样会很好iterator.可以反向迭代的集合只会实现像IReverseEnumerable这样的东西,可以做类似的事情:
List<int>.ReverseEnumerator ritr = collection.GetReverseEnumerator();
while(rtir.MoveNext())
{
// do stuff
}
Run Code Online (Sandbox Code Playgroud)
这样,您就可以以相同的方式迭代Lists和LinkedLists,而不是使用索引器为另一个链接和之前的链接,从而实现更好的抽象
[编辑:我道歉......原来的问题措辞含糊不清,我没有收到我要找的回复]
对于从类Y继承的任何类X,new List<X>() is IEnumerable<Y>都是true.但是,这不适用于结构:new List<int>() is IEnumerable<ValueType>是错误的.我的问题是:为什么?
这是一个示例程序:
class Program
{
class Y { }
class X : Y { }
struct Z { }
static void Main(string[] args)
{
Test(new List<X>());
Test(new List<string>());
Test(new List<Z>());
Test(new List<int>());
Test("blah");
Test(1);
Console.ReadLine();
}
static void Test(object o)
{
if (o is IEnumerable<Y>)
{
Console.WriteLine(o + " is a list of Ys");
}
else if (o is IEnumerable<ValueType>)
{
Console.WriteLine(o + " is a list of ValueTypes"); …Run Code Online (Sandbox Code Playgroud) 我有一个ComboBox,其属性ItemsSource和SelectedValue绑定到模型.有时,模型需要将所选项目调整为不同的项目,但是当我在模型中执行时,模型值不会反映在视图中,即使正确设置了SelectedValue(使用snoop和SelectionChanged进行检查)事件处理程序).
为了说明问题,这里有一个简单的xaml:
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525" DataContext="{Binding RelativeSource={RelativeSource Self}}">
<Grid>
<ComboBox Height="25" Width="120" SelectedValue="{Binding SelectedValue}" SelectedValuePath="Key" ItemsSource="{Binding PossibleValues}" DisplayMemberPath="Value"/>
</Grid>
</Window>
Run Code Online (Sandbox Code Playgroud)
这是模型:
using System.Collections.Generic;
using System.Windows;
using System.ComponentModel;
namespace WpfApplication1
{
public partial class MainWindow : Window, INotifyPropertyChanged
{
int m_selectedValue = 2;
Dictionary<int, string> m_possibleValues = new Dictionary<int, string>() { { 1, "one" }, { 2, "two" }, { 3, "three" }, {4,"four"} };
public int SelectedValue
{
get { return m_selectedValue; }
set
{
if …Run Code Online (Sandbox Code Playgroud) 确保在任何 gem 构建的构建节点上安装依赖项的最佳实践是什么?
bundle exec如果尚未安装某些依赖项,则会失败。
我应该做什么
bundle install
bundle exec rake
Run Code Online (Sandbox Code Playgroud)
在每个构建上?
或者,还有更好的方法?
我有一个WPF应用程序,每当我的应用程序遇到未处理的异常以帮助调试时,我需要编写一个小型转储文件.但是,每次调用异常处理程序时,堆栈都完全展开到处理程序,并且没有可用于转储文件的有用状态.
我试图订阅这两个并且堆栈已经解开了两个:
Application.Current.DispatcherUnhandledException
AppDomain.CurrentDomain.UnhandledException
Run Code Online (Sandbox Code Playgroud)
我尝试使用控制台应用程序,并且堆栈没有解开,因此它肯定与WPF相关.异常和处理程序都发生在主线程中.
这里有两个代码示例,您可以轻松地看到这一点.只需在每个处理程序中设置断点,并在遇到断点时观察调用堆栈.
控制台应用:
class Program
{
static void Main(string[] args)
{
AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
int foo = 5;
++foo;
throw new ApplicationException("blah");
++foo;
}
static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
Console.WriteLine("blah");
}
}
Run Code Online (Sandbox Code Playgroud)
WPF应用:
public partial class App : Application
{
protected override void OnStartup(StartupEventArgs e)
{
Application.Current.DispatcherUnhandledException += new System.Windows.Threading.DispatcherUnhandledExceptionEventHandler(Current_DispatcherUnhandledException);
AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
int foo = 5;
++foo;
throw new ApplicationException("blah");
++foo;
base.OnStartup(e);
}
void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e) {}
void Current_DispatcherUnhandledException(object …Run Code Online (Sandbox Code Playgroud) 我有一个带有AutoIncrement列的数据表.如何在不添加新行的情况下获取列的下一个增量(-1,-2,-3,...)?
c# ×6
.net ×3
wpf ×2
bundler ×1
data-binding ×1
dictionary ×1
enumerator ×1
exception ×1
performance ×1
reflection ×1
ruby ×1
xaml ×1