Tag: C#的

我怎样才能find一个List <T>中的特定元素?

我的应用程序使用这样的列表: List<MyClass> list = new List<MyClass>(); 使用Add方法, MyClass另一个实例被添加到列表中。 MyClass提供了以下方法: public void SetId(String Id); public String GetId(); 如何通过使用GetId方法findMyClass的特定实例? 我知道有Find方法,但我不知道这是否会在这里工作?!

为什么要检查这个!= null?

偶尔,我想花一些时间看.NET代码,看看事情是如何在幕后实现的。 我偶然发现了这个gem,同时通过Reflector查看String.Equals方法。 C# [ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)] public override bool Equals(object obj) { string strB = obj as string; if ((strB == null) && (this != null)) { return false; } return EqualsHelper(this, strB); } IL .method public hidebysig virtual instance bool Equals(object obj) cil managed { .custom instance void System.Runtime.ConstrainedExecution.ReliabilityContractAttribute::.ctor(valuetype System.Runtime.ConstrainedExecution.Consistency, valuetype System.Runtime.ConstrainedExecution.Cer) = { int32(3) […]

在WebBrowser控件中禁用JavaScript错误

我正在开发一个Web浏览器控件导航到SharePoint站点的Windows应用程序。 我的问题是,我得到JavaScript错误。 我怎样才能禁用JavaScript错误? 我不希望他们popup。

我在哪里可以得到一个“有用的”C ++二进制searchalgorithm?

我需要一个与C ++ STL容器兼容的二进制searchalgorithm,就像标准库的<algorithm>标题中的std::binary_search ,但是我需要它返回指向结果的迭代器,而不是一个简单的布尔值来告诉我如果元素存在。 (在旁注中,当他们为binary_search定义API时,标准委员会在想什么?) 我主要关心的是,我需要二进制search的速度,所以虽然我可以用其他algorithmfind数据,但是我想利用这个事实:我的数据被sorting以获得二进制的好处search,而不是一个线性search。 到目前为止,如果数据丢失, lower_bound和upper_bound失败: //lousy pseudo code vector(1,2,3,4,6,7,8,9,0) //notice no 5 iter = lower_bound_or_upper_bound(start,end,5) iter != 5 && iter !=end //not returning end as usual, instead it'll return 4 or 6 注意:只要与容器兼容,我也可以使用不属于std命名空间的algorithm。 就像boost::binary_search 。

在ASP.NET MVC中使用IEnumerable模型的自定义编辑器模板的正确,惯用的方法

这个问题是为什么我的DisplayFor循环通过我的IEnumerable <DateTime>的后续? 快速刷新。 什么时候: 该模型具有IEnumerable<T>types的属性 您使用只接受lambdaexpression式的重载将此属性传递给Html.EditorFor() 在Views / Shared / EditorTemplates下有一个types为T的编辑器模板 那么MVC引擎将自动为可枚举序列中的每个项目调用编辑器模板,生成结果列表。 例如,当有一个具有属性Lines的模型类Order : public class Order { public IEnumerable<OrderLine> Lines { get; set; } } public class OrderLine { public string Prop1 { get; set; } public int Prop2 { get; set; } } 并有一个视图/共享/ EditorTemplates / OrderLine.cshtml: @model TestEditorFor.Models.OrderLine @Html.EditorFor(m => m.Prop1) @Html.EditorFor(m […]

C#优雅的方式来检查属性的属性是否为空

在C#中,假设你想在这个例子中从PropertyC中取出一个值,并且ObjectA,PropertyA和PropertyB都可以为null。 ObjectA.PropertyA.PropertyB.PropertyC 如何以最less的代码安全地获取PropertyC? 现在我会检查: if(ObjectA != null && ObjectA.PropertyA !=null && ObjectA.PropertyA.PropertyB != null) { // safely pull off the value int value = objectA.PropertyA.PropertyB.PropertyC; } 这样做更好(伪代码)是很好的。 int value = ObjectA.PropertyA.PropertyB ? ObjectA.PropertyA.PropertyB : defaultVal; 可能甚至进一步崩溃与空合并运算符。 编辑本来我说我的第二个例子就像js,但我把它改为伪代码,因为它正确地指出,它不会在js中工作。

如何使用boost成员函数进行绑定

以下代码导致cl.exe崩溃(MS VS2005)。 我正在尝试使用boost绑定来创build一个调用myclass方法的函数: #include "stdafx.h" #include <boost/function.hpp> #include <boost/bind.hpp> #include <functional> class myclass { public: void fun1() { printf("fun1()\n"); } void fun2(int i) { printf("fun2(%d)\n", i); } void testit() { boost::function<void ()> f1( boost::bind( &myclass::fun1, this ) ); boost::function<void (int)> f2( boost::bind( &myclass::fun2, this ) ); //fails f1(); f2(111); } }; int main(int argc, char* […]

在C ++中使用C头文件时,我们应该使用std ::还是全局名称空间的函数?

C有点,不完全是C ++的一个子集。 所以我们可以通过改变名称( stdio.h到cstdio , stdlib.h到cstdlib )来使用C ++中的大部分C函数/头文件。 我的问题实际上是一种语义。 在C ++代码中(使用最新版本的GCC编译器),我可以调用printf("Hello world!);和std::printf("Hello world!); 它的工作原理完全一样。 而在引用我使用它也出现为std::printf("Hello world!); 我的问题是,最好是使用std::printf(); 在C ++? 有区别吗?

在WPF中使用超链接的示例

我见过几个build议,你可以通过Hyperlink控制添加到WPF应用程序的Hyperlink 。 以下是我想要在代码中使用它的方法: <Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" x:Class="BookmarkWizV2.InfoPanels.Windows.UrlProperties" Title="UrlProperties" Height="754" Width="576"> <Grid> <Grid.RowDefinitions> <RowDefinition></RowDefinition> <RowDefinition Height="40"/> </Grid.RowDefinitions> <Grid> <ScrollViewer ScrollViewer.VerticalScrollBarVisibility="Auto" Grid.RowSpan="2"> <StackPanel > <DockPanel LastChildFill="True" Margin="0,5"> <TextBlock Text="Url:" Margin="5" DockPanel.Dock="Left" VerticalAlignment="Center"/> <TextBox Width="Auto"> <Hyperlink NavigateUri="http://www.google.co.in"> Click here </Hyperlink> </TextBox> </DockPanel > </StackPanel> </ScrollViewer> </Grid> <StackPanel HorizontalAlignment="Right" Orientation="Horizontal" Margin="0,7,2,7" Grid.Row="1" > <Button Margin="0,0,10,0"> <TextBlock […]

静态扩展方法

可能重复: 我可以将扩展方法添加到现有的静态类吗? 有没有什么办法可以将静态扩展方法添加到类中。 特别是我想重载Boolean.Parse允许一个int参数。