哪里建网站性价比高做网站注意哪方面
news/
2025/9/23 18:06:12/
文章来源:
哪里建网站性价比高,做网站注意哪方面,网站建设公司如何收费,数字展厅公司排名params object[] 用于函数多参数的定义public static void Write(string format, params object[] arg);explicit 关键字用于声明必须使用强制转换来调用的用户定义的类型转换运算符。 例如#xff0c;在下面的示例中#xff0c;此运算符将名为 Fahrenheit 的类转换为名为 Ce…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 doublepublic static implicit operator double(Digit d){return d.val;}// User-defined conversion from double to Digitpublic 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 operatordouble num dig;//This call invokes the implicit Digit operatorDigit 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 doublepublic 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); //输出 2Console.WriteLine(j); //输出 3Console.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); //输出 1Console.WriteLine(j); //输出 100Console.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); //输出 1Console.WriteLine(count1);int count2 Plus(1, 2, 3);//输出 6Console.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 转载于:https://www.cnblogs.com/scmail81/p/8678757.html
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/913413.shtml
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!