C# Programming Study #1
- 引用的时候需要在参数和使用的时候加上 ref 关键字
static bool addnum (ref int val) //引用
{
++val;
return true;
}参数数组的概念,可以接受该类型的数组,不限制大小,关键字 params
static int getlength (params int[] vals) //参数数组
{
int sum = 0;
foreach (int val in vals)
{
sum += val;
}
return sum;
}输出参数的概念,类似参数数组,不过声明的时候可以不初始化
static int MaxValue (int[] intArray, out int maxIndex) //输出参数
{
int maxValue = intArray[0];
maxIndex = 0;
for (int i = 1; i < intArray.Length; ++i)
{
if (intArray[i] > maxValue)
{
maxValue = intArray[i];
maxIndex = i;
}
}
return maxValue;
}- 委托 delegate : 委托的声明类似函数,但不带函数体,并且要使用 delegate 关键字。委托的声明指定了一个 返回类型 和一个 参数列表
delegate double ProcessDelegate (double param1, double param2); static double Multiply (double param1, double param2)
{
return param1 * param2;
} static double Divide (double param1, double param2)
{
return param1 / param2;
} static void Main(string[] args)
{
ProcessDelegate process;
string input = Console.ReadLine();
if (input.CompareTo("M") == 0)
{
process = new ProcessDelegate (Multiply);
}
else
{
process = new ProcessDelegate(Divide);
}
double param1 = 3.123;
double param2 = 23.12;
Console.WriteLine("{0}", process(param1, param2));
}- 指定类是抽象的 abstract,不能实例化,只能继承,可以有抽象成员
abstract class MyClass1
{ }- 指定类是密封的 sealed, 不能继承
sealed class MyClass2
{ }- 成员定义
- public 成员可以由任何代码访问
- private 成员由类中的代码访问
- internal 成员只能由定义它的程序集内部的代码访问
- protected 成员只能由类或派生类中的代码访问
- 定义方法
- virtual 方法可以重写
- abstract 方法必须在非抽象类的派生类中重写
- override 方法重写了一个基类方法
- extern 方法定义放在其他地方
- 接口实现
- 不允许使用访问修饰符(public, private, protected, internal)
- 接口成员不能包含代码体
- 接口不能定义字段成员
- 接口成员不能使用关键字 static, virtual, abstractm sealed
- 类型定义成员是禁止的
public interface IMyInterface //在类中实现接口
{
void DoSomething();
void DoSomethingElse();
} public class MyClass : IMyInterface
{
public void DoSomething()
{ }
public void DosomethingElse()
{ }
}
- 泛型
- 可空类型
- int? 是 System.Nullble <int>的缩
int? op = 5;
int? result1 = (int)op * 2; //method 1
int? result2 = op.Value * 2; //method 2 int? op1 = null;
int? op2 = 5;
int? result3 = op1 * op2;
- ?? 运算符
op1 ?? op2
op1 == null ? op2 : op1
- 可空类型
- 泛型类 List
-
class Program
{
static void Main(string[] args)
{
List<Animal> animalCollection = new List<Animal>();
animalCollection.Add(new Cow("Tom"));
animalCollection.Add(new Dog("Dave")); foreach (Animal myAnimal in animalCollection)
{
Console.WriteLine(myAnimal.GetName());
}
}
}
-
- 属性的访问器
- 包含与获取(读取或计算)或设置(写)属性有关的可执行语句。访问器声明可以包含 get 访问器或 set 访问器,或者两者均包含。
- get {}
- get 访问器体与方法体相似。它必须返回属性类型的值。执行 get 访问器相当于读取字段的值。以下是返回私有字段 name 的值的 get 访问器:
-
private string name; // the name field
public string Name // the Name property
{
get
{
return name;
}
} - 当引用属性时,除非该属性为赋值目标,否则将调用 get 访问器读取该属性的值。例如:
Employee e1 = new Employee(); ... Console.Write(e1.Name);
// The get accessor is invoked here get 访问器必须在 return 或 throw 语句中终止,并且控制不能超出访问器体。
- set {}
- set 访问器与返回 void 的方法类似。它使用称为 value 的隐式参数,此参数的类型是属性的类型。在下例中,set 访问器被添加到 Name 属性:
public string Name
{
get
{
return name;
}
set
{
name = value;
}
}- 当对属性赋值时,用提供新值的参数调用 set 访问器。例如:
e1.Name = "Joe";
// The set accessor is invoked here 在 set 访问器中对局部变量声明使用隐式参数名 (value) 是错误的。
// property_hiding.cs
// Property hiding
using System;
public class BaseClass
{
private string name;
public string Name
{
get
{
return name;
}
set
{
name = value;
}
}
} public class DerivedClass : BaseClass
{
private string name;
public new string Name // Notice the use of the new modifier
{
get
{
return name;
}
set
{
name = value;
}
}
} public class MainClass
{
public static void Main()
{
DerivedClass d1 = new DerivedClass();
d1.Name = "John"; // Derived class property
Console.WriteLine("Name in the derived class is: {0}",d1.Name);
((BaseClass)d1).Name = "Mary"; // Base class property
Console.WriteLine("Name in the base class is: {0}",
((BaseClass)d1).Name);
}
}Example Code
- get {}
- #region DisaplayName
- #endregion
- 用来注释中间代码 的作用 而且在其他地方用到中间的类和方法 都会有你标注的注释
- 本身不参与编译 还可以缩进代码 方便阅览折叠代码
-
// preprocessor_region.cs
#region MyClass definition
public class MyClass
{
public static void Main()
{
}
}
#endregion
-
C# Programming Study #1的更多相关文章
- C# Programming Study #2
readonly (C# Reference) readonly 关键字是可以在字段上使用的修饰符. 当字段声明包括 readonly 修饰符时,该声明引入的字段赋值只能作为声明的一部分出现,或者 ...
- Linux: bash script
content [toc] bash scripts equivalent bash command to rename a bash variable/command alias fire='fir ...
- 随机生成&部门匹配
整体概况 1.完整程序概况 (1)程序整体构架 (2)生成程序模型 (3)匹配算法模型 (4)生成结果评估 (5)命名规范 (6)先期和后期分工 2.心路历程与对全新的java认识(心得体会篇) (1 ...
- Beginning Scala study note(4) Functional Programming in Scala
1. Functional programming treats computation as the evaluation of mathematical and avoids state and ...
- study notes: high performance linux server programming
1:linux网络API分为:socker地址API,socker基础API,网络信息API 1,socker地址API:包含IP地址和端口(ip, port).表示TCP通信的一端. 2,socke ...
- Programming Learning - Based on Project
Today when taking a bath I got a good idea that it is an efficient and interesting way to learn a ne ...
- Dynamic Programming
We began our study of algorithmic techniques with greedy algorithms, which in some sense form the mo ...
- Case Study: Random Number Generation(翻译教材)
很荣幸,经过三天的努力.终于把自己翻译的教材做完了,现在把它贴出来,希望能指出其中的不足. Case Study: Random Number Generation Fig. 6.7 C++ 标 ...
- PHP Laravel框架入门心得 | How to study PHP Laravel Framework
PHP有不少开发框架,其中比较出名的有Symfony和Laravel. 我说说我最近入门Laravel的感受和学习方法吧. 1.第一个感受是Laravel的社区讨论和学习资源真的是太棒了,中文化也做得 ...
随机推荐
- ACCP6.0 教程课件,可用
下载地址 求分 http://download.csdn.net/detail/qq873113580/6038955 包含ACCP6.0所有,是我朋友的自己保存的,请大家不要修改密码,谢谢 下面就是 ...
- js实现日期的相加减
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/ ...
- 基于ZooKeeper的Dubbo简单抽样登记中心
一:设备zookeeper 系统环境 Ubuntu 14.04.2 LTS x64 IP : 192.168.1.102 下载zookeeper-3.4.6.tar.gz到文件夹/opt.拉开拉链 m ...
- 解决iOS7中UITableView在使用autolayout时layoutSubviews方法导致的crash
近期公司项目上线后,出现了大量的crash,发生在iOS7系统上,和UITableView相关: Auto Layout still required after executing -layoutS ...
- HTTP协议(超文本传输协议)
一.HTTP的简介: 超文本传输协议. 它是基于TCP连接的(默认端口号是80).所以在传输数据前客户端需向服务器发送连接请求.当服务器同意连接请求,建立连接后才可以发送数据报文. 二.HTTP的报文 ...
- windows下fitness python版本安装测试
FitNesse介绍¶ FitNesse是一套软件开发协作工具. 伟大的软件需要协作和交流,FitNesse可以帮助大家加强软件开发过程中的协作.能够让客户.测试人员和开发人员了解软件要做成什么样,自 ...
- 8.PHP 教程_PHP字符串
字符串变量用于存储并处理文本. PHP中的字符串变量 字符串变量用于包含有字符的值. 在创建字符串之后,我们就可以对它进行操作了.您可以直接在函数中使用字符串,或者把它存储在变量中. 在下面的实例中, ...
- python初探-copy
python中,数据的拷贝有以下三种形式:赋值.浅copy和深copy.根据类型的不同,可以把数据分成以下两类:字符串和数字为一类,其他(包括列表.元祖.字典...)为一类. 在python中有池的概 ...
- Python之路Day2
-->the start 养成好习惯,每次上课的内容都要写好笔记. 第二天内容主要是熟悉int.long.float.str.list.dict.tuple这几个类的内建方法. 对于Python ...
- 设置Firefox禁用js缓存
让firefox禁用缓存 让Firefox不再使用缓存网站开发时经常会有这样的疑问:为什么修改了代码,刷新了页面还是没有看到改动呢? 其实,可能只是你的Firefox并没有去下载你更新了的文件. 这时 ...