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. 网站日志流量分析采集(LuaJIT系统环境部署-node03,相关jar包自己手动上传)

    注:/usr/local/src 是源码包路径,可以自己更改 服务器中安装依赖 yum -y install gcc perl pcre-devel openssl openssl-devel 上传 ...

  2. Navicat导入.xls等文件失败

    一.问题 在学习django的models时,使用Navicat premium导入.xls数据的时候会出现导入失败的原因,即使是换成了csv文件也是失败的,原因是数据库的表有外键,而需要设置外键限制 ...

  3. 使用itext生成pdf的,各种布局

    代码如下,jar包为itext.jar,itextAsia.jar,最好都是最新的 :2张图片也在最后贴出,把图片放到D盘可以直接生成制定格式的pdf. 最后生成的pdf如下: 代码如下: packa ...

  4. Hibernate 基础入门

    概述: JDBC:将java和数据库连接的驱动程序加载到项目中,在代码里面,注册驱动,创建链接,创建satement对象,发送并执行sql之类,关闭连接. hibernate :数据持久化一个框架,对 ...

  5. Ubuntu 14.04 LTS 下使用校园网客户端DrclientLinux

    原先博客放弃使用,几篇文章搬运过来 下载客户端并解压 安装开发包 sudo -i dpkg --add-architecture i386 #添加32位的支持 apt-get update apt-g ...

  6. js中push和pop的用法

    push: 将新元素追加到一个数组中,并返回新的数组长度: 语法:arrayObj.push([item1 [item2 [. . . [itemN ]]]]) var number; var my_ ...

  7. vue.js项目nginx部署

    开发环境搭建完成.二.编译部署1.项目路径下demo输入命令npm run build编译完成后会发现在demo文件夹下多出一个dist文件夹这里面就是编译好的文件了.2.网上下载nginx,下载地址 ...

  8. Python logging模块简介

    logging模块提供logger,handler,filter,formatter. logger:提供日志接口,供应用代码使用.logger最长用的操作有两类:配置和发送日志消息.可以通过logg ...

  9. Scanner 类的使用

    /* Scanner类的使用: 1.导类 2.创建对象 3.调用对应的方法获取数据 */ import java.util.Scanner;//导入Scanner类 public class IfDe ...

  10. Vuejs——(1)入门(单向绑定、双向绑定、列表渲染、响应函数)

    版权声明:出处http://blog.csdn.net/qq20004604   目录(?)[+]   参照链接: http://cn.vuejs.org/guide/index.html [起步]部 ...