foreach
//foreach
int[] fibarray = new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
foreach (int element in fibarray)
{
Console.WriteLine(element);
}
//计数器
int count = 0;
foreach(int element in fibarray)
{
count += 1;
Console.WriteLine("Element #{0}:{1}", count, element);
}
Console.WriteLine("Number of elements in the array:{0}", count);
输出参数out
class NumberManipulator
{
//按引用传递参数
public void swap(ref int x,ref int y)
{
int temp;
temp = x;
x = y;
y = temp;
}
//按输出传递参数
/*
就像引用参数,输出参数的形参担当实参的别名。方法内对形参的任何改变,在方法执行完成后,通过实参变量都是可见的。
不同的是,输出参数的要求是:
在方法内部,输出参数在被读取之前必须被赋值。这意味着,参数的初始值是无关的,而且没有必要在方法调用之前为实参赋值;
在方法返回之前,方法内部的任何贯穿路径的可能路径,都必须为所有输出参数进行一次赋值。
*/
public void getValue(out int x)
{
int temp = 5;
x = temp;
}
static void Main(string[] args)
{
NumberManipulator n = new NumberManipulator();
int a = 100;
int b = 200;
Console.WriteLine("交换前,a={0},b={1}", a, b);
n.swap(ref a, ref b);
Console.WriteLine("交换后,a={0},b={1}", a, b);
n.getValue(out a);
Console.WriteLine("调用后a的值:{0}", a);
}
}
可空类型(Nullable)
int? i=3;
//等价于
Nullable <int> i = new NUllable<int>(3);
int i;//默认值0
int? ii;//默认值null
using System;
namespace CalculatorApplication
{
class NullablesAtShow
{
static void Main(string[] args)
{
double? num1 = null;
double? num2 = 3.14157;
double num3;
num3 = num1 ?? 5.34; // num1 如果为空值则返回 5.34
Console.WriteLine("num3 的值: {0}", num3);
num3 = num2 ?? 5.34;
Console.WriteLine("num3 的值: {0}", num3);
Console.ReadLine();
}
}
}
输入输出:System.Console.ReadLine(),System.Console.WriteLine();
结构体
类VS结构体
#define预处理器
Regex类 用于表示一个正则表达式
特性
预定义特性(Attribute)
索引器(Indexer):索引器(Indexer) 允许一个对象可以像数组一样使用下标的方式来访问。
当您为类定义一个索引器时,该类的行为就会像一个 虚拟数组(virtual array) 一样。您可以使用数组访问运算符 [ ] 来访问该类的的成员。
//一维索引器
element-type this[int index]
{
// get 访问器
get
{
// 返回 index 指定的值
}
// set 访问器
set
{
// 设置 index 指定的值
}
}
委托(Delegate):相当于函数指针。
delegate <return type> <delegate-name> <parameter list>
using System;
delegate int NumberChanger(int n);
namespace DelegateAppl
{
class TestDelegate
{
static int num = 10;
public static int AddNum(int p)
{
num += p;
return num;
}
public static int MultNum(int q)
{
num *= q;
return num;
}
public static int getNum()
{
return num;
}
static void Main(string[] args)
{
// 创建委托实例
NumberChanger nc1 = new NumberChanger(AddNum);
NumberChanger nc2 = new NumberChanger(MultNum);
// 使用委托对象调用方法
nc1(25);
Console.WriteLine("Value of Num: {0}", getNum());
nc2(5);
Console.WriteLine("Value of Num: {0}", getNum());
Console.ReadKey();
}
}
}
//Value of Num: 35
//Value of Num: 175
事件(Event):事件(Event) 基本上说是一个用户操作,如按键、点击、鼠标移动等等,或者是一些提示信息,如系统生成的通知。应用程序需要在事件发生时响应事件。例如,中断。
C# 中使用事件机制实现线程间的通信
using System;
namespace SimpleEvent
{
using System;
/***********发布器类***********/
public class EventTest
{
private int value;
public delegate void NumManipulationHandler();
public event NumManipulationHandler ChangeNum;
protected virtual void OnNumChanged()
{
if ( ChangeNum != null )
{
ChangeNum(); /* 事件被触发 */
}else {
Console.WriteLine( "event not fire" );
Console.ReadKey(); /* 回车继续 */
}
}
public EventTest()
{
int n = 5;
SetValue( n );
}
public void SetValue( int n )
{
if ( value != n )
{
value = n;
OnNumChanged();
}
}
}
/***********订阅器类***********/
public class subscribEvent
{
public void printf()
{
Console.WriteLine( "event fire" );
Console.ReadKey(); /* 回车继续 */
}
}
/***********触发***********/
public class MainClass
{
public static void Main()
{
EventTest e = new EventTest(); /* 实例化对象,第一次没有触发事件 */
subscribEvent v = new subscribEvent(); /* 实例化对象 */
e.ChangeNum += new EventTest.NumManipulationHandler( v.printf ); /* 注册 */
e.SetValue( 7 );
e.SetValue( 11 );
}
}
}
/*
event not fire
event fire
event fire
*/
参数数组 params :可变长度参数
class Program
{
static int SumVals params int[] vals)
{
int sum=0;
foreach (int bal in vals)
{
sum+=val;
}
return sum;
}
static void Main(String[] args)
{
int sum = SumVals(1,5,2,9,8);
Console.WriteLine("Summed Values = {0}",sum);
Console.ReadKey();
}
}
原文:https://www.cnblogs.com/Oooval/p/14327790.html