c# |操作符(二)

一、new操作符

在内存当中创造一个类型的实例,并且调用这个实例的实构造器

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;


namespace ConsoleApplication7
{
    class Program
    {
        static void Main(string[] args)
        {
            Form myForm = new Form() { Text = "hello",FormBorderStyle = FormBorderStyle.SizableToolWindow};//在后面加括号,可以设定属性的值
            myForm.ShowDialog();

        }


    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;


namespace ConsoleApplication7
{
    class Program
    {
        static void Main(string[] args)
        {
            var Person = new { Name = "Mr.OK", Age = 34 };
            Console.WriteLine(Person.Name);
            Console.WriteLine(Person.Age);
            Console.WriteLine(Person.GetType().Name);

        }


    }
}

结果:
在这里插入图片描述

一个继承的例子

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;


namespace ConsoleApplication7
{
    class Program
    {
        static void Main(string[] args)
        {
            Student stu = new Student();
            stu.Report();
            CsStludent csStu = new CsStludent();
            csStu.Report();
        }

        class Student
        {
            public void Report()
            {
                Console.WriteLine("I'm a student");
            }           
        }

        class CsStludent: Student   //继承父类
        {
            new public void Report()
            {
                Console.WriteLine("I'm a teacher,not a student.");//子类把父类的方法给隐藏掉了
            }
        }
    }
}

结果:
在这里插入图片描述
这段程序中

class Csstludent:student

是继承的意思,但子类的方法可以改变(覆盖)。

二、checked,unchecked

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;


namespace ConsoleApplication7
{
    class Program
    {
        static void Main(string[] args)
        {
            uint x = uint.MaxValue;
            Console.WriteLine(x);
            string binStr = Convert.ToString(x, 2);
            Console.WriteLine(binStr);
            //uint y = x + 1;
            //Console.WriteLine(y);//这里y溢出了,所以y的结果是0
            /*==========================================================================*/
            try
            {
                uint y = checked(x + 1);
                Console.WriteLine(y);
            }
            catch (OverflowException)
            {

                Console.WriteLine("There is a overflow!");
            }
        }

        
    }
}

结果:
在这里插入图片描述
checked就是检测到溢出就报警,unchecked反之

三、delegate

把delegate当做操作符使用(比较少用),不会懒学

四、sizeof

默认只能获取基本数据类型的字节数

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication8
{
    class Program
    {
        static void Main(string[] args)
        {
            int x = sizeof(decimal);
            Console.WriteLine(x);
            unsafe      //sizeof可以在unsafe中获取自定义的结构体类型尺寸
            {
                int y = sizeof(Student);
                Console.WriteLine(y);
            }
        }
    }

    struct Student
    {
        int ID;
        long Score;
    }
}

答案是16,16,至于为什么自定义类型Student是16而不是12,不细究

五、->

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication8
{
    class Program
    {
        static void Main(string[] args)
        {
            unsafe
            {
                Student stu;
                stu.ID = 1;
                stu.Score = 99;
                Student* pStu = &stu;
                pStu->Score = 100;
                Console.WriteLine(stu.Score);
            }
        }
    }

    struct Student
    {
        public int ID;
        public long Score;
    }
}

基本操作符到此结束,下面是一元操作符

六、*和&

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication8
{
    class Program
    {
        static void Main(string[] args)
        {
            unsafe
            {
                Student stu;
                stu.ID = 1;
                stu.Score = 99;
                Student* pStu = &stu;
                pStu->Score = 100;
                (*pStu).Score = 1000;
                Console.WriteLine(stu.Score);
            }
        }
    }

    struct Student
    {
        public int ID;
        public long Score;
    }
}

七、+,-,++,–,~,!

略,小心造成溢出
~:在二进制进行取反

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication8
{
    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(y);
            Console.WriteLine(xStr);
            Console.WriteLine(yStr);
        }
    }
}

在这里插入图片描述
!:取反,只能操作bool类型的

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication8
{
    class Program
    {
        static void Main(string[] args)
        {
            Student stu = new Student(null);
            Console.WriteLine(stu.Name);
        }

        
    }

    class Student
    {
        public Student(string initName)
        {
            if (!string.IsNullOrEmpty(initName))
            {
                this.Name = initName;
            }
            else
            {
                throw new ArgumentException("initName cannot be null or empty");
            }
        }
            public string Name;
    }
    
}
        
    


在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值