1. 声明变量:
int x;
String s1, s2;
Object o;
Object obj = new Object();
public String name;
2. 发出语句:
Response.Write("foo");
3. 注释:
// 单行注释
/**/ 多行注释
4. 访问索引属性:
String s = Request.QueryString("Name");
String value = Request.Cookies["key"];
5. 声明索引属性:
public String this[String name]
{
return (String)lookuptable[name];
}
6. 声明简单属性:
public String name
{
get
{...
return ...;
}
set{
...= value;
}
}
7. 使用和声明枚举:
//Declare the Enumeration
public enum MessageSize{
Small = 0;
Medium = 1;
Large = 2;
}
// Create a Field or Property
public MessageSize msgsize;
// Assign to the property using the Enumeration values
msgsize = Small;
8. 枚举集合
foreach(String s in coll)
{
...
}
9. 声明和使用方法:
// Declare a void return function
void function()
{
...
}
// Declare a function that returns a value
String function()
{
...
return (String) val;
}
String paramfunction(String a, String b){
...
return (String)(a+b);
}
// use the functions
String s1 = stringfunction();
String s2 = paramfunction("Hello","World!");
10. 自定义属性:
//Stand-alone attribute
[STAThread]
//Attribute with parameters
[DllImport("ADVAPI32.DLL",CharSet=CharSet.Auto)]
11. 数组:
String[] a = new String[3];
a[0] = "1";
a[1] = "2";
String[][] a = new String[3][3];
a[0][0] = "1";
12. 初始化:
String s = "Hello World";
int i = 1;
double[] a = {3.00,4.00,5.00}
13. If 语句:
if(Request.QueryString != null){
...
}
14. Case 语句:
switch(FirstName){
case "John":
...
break;
case ""
}
15. for 循环:
for(int i=0;i<3;i++)
{
a(i) = "test";
}
16. while循环:
int i = 0;
while(i<3)
{
Console.WriteLine(i.ToString());
i +=1;
}
17. 异常处理
try{
//Code that throws exceptions
}catch(OverflowException e){
// Catch a specific exception
catch(Exception e){
//Catch the generic exceptions
}finally{
// Execute some cleanup code
}
18. 字符串连接:
(1). +号
(2). StringBuilder s3 = new StringBuilder();
s3.Append("hello");
19. 事件处理程序委托
void MyButton_Click(Object sender,EventArgs E){
...
}
20. 声明事件:
//Create a public event
public event EventHandler MyEvent;
//Create a method for firing the event
protected void OnMyEvent(EventArgs e){
MyEvent(this,e);
}
向事件添加事件处理程序或从事件移除事件处理程序
Control.Change += new EventHandler(this.ChangeEventHandler);
Control.Change += new EventHandler(this.ChangeEventHandler);
21. 强制类型转换:
MyObject obj = (MyObject)Session["Some Value"];
IMyObject iObj = obj;
22. 转换:
int i=3;
String s = i.ToString();
double d = Double.Parse(s);
23. 带继承的类定义:
using System;
namespace MySpace{
public class Foo:Bar{
int x;
public Foo() { x=4;}
public void Add(int x)
{
this.x += x;
}
override public int GetNum() { return x;}
}
}
// csc/out:liberarycs.dll /t:library
// library.cs
24. 实现接口:
public class MyClass:IEnumerable{
...
IEnumerator Ienumerable.GetEnumerator(){
...
}
}
25. 带 main 方法的类定义:
using System;
public class ConsoleCS{
public ConsoleCS()
{
Console.WriteLine("Object Created");
}
public static void Main(String args[]){
Console.WriteLine("Hello World");
ConsoleCS ccs = new ConsoleCS();
}
}
// csc/out:consolecs.exe /t:exe console.cs
26. 标准模块:
using System;
public class Module{
public static void Main(String[] args){
Console.WriteLine("Hello World");
}
}
// csc/out:consolecs.exe /t:exe console.cs
分享到:
相关推荐
Learning hard C# 学习笔记
LearningHard C#学习笔记,学习C#的经典入门教材。 非常适合初学者,希望大家一起努力。
Visual C#学习笔记光盘 简介:本书由浅入深地讲解Visual C# 2005编程知识,全书内容分为6篇,共27章,第一篇介绍Visual Studio 2005开发环境和C#基础,其中包括Visual C# 2005简介,C#语言基础,面向对象编程...
C#学习笔记_20100614.pdf C#学习笔记_20100614.pdf
经典、简单C#学习笔记,各人学习的笔记,提供给大家分享。
c#学习笔记(1) 51099在线学习网发布 文章来源:网络收集 发布时间:2006-05-25 字体: [大 中 小] 51099在线学习网 http://www.51099.com 1, 结构(struct) 与 类(class) [attributes] [modifiers] struct ...
C#学习笔记 很经典的 看看吧 分也不多。。。。
C#学习笔记,内容丰富,十分有用。适合C#初学者,十分易懂。
C#学习笔记网络篇
这是我在学习c#课程过程中自己积累的一点心得,都是关于unity3d中c#的学习笔记,比较入门的那种,很适合小白使用。
C#学习笔记_夜晚回家 (全)C#学习笔记_夜晚回家 (全)C#学习笔记_夜晚回家 (全)
C#学习笔记(非常经典)很多前辈推荐的。希望能对大家有帮助。
C#学习笔记......
转载前辈的C#学习笔记,跟大家分享下 C#中关键字的小示 ........................ 1 如何产生随机数 .......................... 1 反射 - 获取运行时方法的信息 .............. 1 在C#中使用 Params 关键字 .......
C#学习笔记9月份-10月份