一.C#语言
1.程序结构
一个c#程序主要包括以下部分:
命名空间声明(Namespace declaration)
- 一个 class
- Class 方法
- Class 属性
- 一个 Main 方法
- 语句(Statements)& 表达式(Expressions)
- 注释
using System;
namespace HelloWorldApplication
{
class HelloWorld
{
static void Main(string[] args)
{
Console.WriteLine("Hello World");
Console.ReadKey();
}
}
}
Hello world
解释各部分
-
程序的第一行 using System; - using 关键字用于在程序中包含 System 命名空间。
-
一个程序一般有多个 using 语句。
下一行是 namespace 声明。一个 namespace 里包含了一系列的类。HelloWorldApplication 命名空间包含了类 HelloWorld。 -
下一行是 class 声明。类 HelloWorld 包含了程序使用的数据和方法声明。类一般包含多个方法。方法定义了类的行为。在这里,HelloWorld 类只有一个 Main 方法。
-
下一行定义了 Main 方法,是所有 C# 程序的 入口点。Main 方法说明当执行时 类将做什么动作。
-
Main 方法通过语句 Console.WriteLine(“Hello World”); 指定了它的行为。
WriteLine 是一个定义在 System 命名空间中的 Console 类的一个方法。该语句会在屏幕上显示消息 “Hello, World!”。 -
最后一行 Console.ReadKey(); 是针对 VS.NET 用户的。这使得程序会等待一个按键的动作,防止程序从 Visual Studio .NET 启动时屏幕会快速运行并关闭。
2.数据类型
简单数据类型
byte、short、int、long、float、double、char、bool
using System;
namespace DataTypeApplication
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Size of int: {0}", sizeof(int));
}
}
}
Size of int:4
如需得到一个类型或一个变量在特定平台上的准确尺寸,可以使用 sizeof 方法。表达式 sizeof(type) 产生以字节为单位存储对象或类型的存储尺寸。
组合数据类型
struct、enum、class
引用类型
作为参数传递,传递地址。包括:class类型、数组
namespace ConsoleApplication3
{
public class Book
{
public double Price;
public string Title;
public string Author;
}
class Program
{
static void Changbook(Book bk)
{
bk.Price = 1.01;
bk.Title = "Spss";
bk.Author = "john";
}
static void Printbook(Book bk)
{
//依次填充
Console.WriteLine("Book Infor:\nPrice={0},Title={1},Author={2}", bk.Price, bk.Title, bk.Author
);
}
static void Main(string[] args)
{
Book bk=new Book();//错误Book bk;
bk.Price = 10.01;
bk.Title = "Matable";
bk.Author = "Tom";
Printbook(bk);
Changbook(bk);
Printbook(bk);
}
}
}
引用类型传递地址,要改变本身所储存的值
值类型
作为参数传递时,传递拷贝。包括:简单的数据类型、struct类型、enum类型
namespace ConsoleApplication3
{
//struct类型 是值类型作为参数传递时传递拷贝
public struct Book
{
public double Price;
public string Title;
public string Author;
}
class Program
{
static void Changbook(Book bk)
{
bk.Price = 1.01;
bk.Title = "Spss";
bk.Author = "john";
}
static void Printbook(Book bk)
{
Console.WriteLine("Book Infor:\nPrice={0},Title={1},Author={2}", bk.Price, bk.Title, bk.Author
);
}
static void Main(string[] args)
{
//Book bk形式是存在栈里面的,存在栈里面是由通用系统来负责收集和管理
//Book bk=new Book()是存在堆里面的,存在堆里面需要手动的来释放它的存储空间否则会出现内存泄漏错误
Book bk;//=new Book();
bk.Price = 10.01;
bk.Title = "Matable";
bk.Author = "Tom";
Printbook(bk);
Changbook(bk);
Printbook(bk);
}
}
}
该例说明值类型传递拷贝,不改变自身存储的值
- struct和class创建类的区别
- class创建的类,如果没有声明成员变量和函数成员的权限时,默认为都是private的
public static const int MAX_VALUE = 10;
- struct创建的类,如果没有声明成员变量和函数成员的权限时,默认为都是public的
3.常量与变量
常量的定义:变量类型 变量名;
常量可以是任何基本数据类型,比如整数常量、浮点常量、字符常量或者字符串常量,还有枚举常量。
- 常量定义:
- readonly 在声明或构造函数中初始化,动态常量
- const 在声明时初始化 ,静态常量
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace 常量
{
public class SimpleClass
{
public int x;
public readonly int y = 2;
public readonly int z;
public const double pi = 3.1415926;
public const string Etc="...";
public SimpleClass()
{z=3;}
public SimpleClass(int p1, int p2, int p3)
{
x = p1;
y = p2;
z = p3;
}
}
class Program
{
static void Main(string[] args)
{
SimpleClass sp1 = new SimpleClass();
sp1.x = 1;
Console.WriteLine("sp1:x={0},y={1},z={2}", sp1.x, sp1.y, sp1.z);
SimpleClass sp2 = new SimpleClass(-1, -2, -3);
//在这里的const类似于static Console.WriteLine("sp2:x={0},y={1},z={2}",sp2.x,sp2.y,sp2.z);
Console.WriteLine("pi={0}{1}", SimpleClass.pi, SimpleClass.Etc);
}
}
}
4.运算符与表达式
- 运算符:
- 一元运算符 x++,y++
- 二元运算符 x+y,x-y
- 三元运算符 max=(x>y)?x:y;
- 运算符
表达式:有运算符和变量或常量组成的式子。
5.基础语句
5.1 赋值语句
变量名=表达式;
5.2 条件语句
using System;
namespace DecisionMaking
{
class Program
{
static void Main(string[] args)
{
/* 局部变量定义 */
int a = 10;
/* 使用 if 语句检查布尔条件 */
if (a < 20)
{
/* 如果条件为真,则输出下面的语句 */
Console.WriteLine("a 小于 20");
}
Console.WriteLine("a 的值是 {0}", a);
Console.ReadLine();
}
}
}
a 小于 20
a 的值是 10
5.3 开关语句
switch(expression){
case constant-expression :
statement(s);
break;
case constant-expression :
statement(s);
break;
/* 您可以有任意数量的 case 语句 */
default : /* 可选的 */
statement(s);
break;
}
- 当被测试的变量等于 case 中的常量时,case 后跟的语句将被执行,直到遇到 break 语句为止。
- 当遇到 break 语句时,switch 终止,控制流将跳转到 switch 语句后的下一行。
- 一个 switch 语句可以有一个可选的 default case,出现在 switch 的结尾。default case 可用于在上面所有 case 都不为真时执行一个任务。default case 中的 break 语句不是必需的。
using System;
namespace DecisionMaking
{
class Program
{
static void Main(string[] args)
{
/* 局部变量定义 */
char grade = 'B';
switch (grade)
{
case 'A':
Console.WriteLine("很棒!");
break;
case 'B':
case 'C':
Console.WriteLine("做得好");
break;
case 'D':
Console.WriteLine("您通过了");
break;
case 'F':
Console.WriteLine("最好再试一下");
break;
default:
Console.WriteLine("无效的成绩");
break;
}
Console.WriteLine("您的成绩是 {0}", grade);
Console.ReadLine();
}
}
}
做得好
您的成绩是 B
5.4 循环语句
using System;
namespace Loops
{
class Program
{
static void Main(string[] args)
{
/* 局部变量定义 */
int a = 10;
/* while 循环执行 */
while (a < 15)
{
Console.WriteLine("a 的值: {0}", a);
a++;
}
/* for 循环执行 */
for (int a = 10; a < 15; a = a + 1)
{
Console.WriteLine("a 的值: {0}", a);
}
}
}
}
a 的值: 10
a 的值: 11
a 的值: 12
a 的值: 13
a 的值: 14
- foreach语句
使用foreach可以迭代数组或者一个集合对象。
以下实例有三个部分: - 通过 foreach 循环输出整型数组中的元素。
- 通过 for 循环输出整型数组中的元素。
- foreach 循环设置数组元素的计算器。
class ForEachTest
{
static void Main(string[] args)
{
int[] fibarray = new int[] { 0, 1, 1, 2, 3, 5, 8, 13 };
foreach (int element in fibarray)
{
System.Console.WriteLine(element);
}
System.Console.WriteLine();
// 类似 foreach 循环
for (int i = 0; i < fibarray.Length; i++)
{
System.Console.WriteLine(fibarray[i]);
}
System.Console.WriteLine();
// 设置集合中元素的计算器
int count = 0;
foreach (int element in fibarray)
{
count += 1;
System.Console.WriteLine("Element #{0}: {1}", count, element);
}
System.Console.WriteLine("Number of elements in the array: {0}", count);
}
}
0
1
1
2
3
5
8
13
0
1
1
2
3
5
8
13
Element #1: 0
Element #2: 1
Element #3: 1
Element #4: 2
Element #5: 3
Element #6: 5
Element #7: 8
Element #8: 13
Number of elements in the array: 8
5.5 try…catch…finally
以上例子注意try…catch…finally语句的语法结构,该语句通常用来捕获异常语句
5.6 break、continue语句
6.c++创建对象的三种方式
int main()
{
A a(1); //栈中分配
A b = A(1); //栈中分配
A* c = new A(1); //堆中分配
delete c;
return 0; }
第一种和第二种分别是隐式调用,显式调用,两者都是在进程虚拟地址空间中的栈中分配内存,而第三种使用了new,在堆中分配了内存。而栈中内存的分配和释放是由系统管理,而堆中内存的分配和释放必须由程序员手动释放。