catch speeding up my code?

I wrote some code for testing the impact of try-catch, but seeing some surprising results. static void Main(string[] args) { Thread.CurrentThread.Priority = ThreadPriority.Highest; Process.GetCurrentProcess().PriorityClass = ProcessPriorityClass.RealTime; long start = 0, stop = 0, elapsed = 0; double avg = 0.0; long temp = Fibo(1); for (int i = 1; i < 100000000; i++

赶上加速我的代码?

我写了一些代码来测试try-catch的影响,但看到了一些令人惊讶的结果。 static void Main(string[] args) { Thread.CurrentThread.Priority = ThreadPriority.Highest; Process.GetCurrentProcess().PriorityClass = ProcessPriorityClass.RealTime; long start = 0, stop = 0, elapsed = 0; double avg = 0.0; long temp = Fibo(1); for (int i = 1; i < 100000000; i++) { start = S

Interop type cannot be embedded

I am creating a web application on the .NET 4.0 framework (beta2) in C#. When I try to use a assembly called "ActiveHomeScriptLib", I get the following error: Interop type 'ActiveHomeScriptLib.ActiveHomeClass' cannot be embedded. Use the applicable interface instead. When I change the framework to version 3.5, I don't have any errors. What is an Interop Type and why

Interop类型不能嵌入

我正在C#中的.NET 4.0框架(beta2)上创建一个Web应用程序。 当我尝试使用名为“ActiveHomeScriptLib”的程序集时,出现以下错误: 互操作类型'ActiveHomeScriptLib.ActiveHomeClass'不能嵌入。 改为使用适用的界面。 当我将框架更改为3.5版时,我没有任何错误。 什么是Interop类型?为什么只有在使用4.0框架时才会发生? .NET 4.0允许将主要的互操作程序集(或者说需要的部分)嵌入到程序集中,这样就不需要

byte + byte = int... why?

Looking at this C# code: byte x = 1; byte y = 2; byte z = x + y; // ERROR: Cannot implicitly convert type 'int' to 'byte' The result of any math performed on byte (or short ) types is implicitly cast back to an integer. The solution is to explicitly cast the result back to a byte: byte z = (byte)(x + y); // this works What I am wondering is why? Is it architectural? Philosophical? We hav

字节+字节=整数...为什么?

看看这个C#代码: byte x = 1; byte y = 2; byte z = x + y; // ERROR: Cannot implicitly convert type 'int' to 'byte' 在byte (或short )类型上执行的任何数学运算的结果都隐含地转换回整数。 解决方案是显式将结果转换回一个字节: byte z = (byte)(x + y); // this works 我想知道的是为什么? 它是建筑吗? 哲学? 我们有: int + int = int long + long = long float + float = float double + doubl

Create Generic method constraining T to an Enum

I'm building a function to extend the Enum.Parse concept that Allows a default value to be parsed in case that an Enum value is not found Is case insensitive So I wrote the following: public static T GetEnumFromString<T>(string value, T defaultValue) where T : Enum { if (string.IsNullOrEmpty(value)) return defaultValue; foreach (T item in Enum.GetValues(typeof(T))) {

创建通用方法约束T到一个枚举

我正在构建一个扩展Enum.Parse概念的函数 允许在未找到枚举值的情况下解析默认值 不区分大小写 所以我写了以下内容: public static T GetEnumFromString<T>(string value, T defaultValue) where T : Enum { if (string.IsNullOrEmpty(value)) return defaultValue; foreach (T item in Enum.GetValues(typeof(T))) { if (item.ToString().ToLower().Equals(value.Trim().ToLower())) return i

How do I get the path of the assembly the code is in?

Is there a way to get the path for the assembly in which the current code resides? I do not want the path of the calling assembly, just the one containing the code. Basically my unit test needs to read some xml test files which are located relative to the dll. I want the path to always resolve correctly regardless of whether the testing dll is run from TestDriven.NET, the MbUnit GUI or someth

我如何获得代码所在的程序集路径?

有没有办法获得当前代码所在的程序集的路径? 我不想要调用程序集的路径,只是包含代码的路径。 基本上我的单元测试需要读取一些相对于dll的xml测试文件。 无论测试dll是从TestDriven.NET,MbUnit GUI还是其他东西运行,我都希望路径始终正确解析。 编辑 :人们似乎误解了我的要求。 我的测试库位于说 C:项目 MyApplication的 daotests BIN 调试 daotests.dll 我想获得这个路径: C:项目 MyApplication的 daote

Reading settings from app.config or web.config in .net

I'm working on a C# class library that needs to be able to read settings from the web.config or app.config file (depending on whether the DLL is referenced from an ASP.NET web application or a Windows Forms application). I've found that ConfigurationSettings.AppSettings.Get("MySetting") works, but that code has been marked as deprecated by Microsoft. I've read that I should be u

从.net中的app.config或web.config中读取设置

我正在研究一个C#类库,它需要能够从web.config或app.config文件中读取设置(具体取决于DLL是从ASP.NET Web应用程序还是Windows窗体应用程序中引用的)。 我发现了 ConfigurationSettings.AppSettings.Get("MySetting") 工作,但该代码已被标记为Microsoft弃用。 我读过,我应该使用: ConfigurationManager.AppSettings["MySetting"] 但是, System.Configuration.ConfigurationManager类似乎从C#类库项目中不可用。

Static readonly vs const

I've read around about const and static readonly fields. We have some classes which contains only constant values. Used for various things around in our system. So I am wondering if my observation is correct: Should these kind of constant values always be static readonly for everything that is public? And only use const for internal/protected/private values? What do you recommend? Sh

静态只读与常量

我读过关于const和static readonly字段的内容。 我们有一些只包含常量值的类。 用于我们系统中的各种事物。 所以我想知道我的观察是否正确: 这些常量值是否应该是公开的static readonly ? 并且只使用const来表示内部/保护/私有值? 你有什么建议? 我应该甚至可能不使用static readonly字段,而是使用属性也许? 公共静态只读字段有点不寻常; 公共静态属性(只有一个get )会更普遍(也许由一个私有静态只读字段支

Best way to repeat a character in C#

What it's the best way to generate a string of t 's in C# I am learning C# and experimenting with different ways of saying the same thing. Tabs(uint t) is a function that returns a string with t amount of t 's For example Tabs(3) returns "ttt" Which of these three ways of implementing Tabs(uint numTabs) is best? Of course that depends on what "best" means

在C#中重复字符的最佳方法

在C#中生成t字符串的最佳方式是什么? 我正在学习C#并尝试用不同的方式说同一件事。 Tabs(uint t)是一个返回string t量为t的函数 例如, Tabs(3)返回"ttt" 实现Tabs(uint numTabs)的这三种方法中哪一种最好? 当然,这取决于“最好”的含义。 LINQ版本只有两行,这很好。 但是,呼吁重复和聚合不必要的时间/资源消耗? StringBuilder版本非常清晰,但是StringBuilder类以某种方式更慢? string版本是

Should 'using' directives be inside or outside the namespace?

I have been running StyleCop over some C# code, and it keeps reporting that my using directives should be inside the namespace. Is there a technical reason for putting the using directives inside instead of outside the namespace? There is actually a (subtle) difference between the two. Imagine you have the following code in File1.cs: // File1.cs using System; namespace Outer.Inner { cla

“使用”指令应该位于命名空间的内部还是外部?

我一直在通过一些C#代码运行StyleCop,并且不断报告我的using指令应该位于命名空间内。 将using指令放在里面而不是命名空间之外是否有技术原因? 两者之间实际存在着(微妙的)区别。 想象一下你在File1.cs中有以下代码: // File1.cs using System; namespace Outer.Inner { class Foo { static void Bar() { double d = Math.PI; } } } 现在想象一下,有人将另一个文

C#.Net case

Why does C#.Net allow the declaration of the string object to be case-insensitive? String sHello = "Hello"; string sHello = "Hello"; Both the lower-case and upper-case S of the word String are acceptable and this seems to be the only object that allows this. Can anyone explain why? string is a language keyword while System.String is the type it aliases. Both compile to exactly the same th

C#.Net案例

为什么C#.Net允许字符串对象的声明不区分大小写? String sHello = "Hello"; string sHello = "Hello"; 单词String的小写字母和大写字母S都可以接受,并且这似乎是唯一允许这样做的对象。 谁能解释为什么? string是一个语言关键字,而System.String是它的别名类型。 两者都编译成完全相同的东西,类似地: int是System.Int32 long是System.Int64 float是System.Single double是System.Double char是System.C