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的环境配置的时候,按照网上的教程,我还是走了许多弯道,其中遇到了不少的问题,但是现在都一一解决了. 配置时安装东西少 在配置的时候 ...

  2. ABP框架系列之二十五:(Embedded-Resource-Files-嵌入式资源文件)

    Introduction ASP.NET Boilerplate provides an easy way of using embedded Razor views (.cshtml files) ...

  3. python实现netcat部分功能源代码

    #!/opt/local/bin/python2.7 import sys import socket import getopt import threading import subprocess ...

  4. ROM、RAM、DRAM、SRAM和FLASH、PSRAM

    1.ROM和RAM指的都是半导体存储器,ROM是Read Only Memory的缩写,RAM是Random Access Memory的缩写.ROM在系统停止供电的时候仍然可以保持数据,而RAM通常 ...

  5. Hadoop 综合揭秘——MapReduce 基础编程(介绍 Combine、Partitioner、WritableComparable、WritableComparator 使用方式)

    前言 本文主要介绍 MapReduce 的原理及开发,讲解如何利用 Combine.Partitioner.WritableComparator等组件对数据进行排序筛选聚合分组的功能.由于文章是针对开 ...

  6. 基于ε-NFA的正则表达式引擎

    正则表达式几乎每个程序员都会用到,对于这么常见的一个语言,有没有想过怎么去实现一个呢?乍一想,也许觉得困难,实际上实现一个正则表达式的引擎并没有想像中的复杂,<编译原理>一书中有一章专门讲 ...

  7. 快速部署Apache服务静态网站

    Apache是世界使用排名第一的Web服务器软件.它可以运行在几乎所有广泛使用的计算机平台上,由于其跨平台和安全性被广泛使用,是最流行的Web服务器端软件之一.它快速.可靠并且可通过简单的API扩充, ...

  8. rocketmq搭建趟坑记

    这个坑对小白来讲可能要趟很久才能过,我就是这样~~明明很简单的配置,搞了半天 我用的是rocketmq4.1.0,配置了jvm参数,都能正常启动,且能在线上运行demo,但是线下就是连不上 在conf ...

  9. git简易使用

    git的安装以及GitHub的注册这里就不说了,这里直接从上传开始. 1. 登录github进入settings 2. 添加SSH KEY,添加方式查看第3步 3. 由于本地Git仓库和Github仓 ...

  10. 数组的三种声明方式总结、多维数组的遍历、Arrays类的常用方法总结

    1. 数组的三种声明方式 public class WhatEver { public static void main(String[] args) { //第一种 例: String[] test ...