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. Android 多线程编程

    Android 多线程编程 //1.多线程 进程:操作系统的多道程序 线程:同一个程序的多条路径 //2.创建多线程程序 创建一个类extends Thread 重写run方法 在main方法中创建对 ...

  2. 数据库-mysql语句-查

    复习: 列类型: 数值类型:   20   '20' tinyint / smallint / int / bigint float / double / decimal(m,d) bool (TRU ...

  3. shell脚本中跳转另一台主机执行命令 <<EOF

    ssh vmuser@ <<EOFmkdir /home/vmuser/xunjianrm /home/vmuser/xunjian/xj.logtouch /home/vmuser/xu ...

  4. Paper | 帧间相关性 + 压缩视频质量增强(MFQE)

    目录 1. ABSTRACT 2. INTRODUCTION 3. RELATED WORKS 3.1. Quality Enhancement 3.2. Multi-frame Super-reso ...

  5. IntelliJ IDEA 2018.1.3 破解方法之一

    IntelliJ IDEA 2018.1.3 破解方法之一 声明:如果资金充足请购买正版! NO1 下载安装IntelliJ IDEA 企业版 NO2 下载jar包 (1)下载地址:http://id ...

  6. Android-Java-静态变量与静态方法内存图

    描述Dog对象: package android.java.oop10; public class Dog { public static String name; public static int ...

  7. 谈谈最近的想法和 Thoughtworks 的 Offer

    最近笔者一直没有记录博客,原因是因为卷入了面试,离职,谈判,思考等一系列事件中.不过可以先说明一下的是, 笔者最后还是拒绝了 Thoughtworks 的 Offer,继续留在目前的公司. 去年毕业后 ...

  8. 背水一战 Windows 10 (87) - 文件系统: 获取文件的属性, 修改文件的属性, 获取文件的缩略图

    [源码下载] 背水一战 Windows 10 (87) - 文件系统: 获取文件的属性, 修改文件的属性, 获取文件的缩略图 作者:webabcd 介绍背水一战 Windows 10 之 文件系统 获 ...

  9. 第30节:Java基础-内部类

    内部类 // 外部类 class Demo{ private int num = 3; // 定义内部类 class Int{ void show(){ System.out.println(&quo ...

  10. 第六篇: 分布式配置中心(Spring Cloud Config)

    一.简介 在分布式系统中,由于服务数量巨多,为了方便服务配置文件统一管理,实时更新,所以需要分布式配置中心组件. 在Spring Cloud中,有分布式配置中心组件spring cloud confi ...