C#基本数据类型和流程控制语句

 

1.类型划分:

基本分为值类型和引用类型;值类型相对熟悉,引用类型为类、接口、数组、委托。

2.二者的区别:值类型直接在堆栈中保存变量的值,引用类型保存到是对象的引用地址。

3.常量与变量:

const int pi = 3.1415927  //const声明的常量

//利用var声明未知的类型

var key = Console.ReadKey():
//或者
ConsoleKeyInfo key = Console.ReadKry();

4.运算符与表达式:

(1)圆括号:运算顺序;显式转换;方法或委托

(2)方括号:数组或索引;特性;指针;

(3)new 运算:创建

(4)自增自减;

(5)赋值运算:=、+=、-=、*=、/=、%=........

(6)关系运算:比大小之类的

(7)条件运算:与或运算

(8)??运算:

int j = i ??0;    //如果i不是null,则j=i,否者,j=0;

(9)?三目运算

(10)逻辑运算:与(&)、或(|)、非(!)、异或(^)

(11)typedef 运算符:获取System.Type 对象,

System.Type type = typedef(int);

(12)is运算符:课本P36

(13)as运算符:

 

 

5.简单类型

(1)整型:整型在C#用来表示定点数

(2)浮点型:float;double;decimal;

(3)布尔型bool

(4)字符型char

(5)枚举型(enum):

public enum Mycolor{Red =1,Green=1,bule=2}; //基础类型是int枚举类,直接赋值

public enum Mycolor{Red ,Green,bule};   //效果同上,自动赋值0,1,2

public enum Number :byte{x1 =254,x2}; 

(6)可空类型:

 

 

6.字符串

(1)创建方式:

string str1 = "ABCD";

string str2 = mystr1;

int i = 3 ; 

string str3 = str1 +str2;

string str4 = str3 +i;  //int 型变量可以直接变成string进行加减操作

//利用new进行构造

string s2 = new string('a',4);  // 结果为aaaa

string s  = string.Format("{0,30}",' ');    //s为30个空格

string s1 = string.Format("{0,20}","abc");   //s1为左对齐的长度为20的字符串



//对于转义字符的操作:


string filePath = "C:\\Csharp\\MyFile.cs";

string filePath = @"C:\Csharp\Myfile.cs";    //在前面加@,字符串内均不转译


(2)字符串的常用操作:

    比较:string.Compare(string s1,string s2);                  //s1>s2 返回 1,s1=s2 返回 0 ,s1<s2 返回 -1

     s1 == s2 结果为 true 或者 false

     查找·:

//Contains方法:

if(s1.Contains("abc")) Console.write("abc exists in s1");

//StartsWith 和 EndsWith 方法   从开头或者结尾开始查找,并返回bool值

string s1 = "this is a string.";

Console.WriteLine(s1.StartsWith("this"));   //true;

Console.WriteLine(s1.EndsWith("ing"));    //false

//indexof方法

//public int IndexOf(string s);

string s = "123765f6ds75f656d";

int x = s1.IndexOf("f6");   //返回值为6

//indexOfAny方法   查找一个字符串里是否包含某些字符 返回第一个匹配项的位置

char []c ={'a','b','3','8'};

string s2 = "u489uhfreiu2ht74832hu4ih3261t`1";

int x = s2.InedxOfAny(c);     //值为2


//Substring 方法

string s1 = "abc123";

string s2  = s1.Substring(2);    // 从第二个开始,取到末尾

string s3 = s1.Substring(2,3);    //从第二个开始,取3个

字符串插改删:

//Insert方法插入字符串value

public string Insert (int startIndex , string value);  //从startInsex开始插入value

//Remove 方法

public string Remove(int startIndex);   //删除从startIndex开始到结尾

public string Remove(int startIndex, int length); //删除从s开始长度为l的字符串

//Replace 方法

public string Replace(char oldcChar, char newChar);

public String Repalce(string oldValue , string newValue);

  

 

7.数组

一维数组;多维数组;交错数组(广义表)

数组常用操作:求值;统计;排序;查找;复制

string [ ] sArray1 = { "123" ,"456" , "abc" } ;
 
string s1 = string.Join("," ,sArray1);   //结果为"123,456,abc"

string[ ] sArray2 = s1.Split(',');   //得到结果与SArray1 相同

string s2 = "abc 12;34,56";

string [ ] sArray3 =s2.Split(',',';',' '); //分隔符为逗号,分号,空格

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值