BCL中有几个地方可以使用IEqualityComparer.像Enumerable.Contains或Dictionary Constructor.如果我对默认的不满意,我可以提供我的比较器.
有时我想知道集合是否包含我所引用的那个对象.不是任何其他意义上被认为"相等"的那个.
问题是:BCL中是否存在仅依赖于ReferenceEquals方法的标准相等比较器?
我自己写的是这样的:
class ReferenceComparer<T> : IEqualityComparer<T> where T : class
{
private static ReferenceComparer<T> m_instance;
public static ReferenceComparer<T> Instance
{
get
{
return m_instance ?? (m_instance = new ReferenceComparer<T>());
}
}
public bool Equals(T x, T y)
{
return ReferenceEquals(x, y);
}
public int GetHashCode(T obj)
{
return RuntimeHelpers.GetHashCode(obj);
}
}
Run Code Online (Sandbox Code Playgroud)
我没有测试它彻底,也没有考虑很多的场景,但它似乎使Enumerable.Contains
和Dictionary
很高兴.
我有以下代码
var s1 = "ABzzzzz2";
var s2 = "äbzzzzz1";
var cmp = StringComparison.InvariantCultureIgnoreCase;
Console.WriteLine(string.Compare(s1, 0, s2, 0, 7, cmp)); //prints -1
Console.WriteLine(string.Compare(s1, 0, s2, 0, 8, cmp)); //prints 1
Run Code Online (Sandbox Code Playgroud)
怎么可能是第一个字符串的一部分小于第二个字符串的一部分,而整个第一个字符串大于整个第二个字符串?
我在x64,.net 2.0,3.5,4.0上测试过它
我已经通过C#在CLR中看到过,并且在代码项目文章" 幕后委托"中看到,当C#编译器看到这个时
public delegate void MyDelegate(int intValue);
Run Code Online (Sandbox Code Playgroud)
它实际上产生了这样的东西
class MyDelegate : System.MulticastDelegate
{
public virtual void Invoke(Int32 intValue);
...
}
Run Code Online (Sandbox Code Playgroud)
问题是,为什么Invoke方法是虚拟的?这个生成的委托类型可以继承吗?从CLR的角度来看,它可以.但为什么?为什么不生成密封类,因此在运行时不会有虚拟方法查找惩罚?
我发现我可以直接将DataGridView.DataSource设置为DataTable而不使用它们之间的BindingSource,这就是我到目前为止所看到的所有教程所使用的.那两个人之间的区别是什么?
我有好奇心的问题.
如何Int64
实现32位处理器?我可以看到三种选择
Int64
它是完全在CLR中实现的,它通过使用32位操作实现64位算术第一种情况看起来似乎不合理,因为它意味着64位处理器现在应该能够进行128位运算.第二个提出了一个如何有效地完成它的问题.第三种看起来想要统一两种方法的最差特征.
我正在尝试在代码隐藏中创建DataTemplate.我的DataTrigger存在问题.
这是DataTemplate,用xaml编写:
<DataTemplate x:Key="XamlTemplate" >
<TextBox Text="{Binding Name}" Name="element" Width="100"/>
<DataTemplate.Triggers>
<DataTrigger Binding="{Binding Flag}" Value="true">
<DataTrigger.EnterActions>
<BeginStoryboard>
<Storyboard>
<DoubleAnimation Storyboard.TargetName="element" Storyboard.TargetProperty="Width"
To="200" Duration="0:0:2" />
</Storyboard>
</BeginStoryboard>
</DataTrigger.EnterActions>
</DataTrigger>
</DataTemplate.Triggers>
</DataTemplate>
Run Code Online (Sandbox Code Playgroud)
这是我用c#写的
var template = new DataTemplate();
//create visual tree
var textFactory = new FrameworkElementFactory(typeof(TextBox));
textFactory.SetBinding(TextBox.TextProperty, new Binding("Name"));
textFactory.SetValue(TextBox.NameProperty, "element");
textFactory.SetValue(TextBox.WidthProperty, 100D);
template.VisualTree = textFactory;
//create trigger
var animation = new DoubleAnimation();
animation.To = 200;
animation.Duration = TimeSpan.FromSeconds(2);
Storyboard.SetTargetProperty(animation, new PropertyPath("Width"));
Storyboard.SetTargetName(animation, "element");
var storyboard = new …
Run Code Online (Sandbox Code Playgroud) 编辑解决F Ruffell的答案
我有以下xaml
<StackPanel>
<ListBox x:Name="_list1"/>
<ListBox x:Name="_list2"/>
</StackPanel>
Run Code Online (Sandbox Code Playgroud)
这个代码隐藏:
var ints = new[] { 1, 2, 3 };
_list1.ItemsSource = ints;
_list2.ItemsSource = ints;
_list1.Items.Filter = i => ((int)i) < 2;
Run Code Online (Sandbox Code Playgroud)
出于某种原因,在设置过滤器后,仅对第一个ListBox
列表进行过滤.我希望列表完全不同CollectionViews
,确实如此_list1.Items != _list2.Items
.同时设置过滤器到其中一个也以某种方式设置非常过滤到另一个.
现在的问题是,为什么和如何被CollectionViews
同步?
我在TabControl中遇到了WPF ListBox的问题.当我更改标签时,ListBox将其滚动条位置重置为0.这是repro代码:
<TabControl x:Name="_tabs">
<TabItem Header="1">
<ListBox ItemsSource="{Binding}" ScrollViewer.VerticalScrollBarVisibility="Auto"/>
</TabItem>
<TabItem Header="2">
<ListBox ItemsSource="{Binding}" ScrollViewer.VerticalScrollBarVisibility="Auto"/>
</TabItem>
</TabControl>
_tabs.DataContext = Enumerable.Range(1, 300).ToArray();
Run Code Online (Sandbox Code Playgroud)
当窗口打开时,我打开第二个选项卡,将列表滚动到中间位置,返回到第一个选项卡,然后再次打开第二个选项卡.由于某种原因,列表滚动到顶部.
为什么会这样?我犯了一些愚蠢的错误吗?
如何检查请求中是否存在Accept-Language标头?
我试过这个,但它产生了一个错误
<cfset requestData = GetHttpRequestData() >
<cfif IsDefined("requestData.Headers['Accept-Language']")>
...
Run Code Online (Sandbox Code Playgroud)
对不起,可能是蹩脚的问题.那是我的ColdFusion编码的第一天.
.net ×5
c# ×3
wpf ×3
comparison ×2
.net-4.0 ×1
32-bit ×1
32bit-64bit ×1
64-bit ×1
clr ×1
code-behind ×1
coldfusion ×1
coldfusion-7 ×1
datatemplate ×1
datatrigger ×1
delegates ×1
http-headers ×1
scroll ×1
string ×1
tabcontrol ×1
tabitem ×1