I'm trying to work out how .NET Code Contracts interact with the lock keyword, using the following example: public class TestClass { private object o1 = new object(); private object o2 = new object(); private void Test() { Contract.Requires(this.o1 != null); Contract.Requires(this.o2 != null); Contract.Ensures(this.o1 != null); lock (this.o2) { this.o1 = new o
我正在尝试使用以下示例来了解.NET代码合同如何与lock关键字进行交互: public class TestClass { private object o1 = new object(); private object o2 = new object(); private void Test() { Contract.Requires(this.o1 != null); Contract.Requires(this.o2 != null); Contract.Ensures(this.o1 != null); lock (this.o2) { this.o1 = new object(); } } } 当我运行Code Contract
I am trying to connect to a SQL server from outside the domain. I have the following connection string: SqlConnection("SERVER=Server;DATABASE=Database;User ID=DomainUser;Password=password"); When I try to use this outside the domain this will fail as it tries to log in using SQL authentication. Is there any way to pass a command into the string to use Active Directory authentication. What
我正尝试从域外连接到SQL服务器。 我有以下连接字符串: SqlConnection("SERVER=Server;DATABASE=Database;User ID=DomainUser;Password=password"); 当我尝试在域外使用时,它会尝试使用SQL身份验证登录时失败。 有什么方法可以将命令传递到字符串中以使用Active Directory身份验证。 还有什么其他方式可以连接到SQL服务器,使用域外的活动目录细节? 你或者需要: 在连接字符串中使用集成安全性,并在资源域(SQL
I have the following function: //Function to get random number public static int RandomNumber(int min, int max) { Random random = new Random(); return random.Next(min, max); } How I call it: byte[] mac = new byte[6]; for (int x = 0; x < 6; ++x) mac[x] = (byte)(Misc.RandomNumber((int)0xFFFF, (int)0xFFFFFF) % 256); If I step that loop with the debugger during runtime I get differ
我有以下功能: //Function to get random number public static int RandomNumber(int min, int max) { Random random = new Random(); return random.Next(min, max); } 我如何称呼它: byte[] mac = new byte[6]; for (int x = 0; x < 6; ++x) mac[x] = (byte)(Misc.RandomNumber((int)0xFFFF, (int)0xFFFFFF) % 256); 如果我在运行时使用调试器执行该循环,则会得到不同的值(这是我想要的)。 但是,如
When programming interfaces, I've found I'm doing a lot of casting or object type conversion. Is there a difference between these two methods of conversion? If so, is there a cost difference or how does this affect my program? public interface IMyInterface { void AMethod(); } public class MyClass : IMyInterface { public void AMethod() { //Do work } // Othe
在编程接口时,我发现我正在进行大量的转换或对象类型转换。 这两种转换方法有区别吗? 如果是这样,是否有成本差异,或者这对我的计划有什么影响? public interface IMyInterface { void AMethod(); } public class MyClass : IMyInterface { public void AMethod() { //Do work } // Other helper methods.... } public class Implementation { IMyInterface _MyObj; MyClass _myCl
I'm creating a function where I need to pass an object so that it can be modified by the function. What is the difference between: public void myFunction(ref MyClass someClass) and public void myFunction(out MyClass someClass) Which should I use and why? ref tells the compiler that the object is initialized before entering the function, while out tells the compiler that the object will
我正在创建一个函数,我需要传递一个对象以便它可以被该函数修改。 有什么区别: public void myFunction(ref MyClass someClass) 和 public void myFunction(out MyClass someClass) 我应该使用哪个,为什么? ref告诉编译器该对象在进入该函数之前被初始化,而out告诉编译器该对象将在该函数内被初始化。 因此,尽管ref是两路, out是出只。 ref修饰符意味着: 该值已经设置好了 该方法可以读取和修改它。 ou
Possible Duplicate: Casting vs using the 'as' keyword in the CLR What is actually the difference between these two casts? SomeClass sc = (SomeClass)SomeObject; SomeClass sc2 = SomeObject as SomeClass; Normally, they should both be explicit casts to the specified type? The former will throw an exception if the source type can't be cast to the target type. The latter will resul
可能重复: 投射vs在CLR中使用'as'关键字 这两个演员之间究竟有什么区别? SomeClass sc = (SomeClass)SomeObject; SomeClass sc2 = SomeObject as SomeClass; 通常情况下,他们都应该明确转换为指定的类型? 如果源类型不能转换为目标类型,前者将抛出异常。 后者将导致sc2为空引用,但也不例外。 [编辑] 我的原始答案肯定是最明显的差异,但正如Eric Lippert指出的那样,它不是唯一的答案。 其他差异包
I have seen people say that it is bad form to use catch with no arguments, especially if that catch doesn't do anything: StreamReader reader=new StreamReader("myfile.txt"); try { int i = 5 / 0; } catch // No args, so it will catch any exception {} reader.Close(); However, this is considered good form: StreamReader reader=new StreamReader("myfile.txt"); try { int i = 5 / 0; } finall
我曾经看到有人说,使用catch没有任何争论是不好的形式,特别是如果这种catch没有做任何事情: StreamReader reader=new StreamReader("myfile.txt"); try { int i = 5 / 0; } catch // No args, so it will catch any exception {} reader.Close(); 但是,这被认为是很好的形式: StreamReader reader=new StreamReader("myfile.txt"); try { int i = 5 / 0; } finally // Will execute despite any exception {
So I found this macro on SO: #define UNUSED(x) (void)(sizeof((x), 0)) and this (still) produces the following warning: main.c:11:36: warning: left-hand operand of comma expression has no effect [-Wunused-value] #define UNUSED(x) (void)(sizeof((x), 0)) Whereas the simpler version, a normal void cast: #define UNUSED(x) (void)(x) is warning-free. What could be the reason behind it? In gener
所以我在SO上发现了这个宏: #define UNUSED(x) (void)(sizeof((x), 0)) 这(仍然)会产生以下警告: main.c:11:36:warning:逗号表达式的左侧操作数没有作用[-Wunused-value] #define UNUSED(x)(void)(sizeof((x),0)) 鉴于更简单的版本,普通的void cast: #define UNUSED(x) (void)(x)是无警告的。 这背后的原因是什么? 一般而言,警告是高风险情况的一个标志。 这里给出的警告真的有用吗? 我对C
How does this C program work? main(_){_^448&&main(-~_);putchar(--_%64?32|-~7[__TIME__-_/8%8][">'txiZ^(~z?"-48]>>";;;====~$::199"[_*2&8|_/64]/(_&2?1:8)%8&1:10);} It compiles as it is (tested on gcc 4.6.3 ). It prints the time when compiled. On my system: !! !!!!!! !! !!!!!! !! !!!!!! !! !! !! !! !!
这个C程序如何工作? main(_){_^448&&main(-~_);putchar(--_%64?32|-~7[__TIME__-_/8%8][">'txiZ^(~z?"-48]>>";;;====~$::199"[_*2&8|_/64]/(_&2?1:8)%8&1:10);} 它编译原样(在gcc 4.6.3上测试)。 它打印编译时的时间。 在我的系统上: !! !!!!!! !! !!!!!! !! !!!!!! !! !! !! !! !! !! !! !! !! !! !!
For example, I recently came across this in the linux kernel: /* Force a compilation error if condition is true */ #define BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)])) So, in your code, if you have some structure which must be, say a multiple of 8 bytes in size, maybe because of some hardware constraints, you can do: BUILD_BUG_ON((sizeof(struct mystruct) % 8) != 0); and
例如,我最近在linux内核中遇到了这个问题: /* Force a compilation error if condition is true */ #define BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)])) 所以,在你的代码中,如果你有一些必须的结构,比如8字节的倍数,可能是因为硬件限制,你可以这样做: BUILD_BUG_ON((sizeof(struct mystruct) % 8) != 0); 除非struct mystruct的大小是8的倍数,并且如果它是8的倍数,则不会生成运行时代码