【c#-应该在try / finally块内或之前mutex.WaitOne()】教程文章相关的互联网学习教程文章

c#使用try-catch捕获异常的最佳实践?【代码】

假设我需要运行methodA,而methodA将抛出FormatException. 如果我写这个块:try {methodA(); } catch (Exception ex) {methodB(); } catch (FormatException ex) {methodC(); }它是否会运行methodC,知道FormatException也是一个Exception,因此将进入methodB的catchblock. 或者这样写它更好:try {methodA(); } catch (Exception ex) {if(ex is FormatException){methodC();} else{methodB();} }解决方法:不,它不会运行methodC,但是如...

c# – Mono说’System.Net.Dns.GetHostEntry(string)’由于其保护级别而无法访问【代码】

我正在上课,其中一些例子在C#中.由于我的笔记本电脑运行Linux,我在Ubuntu上使用Mono 2.6.7. 我正在尝试编译以下代码:using System.Net.Sockets; using System.Net; using System;/// <summary> /// Example program showing simple TCP socket connections in C#.NET. /// Rather than reading and writing byte arrays, this example show /// how to use a stream reader in the client. /// TCPSocketServer is the socket ser...

忽略C#命令行程序中的try块【代码】

我在C#中有一个命令行程序,我用一个try-catch块包装,以防止它崩溃控制台.但是,当我调试它时,如果在DoStuff()方法的某处抛出异常,Visual Studio将在“catch”语句中中断.我希望Visual Studio能够打破异常发生的位置.最好的方法是什么? 评论试试?Visual Sudio中的一个设置?#if DEBUG语句?static void Main(string[] args) {try{DoStuff();}catch (Exception e){ //right now I have a breakpoint hereConsole.WriteLine(e.Messa...

c# – 使用Try-Catch处理看到的错误,是不是很糟糕?【代码】

我有这个代码块:try {int QuestionAnswerID = 0;// code block which assign value to QuestionAnswerID item.QuestionAnswerID = QuestionAnswerID; } catch (NullReferenceException) {item.QuestionAnswerID = -999; }这在一个循环中运行,这肯定会在循环内进入2-3次catch块.这段代码正是我想要的,但只是想知道使用try-catch块处理已知问题是不好的做法. 如果我在抛出偏差之前使用if语句/ s来识别空值,那会更有效吗?解决方法:是...

使用InvariantCulture的C#double.TryParse返回意外结果【代码】

我正在尝试使用NUnit对getprice方法进行单元测试.我坚持将rawprice解析为double.我的cultureinfo是en-US,但我把它设置为de-DE进行此测试.使用numberstyles.any和invariantculture进行双重解析会返回意外结果. rawprice cultureinfo是未知的,它可以是任何.它运行的服务器也是未知的,可以使用任何语言. 对于这个测试,我尝试使用德语作为原始价格和机器. 我尝试解析“9,42”但结果是942.[Test] [SetCulture("de-DE")] public void Get...

【转载】C#中float.TryParse方法和float.Parse方法的异同之处

在C#编程过程中,float.TryParse方法和float.Parse方法都可以将字符串string转换为单精度浮点类型float,但两者还是有区别,最重要的区别在于float.TryParse方法在字符串无法转换为float类型的情况下不会引发程序异常,而float.Parse方法则是直接抛出程序异常。float.TryParse方法在无法转换的情况下返回false,并且使用了out参数进行转换。 float.TryParse方法的签名为:static bool TryParse(string s, out Single result) float...

c# – Monitor.TryEnter / Monitor.Exit和SynchronizationLockException【代码】

是否有可能检测到同一个线程是否试图释放锁定?我们在代码中有很多地方看起来像:try {try{if(!Monitor.TryEnter(obj, 2000)) { throw new Exception("can not lock"); }}finally{Monitor.Exit(obj);} } catch {//Log }上面的代码非常简化,实际上Enter和Exit语句位于自定义对象(lock manager)中. 问题是,在该结构中,我们在尝试“退出”时遇到SynchronizationLockException,因为它看起来像是没有成功锁定的线程,试图最终释放. 所以问...

c# – 为什么TryParseExact在这个例子中不起作用?【代码】

我不能做这个工作 – 总是返回False. 我在这里想念什么?DateTimeOffset parsedDate;if ( DateTimeOffset.TryParseExact("2012-10-31 23:59:59", "YYYY-MM-dd HH:mm:ss", CultureInfo.InvariantCulture, DateTimeStyles.None, out parsedDate) == false) {throw new ArgumentException("dateToPare", dateToParse); }解决方法:根据MSDN,yyyy应该是低价的.

我是否应该在C#中使用try {}或using()?【代码】

我是C#的新手,直到我在Depth中找到了C#,直到我发现了C#,在Delphi中有一个try {}语句也在C#中实现. 但是,我看到你们中的一些人提到“使用(){}比尝试{}更好”. 这是一个例子://This might throw an exceptionsock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);sock.Connect(ip, port);//Try statementtry{sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);...

c# – 具有“out”参数的类似TryXXX的方法与返回可空值类型?【代码】

我经常在C#中看到这样的方法:(做一个可能给出或不给出结果的计算)bool TrySomething(SomeType inputData, out SomeOtherType result) { ... }为什么人们不使用这样的东西呢?Nullable<SomeOtherType> TrySomething(SomeType inputData) { ... }这只是性能差异吗?这是一个结构,所以必须没有堆分配,对吧?或者我错过了什么?解决方法:在C#2.0中引入了Nullable – 就像泛型一样.有很多代码早于此. 说实话,每当出现问题时,我都不会只...

c# – 将大文件添加到IO.Compression.ZipArchiveEntry会抛出OutOfMemoryException异常【代码】

我正在尝试使用以下代码将大型视频文件(~500MB)添加到ArchiveEntry:using (var zipFile = ZipFile.Open(outputZipFile, ZipArchiveMode.Update)) {var zipEntry = zipFile.CreateEntry("largeVideoFile.avi");using (var writer = new BinaryWriter(zipEntry.Open())){using (FileStream fs = File.Open(@"largeVideoFile.avi", FileMode.Open)){var buffer = new byte[16 * 1024];using (var data = new BinaryReader(fs)){int r...

C#string to number,当Parse失败时,TryParse返回true【代码】

我的数据库(SQL Server)中有一列将平均值存储为varchar. 值并不总是有效数字,它可以是:0,12,5.5,.4,4.0,6.,5.21428571428571428571428571428571428571,X,失败或只是空文本. 使用linq分配变量时,我想显示: >如果平均值是有效数字且> = 4则成功>如果平均值是有效数字且< 4>如果平均值不是有效数字,则为原始文本 代码 :myresults = myDataContext.myDbFunction() double note; from a in myresults select new MainReportModel() {...

c# – Dictionary.cs中的FindEntry函数【代码】

我一直在研究.NET的字典实现,因为我想了解是什么让字典ContainsKey和快速查找:http://referencesource.microsoft.com/#mscorlib/system/collections/generic/dictionary.cs,15debc34d286fdb3 ContainsKey函数基本上导致下面列出的FindEntry: buckets是一个整数数组,entry是一个Entry对象数组,它们是包含HashCode,TKey和TValue的结构体. 所以我理解这个查找很快,因为它是一个简单的数组查找.private int FindEntry(TKey key) {if(...

c# – 在catch和最后抛出异常. CLR行为与try-catch块【代码】

参见英文答案 > When is finally run if you throw an exception from the catch block? 7个我写了简单的C#控制台应用程序:class Mystery {static void Main(string[] args){MakeMess();}private static void MakeMess(){try{System.Console.WriteLine("try");throw new Exception(); // let's invoke catch}catch(Exception){System.Console.WriteLine("catch");throw new Exception("A");}f...

c# – IronPython调用TryGetMember而不是TryInvokeMember【代码】

我正在尝试将一个Dynamic对象交给Ironpython,但似乎Ironpython没有调用TryInvokeMember.相反,它调用TryGetMember并给出一个无法调用结果的错误. 我用IronPython 2.7和2.6.10920尝试过它 ExampleCode: DynamicObject:class ExampleDynamicObject: DynamicObject {public override bool TryGetMember(GetMemberBinder binder,out object result) {result = "TryGetMember";return true;}public override bool TryInvokeMember(Invo...

FINAL - 相关标签
TRY - 相关标签