【SIKIA计划】_03_C#初级教程 (2015版)笔记

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/jvao_q7/article/details/84579747

Win32 API是微软的操作系统Windows提供给开发人员的编程接口,它决定了我们开发的Windows应用程序的能力。MFC是微软为开发人员提供的类库,在某种意义上是对Win32 API的封装。

MFC有一种被称为MFC扩展DLL的共享机制。但是你只能在MFC应用程序中使用它们。

COM提供了一种在不同的应用程序和语言之间共享二进制代码的规范。

C#是一种在.net环境下运行的语言之一。但是.net不只可以用于c#程序的运行,也可以用于其他程序的运行。.net是一个程序运行的环境。

.NET框架

编译工具

   

产生↓

   

被编译的代码

使用→

基类库(BCL)

被执行↓

 

被执行↓

公共语言运行库(CLR)

FCL框架类库划分成以下几层。

最内一层,由BCL的大部分组成,主要作用是对.NET框架、.NET运行时及CIL语言本身进行支持,例如基元类型、集合类型、线程处理、应用程序域、运行时、安全性、互操作等。

中间一层,包含了对操作系统功能的封装,例如文件系统、网络连接、图形图像、XML操作等。

最外一层,包含各种类型的应用程序,例如Windows Forms、Asp.NET、WPF、WCF、WF等。

编译过程

.NET兼容语言的源代码文件

.NET兼容编译器

程序集(公共中间语言CIL)(类型信息)(安全信息)

托管代码是.Net框架编写的代码,需要在CLR的环境下运行

非托管代码不在CLR控制之下,比如Win32 C/C++ DLL 成为非托管代码

//命名空间用到为白色,没用到为灰色

//引用命名空间

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

//定义命名空间从{开始,到}结束

namespace _001_Learn_Csharp

{

//定义类

class Program

{

//定义一个Main方法

static void Main(string[] args)

{

//方法体

Console.WriteLine("Hello world");

}

}

}

//ctrl+f5 运行完暂停,不会马上结束

类型库:命名空间(System)---class(Console)---WriteLine()

程序: 命名空间(_001_Learn_Csharp)---class(Program)---Main()

C#命名规则:变量使用驼峰命名,方法和类为每个单词大写

标识符

数字不能放在首位

@字符只能放在标示符的首位

System.Console.Write("Hello world1");

System.Console.WriteLine("Hello world2");

System.Console.WriteLine("两个数相加为{0}+{1}={2}",3,34,37);

0x 或 0X 表示十六进制,0 表示八进制,没有前缀则表示十进制。

用Unicode来表示一个转义字符(\u加上十六进制值) \u0027 为‘

使用@不识别转义字符(除了双引号其他转义字符都不在识别)

使用@可以多行输出

使用@表示路径(string path = "c:\\xxx\\xx\\xxx.doc"; string path =@"c:\xxx\xx\xxx.doc";)

注释(ctrl+k ctrl+c) 取消注释(ctrl+k ctrl+u)

int hp,mp=90,exp=50;

第一次给变量赋值叫做初始化

浮点数在计算机中用以近似表示任意某个实数。具体的说,这个实数由一个整数或定点数(即尾数)乘以某个基数(计算机中通常是2)的整数次幂得到,这种表示方法类似于基数为10的科学记数法。

001

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

namespace test_001

{

class Program{

static void Main(string[] args)

{

string name = "cai";

int hp = 100;

int level = 34;

float exp = 345.3f;

Console.WriteLine("主角的名字是:{0}\n血量:{1}\n等级:{2}\n经验值:{3}", name, hp, level, exp);

string str1 = "I'm a good man.You are bad gril!";

Console.WriteLine(str1);

string str2 = @"I'm a good man.\n You are bad gril!";

Console.WriteLine(str2);

Console.ReadKey();

}

}

}

02

string str1 = Console.ReadLine();

int num1 = Convert.ToInt32(str1);

string str2 = Console.ReadLine();

int num2 = Convert.ToInt32(str2);

int temp = num1;

num1 = num2;

num2 = num1;

Console.WriteLine(num1+":"+num2);

Console.ReadKey();

类型之间的转换

string locstr = 123.ToString();//如果要将"locstr"转成整型数int i =Convert.ToInt16(locstr);

//方法一: 用 Convert int ii = int.Parse(locstr)

//方法二: 用 Parse

double dnum = 100.1;int ifromd = (int)dnum; //double类型显式转换转为int类型Class1 c11 = new Class1();Class2 c22 = c11as Class2; //使用as进行显式转换

goto

int myInteger = 5;

goto mylabel;//goto语句用来控制程序跳转到某个标签的位置

myInteger++;

Console.WriteLine(myInteger);

Console.ReadKey();

循环的中断 break(终止当前循环)

循环的中断 continue(终止当前循环继续下一个循环)

循环的中断 return 跳出循环(跳出函数)

枚举

枚举类型的定义

enum <typeName>{

  <value1>,

  <value2>,

  <value3>,

  ...

  <valueN>

}

枚举类型的声明 <typeName> <varName>;

枚举类型的赋值<varName>=<typeName>.<value>;

enum GameState{

  Pause,

  Failed,

  Success,

  Start

}

在游戏中定义一个 GameState state = GameState.Start;

class Program{

static void Main(stirng)

}

结构体

如果我们要表示一个向量的话 需要定义,三个float类型 x y z

这样比较麻烦,不方便管理,我们可以使用结构

定义结构

struct <typeName>{

  <memberDeclarations>

}

其中<memberDeclarations>是结构体的成员,每个成员的声明如下

<type> <name>;

struct Vector3{

  float x;

  float y;

  float z;

}

——类是引用类型,结构是值类型。

——结构不支持继承。

——结构不能声明默认的构造函数。

数组

数组是一个存储相同类型元素的固定大小的顺序集合。数组是用来存储数据的集合,通常认为数组是一个同一类型变量的集合。

int[] scores;

第一种方式

scores ={34,34,3,43,43,4,34};

第二种方式

scores = new int[10]; 里面的每一个元素按照类型的默认值赋值

第三种方式

scores = new int[10]{123,12,34,56,77,89,85,6,45634,34};

数组的访问

<arrayName>[条目索引]

int [] shuzhu = new shuzhu[5] { 99, 98, 92, 97, 95}

foreach(int j in shuzhu ) { int i = j-100; Console.WriteLine("Element[{0}] = {1}", i, j); } Console.ReadKey();

多维数组

string [,] names;

交叉数组

int[][] scores = new int[2][]{new int[]{92,93,94},new int[]{85,66,87,88}};

参数数组 params

public 返回类型 方法名称( params 类型名称[] 数组名称 )

字符串

ToLower() 转化小写

ToUpper() 转化大写

Trim()去掉字符串前面或者后面空格

TrimStart()

TrimEnd()

Split('.') 把字符串按照指定的字符进行拆分,得到数组

委托

using System;

using System.IO;

namespace DelegateAppl{

class PrintString {

static FileStream fs;

static StreamWriter sw; // 委托声明  

public delegate void printString(string s); // 该方法打印到控制台  

public static void WriteToScreen(string str) {

Console.WriteLine("The String is: {0}", str);  

} // 该方法打印到文件  

public static void WriteToFile(string s) {

fs = new FileStream("c:\\message.txt", FileMode.Append, FileAccess.Write);

sw = new StreamWriter(fs);

sw.WriteLine(s);

sw.Flush();

sw.Close();

fs.Close();

} // 该方法把委托作为参数,并使用它调用方法  

public static void sendString(printString ps)  

{

ps("Hello World");  

}  

static void Main(string[] args) {

printString ps1 = new printString(WriteToScreen);

printString ps2 = new printString(WriteToFile);

sendString(ps1);

sendString(ps2);

Console.ReadKey();

}

}

}

猜你喜欢

转载自blog.csdn.net/jvao_q7/article/details/84579747