OOP

  • 1.变量和表达式
    • 1.1注释
    • 1.2C#控制台程序基本结构
    • 1.3变量(从存储长度来看)
    • 1.4变量的命名
    • 1.5字面值
    • 1.6运算符
  • 2流程控制
    • 2.1分支
    • 2.2循环
  • 3变量知识拓展
    • 3.1类型转换
    • 3.2枚举
    • 3.3结构(体)
    • 3.4数组
    • 3.5字符串的处理
  • 4函数
    • 4.1返回值
    • 4.2参数
  • 5.调试和错误处理
  • 6.面向对象编程
  • 7.定义类
  • 8.定义类成员
  • 9.集合、比较和转换
  • 10.泛型
  • 11.高级C#技术

1.变量和表达式

1.1注释

  • 单行注释://
  • 多行注释:/**/
  • 文档注释:///
    VS编译后,会产生一个文本文件,该文件可创建文档

1.2C#控制台程序基本结构

using System;
namespace ConsoleApplication1
{
	class Program
	{
		static void Main(string[] args)
		{
			Console.WriteLine("The first App in Begining C# Program!");
			Console.Readkey();
		}
	}
}

折叠预处理

#region 
……
……
#endregion

(折叠中间部分)

1.3变量(从存储长度来看)

  • 转义字符:为了将字面值赋给变量不出错
    eg:
    string myString = ““string” is”;

  • 整数
    sbyte [-128, 127] 8位
    byte [0, 255] 8位
    short [-32 768, 32767] 16位
    ushort [0, 65535] 16位
    int [-2 147 483 648, 2 147 483 647] 32位
    uint [0, 4 294 967 296] 32位
    long [9 223 372 036 854 775 808, 9 223 372 036 854 775 807] 64位
    ulong [0, 18 446 744 073 709 551 615] 64位

  • 浮点
    前两种存储类型:+/- m x 2^e
    后一种存储方式:+/- m x 10^e
    类型 m-min m-max e-min e-max
    float 0 2^24 -149 104
    double 0 2^53 -1075 970
    decimal 0 2^96 -28 0

  • 文本和布尔
    char [0, 65 535]
    bool ture/false
    string 没有上限

1.4变量的命名

1、变量名的第一个字母必须是字母、下划线或@
2、其后可以是字母、下划线或数字
3(注意)变量名不能是关键字

1.5字面值

类型							后缀
int, uint, long, ulong			无
uint、ulong						u或U
long、ulong						l或L
ulong					(u、l 位置关系和大小写不影响)Ul,Lu……均可
float						f或F
double						无、d或D
decimal						m或M

@置于字符串最前端,无需转义,即可正确存储和表达字面值

1.6运算符

  • 比较运算符
    ==、!=、<、>、<=、>=

  • 条件运算符
    &&、| |

  • 赋值运算符
    =、+=、-=、*=、/=、%=、&=、|=、^=

  • 其他
    一元:++、–,+(正)、-(负)
    三元:逻辑表达式?a:b
    逻辑表达式成立,三元表达式值为a,否则为b
    二元:+、-、*、/……

  • 优先级
    (优先级记忆不用担心,因为写代码时一般会使用括号指定优先级,增加可读性)

1、前缀:++,–,一 元:+,-
2、,/,%
3、+,-
4、>>、<<
5、>、<、>=、<=
6、==, !=
7、&
8、^
9、|
10、&&
11、| |
12、=,+=,-=,
=,/=、<<=、>>=、&=、|=、^=
后缀:++,–

2流程控制

该部分同c

2.1分支

三元运算符

<test>? <resultIfTrue> : <resultIfFalse>

if语句

if (<test>)
	<code executed if <test> is true>;

if (<test>)
	<code executed if <test> is true>;
else
	<code executed if <test> is false>;

[效果同三元运算符]

if (varl == 1)
{
	// Do something.
}
else if (varl == 2)
{
	// Do something else.
}
else
{
	// Do something else.
}

switch语句

switch (<testVar>)
{
	case <comparisonVal1>:
	<code to execute if <testVar> == <comparisonVal1> >
	break;
	case <comparisonVal2>:
	<code to execute if <testVar> == <comparisonVal2> >
	break;	
	……
	case <comparisonValN>:
	<code to execute if <testVar> == <comparisonValN> >
	break;
	default:
	<code to execute if <testVar> != <comparisonVals> >
	break;
}

2.2循环

do循环

do
{
	<code to be looped>
}while (<Text>)

while循环

while (<Text>)
{
	<code to be looped>
}

for循环

for (<initialization>; <condition>; <operation>)
{
	<code to loop>
}

无限循环

while (ture)
{}

for (; ;{}

……

3变量知识拓展

3.1类型转换

  • 隐式转换
  • 显式转换
(<destinationType>) <sourceVar>

3.2枚举

  • 定义
enum <typeName><underlyingType>
{
	<value1> = <actualVal1>
	<value2> = <actualVar2>
	……
	<valueN> = <actualVarN>
}

(数据默认为int类型,每个值根据顺序定义【从0开始】)
声明新类型的变量

<typeName> <varName>
  • 赋值:
<varName> = <typeName>.<value>;

3.3结构(体)

  • 定义
struct <typeName>
{
	<memberDeclarations>
}
  • 结构的数据成员
<memberDeclarations>
  • 部分包含变量的声明:
<accessibility> <type> <name>

定义结构类型的变量类似枚举
可以通过句点字符访问这个组合变量中的数据成员

3.4数组

数组的索引是从0算起,如果要查找数组中第三个元素,语法为:myArray[2]

  • 定义:
<baseType> [ ] <name>

(必须在访问之前初始化)
举例

int [] myIntArray = {1, 2, 3};

或,int [] myIntArray = new int[arraySize];
arraySize可以是常数,也可以是变量
或,int [] myIntArray = new int[10] {2, 5, 5};
foreach循环

遍历数组中每一个元素

foreach (<baseType> <name> in <array>)
{
	// can use <name> for each element
}

多维数组
[, ] ;
与一位数组类似,只是使用的时候需要更多的逗号和更多的大括号,数据在内存中顺序存储
eg:double [, ] hillHight = new double[3, 4] {{2, 3 , 4, 5}, {2, 33, 55.7, 5}, {2, 5}};
数组的数组
声明:int[ ] [ ] jaggedIntArray;

  • 初始化:
jaggedIntArray = new int [2] [ ];
jaggedIntArray [0] = new int [3];
jaggedIntArray [0] = new int [2];
//或是下面这样
jaggedIntArray = new int [3] [ ]new int [2] {2, 4}, new int [3] {2, 3,  4},
	new int [1] {5} };
  • 又或声明与初始化放一起
int [ ] [ ] jaggedIntArray = new int [3] [] {
	new int [2] {2, 4}, new int [3] {2, 3,  4},
	new int [1] {5} };
  • foreach实现遍历
foreach (int [ ] sonArray in fatherArray)
{
	foreach (int sprit in sonArray)
	{
		// work;
	}
}

3.5字符串的处理

  • 思路
  • 转为字符数组,(一般)使用封装好的方法操作即可
    1、字符串转字符数组:<stringName>. ToCharArray( ) 【其后方法使用方式一致】,有返回值
    2、获取字符串中字符数:Length( ),有返回值
    3、转为大,小写:ToUpper( ), ToLower( ),有返回值
    4、删除指定字符:Trim(需要的字符),有返回值,默认删除字符串前后空格
    5、删除字符串前后空格:TrimStart( ), TrimEnd( ), 有返回值
    6、在字符串左边或右边添加指定字符至指定长度:PadLeft(<length>,<char> ), PadRight(<length> , <char>), 有返回值,默认添加空格

4函数

static <returnType> <FunctionName>( <type> <typeName>)
{
	// do something.
	return <typeVar>
}

如果是void类型,则之间return; 或省略不写

4.1返回值

如果方法有返回值,在方法体内,所有情况下,都必须有返回内容,否则会报错 “并不是所有的处理路径都用返回值”

针对一行代码的方法,可以使用 表达式体方法(expression-bodied method)处理
如:

static double Multiply (double myVal1, double myVal2)
{
	return myVal1 * myVal2;
}

可改写为:

static double Multiply (double myVal1, double myVal2) => myVal1 * myVal2;

4.2参数

5.调试和错误处理

6.面向对象编程

7.定义类

8.定义类成员

9.集合、比较和转换

10.泛型

11.高级C#技术

To be continue……

转载请注明出处(周更)。

本文地址:https://blog.csdn.net/buyaozhongwe/article/details/107299240