Tag: .net

哪个方法执行得更好:.Any()vs .Count()> 0?

在System.Linq命名空间中,我们现在可以扩展IEnumerable的Any()和Count() 扩展方法 。 我最近被告知,如果我想检查一个集合包含1个或多个项目,我应该使用.Count() > 0扩展方法,而不是.Count() > 0扩展方法,因为.Count()扩展方法必须遍历所有的项目。 其次,一些集合有一个属性 (不是扩展方法),即Count或Length 。 使用这些,而不是.Count()或.Count()会更好吗? 是/娜?

你能解释STA和MTA吗?

你能用你自己的话来解释STA和MTA吗? 另外,什么是公寓线程,只涉及到COM? 如果是这样,为什么?

什么是“索引超出范围”exception,我该如何解决?

我收到以下错误之一: “索引超出范围,必须是非负的,小于收集的大小” “插入指数超出范围,必须是非负值,小于等于尺寸。” “指数数组的边界之外。” 这是什么意思,我该如何解决? 也可以看看 IndexOutOfRangeException ArgumentOutOfRangeException

WinForms中的水印文本框

任何人都可以指向我一个基本的Windows窗体文本框的一个很好的实现,最初将显示光标进入时消失的水印文本? 我想我可以创build自己的一些创造性的使用进入和离开事件,但我相信有一个完全可用的实现坐在某处。 我看到了WPF的实现,如果有必要,我可以嵌套它,但本地的WinForms TextBox衍生物会更好。 我到目前为止, 还没有尝试过,但没有人看到任何明显的问题? public class WatermarkTextBox:TextBox { public string WatermarkText { get; set; } public Color WatermarkColor { get; set; } private Color TextColor { get; set; } private bool isInTransition; public WatermarkTextBox() { WatermarkColor = SystemColors.GrayText; } private bool HasText { get { return Text.IsNotNullOrBlankOr(WatermarkText); }} protected override void OnEnter(EventArgs e) […]

如何将字节转换为string?

我有一个从我刚刚知道包含UTF-8的文件加载的byte[]数组。 在一些debugging代码中,我需要将其转换为string。 有没有一个class轮可以做到这一点? 在封面之下它应该只是一个分配和一个memcopy ,所以即使它没有被执行,也应该是可能的。

回发或callback参数无效。 事件validation使用'<pages enableEventValidation =“true”/>'

当我从客户端回发一页时,出现以下错误。 我有JavaScript代码,修改客户端的asp:ListBox。 我们如何解决这个问题? 以下错误详情: Server Error in '/XXX' Application. ——————————————————————————– Invalid postback or callback argument. Event validation is enabled using <pages enableEventValidation="true"/> in configuration or <%@ Page EnableEventValidation="true" %> in a page. For security purposes, this feature verifies that arguments to postback or callback events originate from the server control that originally rendered them. If […]

如何在.NET中格式化string中的花括号(大括号)

如何使用string.Format方括号。 例如: String val = "1,2,3" String.Format(" foo {{0}}", val); 这个例子不会引发exception,而是输出stringfoo {0} 有没有办法摆脱括号?

TransactionScope在一些机器上自动升级到MSDTC?

在我们的项目中,我们使用TransactionScope来确保我们的数据访问层在事务中执行它的操作。 我们的目标是不要求我们的terminal用户的机器上启用MSDTC服务。 麻烦的是,在我们的一半开发人员机器上,我们可以禁用MSDTC来运行。 另一半必须启用它或他们得到“MSDTC on [服务器]不可用”错误消息。 这真让我挠头,让我认真考虑回滚到基于ADO.NET事务对象的基于TransactionScope的解决scheme。 这看起来很疯狂 – 对我们开发人员的一半工作(而不是升级)相同的代码在其他开发人员的升级。 我希望有一个更好的答案跟踪为什么交易升级到DTC,但不幸的是它不。 下面是一些会导致麻烦的代码示例,在尝试升级的机器上,尝试在第二个连接上升级。打开()(是的,当时没有其他连接打开)。 using (TransactionScope transactionScope = new TransactionScope() { using (SqlConnection connection = new SqlConnection(_ConStr)) { using (SqlCommand command = connection.CreateCommand()) { // prep the command connection.Open(); using (SqlDataReader reader = command.ExecuteReader()) { // use the reader connection.Close(); } } } // Do other stuff […]

公共,私人,受保护和没有什么区别?

我所有的大学生涯中,我一直在使用public ,想知道public , private和protected之间的区别吗? 还有什么static做,而不是什么都没有?

在C#中,为什么List <string>对象不能存储在List <object>variables中

看来List对象不能存储在C#中的Listvariables中,甚至不能以这种方式显式地转换。 List<string> sl = new List<string>(); List<object> ol; ol = sl; 结果不能将typesSystem.Collections.Generic.List<string>隐式转换为System.Collections.Generic.List<object> 接着… List<string> sl = new List<string>(); List<object> ol; ol = (List<object>)sl; 结果无法将typesSystem.Collections.Generic.List<string>转换为System.Collections.Generic.List<object> 当然,你可以把所有的东西都从string列表中拉出来放回去,但这是一个相当复杂的解决scheme。