c# &与&& 和 |与||的区别

&:按位与,对两个条件都进行判断测试

&&:逻辑与,只要一个条件知足,另一个条件就不会执行spa

同理:code

|:按位或,对两个条件都进行判断blog

||:逻辑或,只要一个条件知足,另一个条件就不会执行it

上代码:class

//&与&&的测试
 public static bool oneMethod()
        {
            Console.WriteLine("这是第一个方法");
            return false;
        }
        public static bool twoMethod()
        {
            Console.WriteLine("这是第二个方法");
            return false;
        }
//执行
 Console.WriteLine("&符号的执行结果");
            Console.WriteLine("输入这两个方法的结果{0}",oneMethod()&twoMethod());
            Console.WriteLine("******************************************");
            Console.WriteLine("&&符号的执行结果");
            Console.WriteLine("输入这两个方法的结果{0}", oneMethod() && twoMethod());

结果:方法

 

// |与||的测试
 public static bool oneMethod()
        {
            Console.WriteLine("这是第一个方法");
            return true;
        }
        public static bool twoMethod()
        {
            Console.WriteLine("这是第二个方法");
            return true;
        }

//调用
 Console.WriteLine("|符号的执行结果");
            Console.WriteLine("输入这两个方法的结果{0}",oneMethod() | twoMethod());
            Console.WriteLine("******************************************");
            Console.WriteLine("||符号的执行结果");
            Console.WriteLine("输入这两个方法的结果{0}", oneMethod() || twoMethod());

结果:im

相关文章
相关标签/搜索