C#学习笔记 操作符(中)

各类操作符的示例

操作符

  • 关键字 var
using System;

namespace OperatorsExample
{
    class Program
    {
        static void Main(string[] args)
        {
            var x = 100;
            //var声明隐式类型变量,推断数据类型
            Console.WriteLine(x.GetType().Name);
            var y = "100";
            //y = 100;          //不能将整型转化为字符串
        }
    }
}
  • new操作符
    new操作符在内存中创建类型的实例,并且立刻调用它的实例构造器。如果new左边有赋值符号,new操作符会把自己拿到的实例内存地址通过赋值操作符交给负责访问这个实例的变量。
    new操作符还可以调用实例的初始化器。可以立刻为实例的属性设置值,初始化器可以初始化多个属性
using System;
using System.Windows.Forms;

namespace OperatorsExample
{
    class Program
    {
        static void Main(string[] args)
        {
            Form myForm = new Form() { Text="Hello!"};  //初始化器
            //myForm.Text = "Hello!";
            myForm.ShowDialog();
        }
    }
}
using System;
using System.Windows.Forms;

namespace OperatorsExample
{
    class Program
    {
        static void Main(string[] args)
        {
            Form myForm = new Form() { Text="Hello!"};  //初始化器
            //myForm.Text = "Hello!";
            myForm.ShowDialog();
            var person = new { Name = "Mr.Okay", Age = 24 };    
            //匿名类型创建变量,用隐式类型变量来引用该实例
            Console.WriteLine(person.Name);
            Console.WriteLine(person.Age);
            Console.WriteLine(person.GetType().Name);
        }
    }
}
  • checked、unchecked操作符
using System;

namespace OperatorsExample
{
    class Program
    {
        static void Main(string[] args)
        {
            uint x = uint.MaxValue;
            //无符号整型 32比特位
            Console.WriteLine(x);
            string binStr = Convert.ToString(x, 2);
            Console.WriteLine(binStr);
            try
            {
                uint y = checked(x + 1);    //检查溢出
                //uint y = unchecked(x + 1);    //不检查溢出,输出为0
                Console.WriteLine(y);
            }
            catch (OverflowException ex)
            {
                Console.WriteLine("There's overflow!");
            }
        }
    }
}
  • checked、unchecked关键字
using System;

namespace OperatorsExample
{
    class Program
    {
        static void Main(string[] args)
        {
            uint x = uint.MaxValue;
            Console.WriteLine(x);
            string binStr = Convert.ToString(x, 2);
            Console.WriteLine(binStr);
            checked         //检查溢出,抛异常
            //unchecked     不检查溢出,输出为0
            {
                try
                {
                    uint y = x + 1;    //检查溢出
                    Console.WriteLine(y);
                }
                catch (OverflowException ex)
                {
                    Console.WriteLine("There's overflow!");
                }
            }
        }
    }
}

  • sizeof操作符
    默认情况下,只能用于去获取基本数据类型(结构体)的实例在内存当中所占的字节数。
using System;

namespace OperatorsExample
{
    class Program
    {
        static void Main(string[] args)
        {
            unsafe
            {
                int x = sizeof(Student);
                Console.WriteLine(x);
            }
        }
    }
    struct Student
    {
        int ID;
        long Scroe;
    }
}
  • ->操作符
    C#中指针操作、取地址操作以及用指针访问成员的操作,只能用来操作结构体类型,不能用来操作引用类型。
using System;

namespace OperatorsExample
{
    class Program
    {
        static void Main(string[] args)
        {
            unsafe
            {
                Student stu;
                stu.ID = 1;
                stu.Score = 97;
                Student* pStu = &stu;   //取stu地址,交给指针变量
                pStu->Score = 100;      //通过指针的间接访问
                Console.WriteLine(stu.Score);
            }
        }
    }
    struct Student
    {
        public int ID;
        public long Score;
    }
}
  • ~操作符
using System;

namespace OperatorsExample
{
    class Program
    {
        static void Main(string[] args)
        {
            int x = 12345678;
            int y = ~x;         //按位取反
            string xStr = Convert.ToString(x, 2).PadLeft(32, '0');
            string yStr = Convert.ToString(y, 2).PadLeft(32, '0');
            Console.WriteLine(xStr);
            Console.WriteLine(yStr);
        }
    }
}
  • !操作符
using System;

namespace OperatorsExample
{
    class Program
    {
        static void Main(string[] args)
        {
            bool b1 = true;
            bool b2 = !b1;      
            Console.WriteLine(b2);  //输出为false
        }
    }
}
  • ++x,- -x操作符
using System;

namespace OperatorsExample
{
    class Program
    {
        static void Main(string[] args)
        {
            int x1 = 100;
            int y1 = x1++;              //先赋值,在自增
            Console.WriteLine(x1);      //输出101
            Console.WriteLine(y1);      //输出100
            int x2 = 100;
            int y2 = ++x2;              //先自增,再赋值
            Console.WriteLine(x2);      //输出101
            Console.WriteLine(y2);      //输出101
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值