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 + );
}
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 - ));
}
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();
}
}
}

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();
//This call invokes the implicit "double" operator
double num = dig;
//This call invokes the implicit "Digit" operator
Digit dig2 = ;
Console.WriteLine("num = {0} dig2 = {1}", num, dig2.val);
Console.ReadLine();
}
}
}

使用 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(, );
Fraction b = new Fraction(, );
Fraction c = new Fraction(, );
Console.WriteLine((double)(a * b + c));
Console.ReadLine();
}
}
}
 
按引用传递参数 -- 关键字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 = ;
int j = ;
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 + ;
j = j + ;
return i + j;
}
}
}
 
输出参数 - 关键字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 = ;
int j = ;
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 + ;
j = ;
return i + j;
}
}
}

参数数组 - 关键字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
Console.WriteLine(count1); int count2 = Plus(, , );//输出 6
Console.WriteLine(count2); int count3 = Plus(); //输出 0 参数数组本身可选,没传入值也不会出错
{
Console.WriteLine(count3);
} Console.ReadKey();
} public static int Plus(params int[] values)
{
int count = ;
foreach (int i in values)
{
count = count + i;
}
return count;
}
}
}

C#关键字的使用的更多相关文章

  1. 作为一个新手的Oracle(DBA)学习笔记【转】

    一.Oracle的使用 1).启动 *DQL:数据查询语言 *DML:数据操作语言 *DDL:数据定义语言 DCL:数据控制语言 TPL:事务处理语言 CCL:指针控制语言 1.登录 Win+R—cm ...

  2. JavaScript var关键字、变量的状态、异常处理、命名规范等介绍

    本篇主要介绍var关键字.变量的undefined和null状态.异常处理.命名规范. 目录 1. var 关键字:介绍var关键字的使用. 2. 变量的状态:介绍变量的未定义.已定义未赋值.已定义已 ...

  3. java面向对象中的关键字

    1,super关键字 super:父类的意思 1. super.属性名 (调用父类的属性) 2. super.方法名 (调用父类的方法) 3. super([参数列表])(调用父类的构造方法) 注意: ...

  4. 关于javascript中的this关键字

    this是非常强大的一个关键字,但是如果你不了解它,可能很难正确的使用它. 下面我解释一下如果在事件处理中使用this. 首先我们讨论一下下面这个函数中的this关联到什么. function doS ...

  5. transient关键字的用法

    本篇博客转自 一直在路上 Java transient关键字使用小记 1. transient的作用及使用方法 我们都知道一个对象只要实现了Serilizable接口,这个对象就可以被序列化,Java ...

  6. Java关键字:static

    通常,当创建类时,就是在描述那个类的外观和行为.只有用new创建类的对象时,才分配数据存储空间,方法才能被调用.但往往我们会有下面两种需求: 1.我想要这样一个存储空间:不管创建多少对象,无论是不创建 ...

  7. Core Java 总结(关键字,特性问题)

    2016-10-19 说说&和&&的区别 初级问题,但是还是加入了笔记,因为得满分不容易. &和&&都可以用作逻辑与的运算(两边是boolean类型), ...

  8. Net中的常见的关键字

    Net中的关键字有很多,我们最常见的就有new.base.this.using.class.struct.abstract.interface.is.as等等.有很多的,在这里就介绍大家常见的,并且有 ...

  9. php多关键字查询

      php单一关键字查询 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 tdansitional//EN" "http: ...

  10. Keil> 编译器特有的功能 > 关键字和运算符 > __weak

    __weak 此关键字指示编译器弱导出符号. 可以将 __weak 关键字应用于函数和变量声明以及函数定义. 用法 函数和变量声明 对于声明,此存储类指定一个 extern 对象声明,即使不存在,也不 ...

随机推荐

  1. The current state of generics in Delphi( 转载)

    The current state of generics in Delphi   To avoid duplication of generated code, the compiler build ...

  2. MYSQL 开发总结

    1.mysql中,VARCHAR(N)中的N代表的是字符数,而不是字节数.例如VARCHAR(255)表示可以保存255的中文 2.过大的长度会消耗更多的内存.VARCHAR(N),存储时是按照数据实 ...

  3. 题解 luogu P1144 【最短路计数】

    本蒟蒻也来发一次题解第一篇请见谅 这个题有几个要点 1.无向无权图,建图的时候别忘记建来回的有向边[因此WA掉1次 2.无权嘛,那么边长建成1就好了2333333 3.最短路采用迪杰斯特拉(别忘用堆优 ...

  4. Python之路(一)-python简介

    一.python简介,python2.x与python3.x的区别 Python是著名的“龟叔”Guido van Rossum在1989年圣诞节期间,为了打发无聊的圣诞节而编写的一个编程语言. Py ...

  5. B - Dropping tests

    In a certain course, you take n tests. If you get ai out of bi questions correct on test i, your cum ...

  6. Navicat for MYSQL 数据库手动同步方法

    Navicat for MYSQL 数据库手动同步方法     数据库同步有两种类型,一是结构同步,一般是数据库表增删,或是表中字段的增删:二是数据同步,即是表里面的记录的增删. 现假设我要让本地数据 ...

  7. 【接口时序】3、UART串口收发的原理与Verilog实现

    一.软件平台与硬件平台 软件平台: 1.操作系统:Windows-8.1 2.开发套件:ISE14.7 3.仿真工具:ModelSim-10.4-SE 硬件平台: 1.FPGA型号:XC6SLX45- ...

  8. AJPFX简述:MetaTrader 4移动交易平台

    (AJPFX)移动交易平台可以让客户随时通过客户手中的移动设备例如智能手机.PDA等管理自己帐户和进行交易.移动交易平台提供了完整的交易帐户管理分析选项,当客户无法使用台式计算机的时候,移动交易平台为 ...

  9. nova scheduler filters 配置

    scheduler_driver = nova.scheduler.filter_scheduler.FilterScheduler scheduler_default_filters = Avail ...

  10. Maven(个人整理)(一,未完待续)

                                                      Maven 1. 什么是Maven? 乍一看,Maven看起来很多东西,但简而言之,Maven试图将 ...