• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
PowerCoder
博客园    首页    新随笔    联系   管理    订阅  订阅

c# &与&& 和 |与||的区别(转载)

&:按位与,对两个条件都进行判断
&&:逻辑与,只要前面的条件为false,后面的条件就不会执行


同理:
|:按位或,对两个条件都进行判断
||:逻辑或,只要前面的条件为true,后面的条件就不会执行


上代码:

//&与&&的测试
 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());

结果:

 

 

原文链接

 

posted @ 2019-06-11 10:31  PowerCoder  阅读(6008)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3