C#关键字的使用
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();
}
}
}
和前面的“按值传递”相对应的是按引用传递。顾名思义,这里传递的不在是值,而是引用。注意这里不是传递一个复制品了,而是将真实的自己传到方法中供方法玩弄。
注意点:
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;
}
}
}
输出参数和引用参数有一定程度的类似,输出参数可用于将值从方法内传递到方法外,实际上就相当于有多个返回值。要使用输出参数只需要将引用参数的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#关键字的使用的更多相关文章
- 作为一个新手的Oracle(DBA)学习笔记【转】
一.Oracle的使用 1).启动 *DQL:数据查询语言 *DML:数据操作语言 *DDL:数据定义语言 DCL:数据控制语言 TPL:事务处理语言 CCL:指针控制语言 1.登录 Win+R—cm ...
- JavaScript var关键字、变量的状态、异常处理、命名规范等介绍
本篇主要介绍var关键字.变量的undefined和null状态.异常处理.命名规范. 目录 1. var 关键字:介绍var关键字的使用. 2. 变量的状态:介绍变量的未定义.已定义未赋值.已定义已 ...
- java面向对象中的关键字
1,super关键字 super:父类的意思 1. super.属性名 (调用父类的属性) 2. super.方法名 (调用父类的方法) 3. super([参数列表])(调用父类的构造方法) 注意: ...
- 关于javascript中的this关键字
this是非常强大的一个关键字,但是如果你不了解它,可能很难正确的使用它. 下面我解释一下如果在事件处理中使用this. 首先我们讨论一下下面这个函数中的this关联到什么. function doS ...
- transient关键字的用法
本篇博客转自 一直在路上 Java transient关键字使用小记 1. transient的作用及使用方法 我们都知道一个对象只要实现了Serilizable接口,这个对象就可以被序列化,Java ...
- Java关键字:static
通常,当创建类时,就是在描述那个类的外观和行为.只有用new创建类的对象时,才分配数据存储空间,方法才能被调用.但往往我们会有下面两种需求: 1.我想要这样一个存储空间:不管创建多少对象,无论是不创建 ...
- Core Java 总结(关键字,特性问题)
2016-10-19 说说&和&&的区别 初级问题,但是还是加入了笔记,因为得满分不容易. &和&&都可以用作逻辑与的运算(两边是boolean类型), ...
- Net中的常见的关键字
Net中的关键字有很多,我们最常见的就有new.base.this.using.class.struct.abstract.interface.is.as等等.有很多的,在这里就介绍大家常见的,并且有 ...
- php多关键字查询
php单一关键字查询 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 tdansitional//EN" "http: ...
- Keil> 编译器特有的功能 > 关键字和运算符 > __weak
__weak 此关键字指示编译器弱导出符号. 可以将 __weak 关键字应用于函数和变量声明以及函数定义. 用法 函数和变量声明 对于声明,此存储类指定一个 extern 对象声明,即使不存在,也不 ...
随机推荐
- 在excel中如何利用vba通过网址读取网页title(网址是https的)?
昨天在百度知道上提了这个问题,我保存了些百度知道我回答的网址,想利用excel直接读取出网址的title,请问vba代码怎么写?(要支持https的) excel大神帮我回答了,在这记录下: Func ...
- MYSQL 开发总结
1.mysql中,VARCHAR(N)中的N代表的是字符数,而不是字节数.例如VARCHAR(255)表示可以保存255的中文 2.过大的长度会消耗更多的内存.VARCHAR(N),存储时是按照数据实 ...
- 如何搭建zabbix server端
1.背景介绍: nginx:1.9.3 安装路径/data/nginxphp:5.5.27 安装路径 /data/phpmysql:5.6.28 安装路径/usr/local/mysqlzabbix ...
- 小程序快捷键(mac中)
快捷键 格式调整 - Ctrl+S:保存文件 - Ctrl+[, Ctrl+]:代码行缩进 - Ctrl+Shift+[, Ctrl+Shift+]:折叠打开代码块 - Ctrl+C Ctrl ...
- MFC开发中添加自定义消息和消息响应函数
(1)在.h或.cpp文件定义一个消息 #define CLICK_MESSAGE_BOX WM_USER+1001 //add by 20180612 给主窗口ctrl.cpp发送消息 //自定义消 ...
- uniGUI日志的控制
uniGUI日志的控制 (2015-10-12 08:30:29) 转载▼ 标签: unigui 分类: uniGUI uniGUI本身提供了日志功能,利用uniServerModule.Server ...
- zookeeper配置文件共享中心
最近频繁的系统上线,每次打包都要把配置文件替换为正式环境的配置文件,虽然说就是复制粘贴的事,架不住文件杂乱,而且多. 期初的想法是有没有办法将配置文件与系统隔离开来,这样在更新时候,就只需要更新代码部 ...
- Git 的一个教程网站(中文、GUI)
交互教学网站:http://learngitbranching.js.org/ Github Repositry (fork)https://github.com/pcottle/learnGitBr ...
- 人脸检测第一文---A Dream of Spring
人脸识别研究的人很多,可是,真正具有划时代意义的还要当属Paul Viola的一篇文章<RobustReal-time Object Detection>.这篇文章让 人脸识别在实际应用中 ...
- javascript 异步解析
js 异步解析 一 .js单线程分析 我们都知道js的一大特点是单线程,也就是同一时间点,只能处理一件事,一句js代码.那为什么js要设计成单线程而不是多线程呢?这主要和js的用途有关,js作为浏览器 ...