zoukankan      html  css  js  c++  java
  • C#关键字的使用

    params object[] 用于函数多参数的定义
    public static void Write(string format, params object[] arg);
     
    explicit 关键字用于声明必须使用强制转换来调用的用户定义的类型转换运算符。 例如,在下面的示例中,此运算符将名为 Fahrenheit 的类转换为名为 Celsius 的类:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    
    namespace EventDemo
    {
        class Celsius
        {
            private float degrees;
            public Celsius(float temp)
            {
                degrees = temp;
            }
            public static explicit operator Fahrenheit(Celsius c)
            {
                return new Fahrenheit((9.0f / 5.0f) * c.degrees + 32);
            }
            public float Degrees
            {
                get { return degrees; }
            }
            
        }
    
        class Fahrenheit
        {
            private float degrees;
            public Fahrenheit(float temp)
            {
                degrees = temp;
            }
            // Must be defined inside a class called Fahrenheit:
            public static explicit operator Celsius(Fahrenheit fahr)
            {
                return new Celsius((5.0f / 9.0f) * (fahr.degrees - 32));
            }
            public float Degrees
            {
                get { return degrees; }
            }
            
        }
    
        class Program
        {
            static void Main()
            {
                Fahrenheit fahr = new Fahrenheit(100.0f);
                Console.Write("{0} Fahrenheit", fahr.Degrees);
                Celsius c = (Celsius)fahr;
    
                Console.Write(" = {0} Celsius", c.Degrees);
                Fahrenheit fahr2 = (Fahrenheit)c;
                Console.WriteLine(" = {0} Fahrenheit", fahr2.Degrees);
                Console.ReadLine();
            }
        }
    }
    View Code

    implicit 关键字用于声明隐式的用户定义类型转换运算符。 如果可以确保转换过程不会造成数据丢失,则可使用该关键字在用户定义类型和其他类型之间进行隐式转换

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    
    namespace EventDemo
    {
        class Digit
        {
            public Digit(double d) { val = d; }
            public double val;
            // ...other members
            // User-defined conversion from Digit to double
            public static implicit operator double(Digit d)
            {
                return d.val;
            }
            //  User-defined conversion from double to Digit
            public static implicit operator Digit(double d)
            {
                return new Digit(d);
            }
        }
    
    
        class Program
        {
            static void Main(string[] args)
            {
                Digit dig = new Digit(7);
                //This call invokes the implicit "double" operator
                double num = dig;
                //This call invokes the implicit "Digit" operator
                Digit dig2 = 12;
                Console.WriteLine("num = {0} dig2 = {1}", num, dig2.val);
                Console.ReadLine();
            }
        }
    }
    View Code

    使用 operator 关键字来重载内置运算符,或提供类或结构声明中的用户定义转换。

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    
    namespace EventDemo
    {
        class Fraction
        {
            int num, den;
            public Fraction(int num, int den)
            {
                this.num = num;
                this.den = den;
            }
    
            // overload operator +
            public static Fraction operator +(Fraction a, Fraction b)
            {
                return new Fraction(a.num * b.den + b.num * a.den,
                   a.den * b.den);
            }
    
            // overload operator *
            public static Fraction operator *(Fraction a, Fraction b)
            {
                return new Fraction(a.num * b.num, a.den * b.den);
            }
    
            // user-defined conversion from Fraction to double
            public static implicit operator double(Fraction f)
            {
                return (double)f.num / f.den;
            }
        }
    
    
        class Program
        {
            static void Main()
            {
                Fraction a = new Fraction(1, 2);
                Fraction b = new Fraction(3, 7);
                Fraction c = new Fraction(2, 3);
                Console.WriteLine((double)(a * b + c));
                Console.ReadLine();
            }
        }
    }
    View Code
     
    按引用传递参数 -- 关键字ref

    和前面的“按值传递”相对应的是按引用传递。顾名思义,这里传递的不在是值,而是引用。注意这里不是传递一个复制品了,而是将真实的自己传到方法中供方法玩弄。

      注意点:

      1、按引用传递的参数,系统不再为形参在托管栈中分配新的内存。

      2、此时,形参名其实已经成为实参名的一个别名,它们成对地指向相同的内存位置。

    using System;
    using System.Collections.Generic;
    using System.Diagnostics;
    using System.Linq;
    using System.Runtime.InteropServices;
    using System.Text;
    using System.Threading.Tasks;
    using System.Timers;
    
    namespace AllDemo
    {
        public class Program
        {
            static void Main(string[] args)
            {
                int i = 1;
                int j = 2;
                int k = Plus(ref i, ref j);      //实参前也要加ref关键字
                Console.WriteLine(i);   //输出 2
                Console.WriteLine(j);   //输出 3
                Console.WriteLine(k);   //输出 5
    
                Console.ReadKey();
            }
    
            public static int Plus(ref int i, ref int j)    //形参钱要加ref关键字
            {
                i = i + 1;
                j = j + 1;
                return i + j;
            }
        }
    }
    View Code
     
    输出参数 - 关键字out

    输出参数和引用参数有一定程度的类似,输出参数可用于将值从方法内传递到方法外,实际上就相当于有多个返回值。要使用输出参数只需要将引用参数的ref关键字替换为out关键字即可。但又一点必须注意,只有变量才有资格作为输出参数,文本值和表达式都不可以,这点要谨记。

      注意两个问题:

      1、编译器允许在方法中的任意位置、任意时刻读取引用参数的值。

      2、编译器禁止在为输出参数赋值前读取它。

      这意味着输出参数的初始值基本上是没意义的,因为它在使用前要被赋予新的值。因此想通过输出参数将值传入方法的路是行不通的。

    using System;
    using System.Collections.Generic;
    using System.Diagnostics;
    using System.Linq;
    using System.Runtime.InteropServices;
    using System.Text;
    using System.Threading.Tasks;
    using System.Timers;
    
    namespace AllDemo
    {
        public class Program
        {
            static void Main(string[] args)
            {
                int i = 1;
                int j = 2;
                int k = Plus(i, out j);      //实参前也要加out关键字
                Console.WriteLine(i);   //输出 1
                Console.WriteLine(j);   //输出 100
                Console.WriteLine(k);   //输出 102
    
                Console.ReadKey();
            }
    
            public static int Plus(int i, out int j)
            {
                i = i + 1;
                j = 100;
                return i + j;
            }
        }
    }
    View Code

    参数数组 - 关键字params

    using System;
    using System.Collections.Generic;
    using System.Diagnostics;
    using System.Linq;
    using System.Runtime.InteropServices;
    using System.Text;
    using System.Threading.Tasks;
    using System.Timers;
    
    namespace AllDemo
    {
        public class Program
        {
            static void Main(string[] args)
            {
                int count1 = Plus(1);       //输出 1
                Console.WriteLine(count1);
    
                int count2 = Plus(1, 2, 3);//输出 6
                Console.WriteLine(count2);
    
                int count3 = Plus();    //输出 0  参数数组本身可选,没传入值也不会出错
                {
                    Console.WriteLine(count3);
                }
    
                Console.ReadKey();
            }
    
            public static int Plus(params int[] values)
            {
                int count = 0;
                foreach (int i in values)
                {
                    count = count + i;
                }
                return count;
            }
        }
    }
    View Code
  • 相关阅读:
    iou与giou对比
    Linux学习第一天 vim
    奖励加分申请
    人月神话阅读笔记3
    5.27
    5.26
    5.25
    5.23
    5.22
    5.21
  • 原文地址:https://www.cnblogs.com/scmail81/p/8678757.html
Copyright © 2011-2022 走看看