zoukankan      html  css  js  c++  java
  • c#的运算符

    c#的运算符和c的运算符的关系,是c包含于C#,因此C#有更多的运算符。
    先来学习一下三个是c中没用的:
    1、typeof()    返回 class 的类型。                                typeof(StreamReader);
    2、is    判断对象是否为某一类型。                              If( Ford is Car) // 检查 Ford 是否是 Car 类的一个对象。
    3、as    强制转换,即使转换失败也不会抛出异常。    Object obj = new StringReader("Hello");
                                                                                     StringReader r = obj as StringReader;

    实例

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    
    namespace OperatorsAppl
    {
        class Class1
        {
    
        }
    
        class Class2 : Class1 //类Class2是类Class1的子类
        {
    
        }
        class Rectangle
        {
            // 成员变量
            double length;
            double width;
            private const string bookName = "新华字典";
            public void Acceptdetails()//一个用来赋值的方法
            {
                length = 4.5;
                width = 3.5;
            }
            public double GetArea()//一个用来计算的方法
            {
                return length * width;
            }
            public void Display()//一个用来打印的方法
            {
                Console.WriteLine("Length: {0}", length);
                Console.WriteLine("Width: {0}", width);
                Console.WriteLine("Area: {0}", GetArea());//打印GetArea方法的计算结果
            }
        }
    
        class Program
        {
            static void Main(string[] args)
            {
    
                /* sizeof 运算符的实例 */
                Console.WriteLine("int 的大小是 {0}", sizeof(int));
                Console.WriteLine("short 的大小是 {0}", sizeof(short));
                Console.WriteLine("double 的大小是 {0}", sizeof(double));
    
                /* 三元运算符的实例 */
                int a, b;
                a = 10;
                b = (a == 1) ? 20 : 30;
                Console.WriteLine("b 的值是 {0}", b);
    
                b = (a == 10) ? 20 : 30;
                Console.WriteLine("b 的值是 {0}", b);
    
                if(b is int)
                {
                    Console.WriteLine("b is int");
                }
                Console.WriteLine("typeof(Program)={0}", typeof(Program));
                Console.WriteLine("typeof(Rectangle)={0}", typeof(Rectangle));
    
                Object rec = new Rectangle();
    
                Rectangle r2 = rec as Rectangle;
    
                r2.Acceptdetails();
                r2.GetArea();
                r2.Display();
    
                Console.ReadLine();
            }
        }
    }

    当上面的代码被编译和执行时,它会产生下列结果:

    int 的大小是 4
    short 的大小是 2
    double 的大小是 8
    b 的值是 30
    b 的值是 20
    b is int
    typeof(Program)=OperatorsAppl.Program
    typeof(Rectangle)=OperatorsAppl.Rectangle
    Length: 4.5
    Width: 3.5
    Area: 15.75


    C# 中的运算符优先级
    基本上和c类似,就是多了type如下所示:
    一元     + - ! ~ ++ - - (type)* & sizeof     从右到左 
     

  • 相关阅读:
    poj3278 Catch That Cow
    poj2251 Dungeon Master
    poj1321 棋盘问题
    poj3083 Children of the Candy Cor
    jvm基础知识—垃圾回收机制
    jvm基础知识1
    java面试基础必备
    java soket通信总结 bio nio aio的区别和总结
    java scoket aIO 通信
    java scoket Blocking 阻塞IO socket通信四
  • 原文地址:https://www.cnblogs.com/CodeWorkerLiMing/p/12007299.html
Copyright © 2011-2022 走看看