C#小知识整理一

C#小知识整理一

1.char表示一个字符,不能'' char a='',不能'ab' char a='ab' 出错提示字符串文本中的字符太多汉字也是一个字符char char a='张'.

2.测试int的最大值和最小值

Console.WriteLine(int.MaxValue); Console.WriteLine(int.MinValue);

Console.WriteLine(Double.MaxValue); .

Net 类名中的最大值 Console.WriteLine(double.MaxValue); .Net 数据类型的最大值

3.long--int64位 int--int32位

4.转义符 \ 只管后面的一个 \n 而不是\nn 如果想这样写要\n\n \n回车符号

\表示不要把\后的"当做字符串的开始或结尾,而是就当做"来看

string a="tommbox\"shan"; tommbox\"shan

\n表示回车字符,看起来是两个,其实是一体的 string a="to\nname";

to

name

string a="to\\m"; \\表示自己转义自己,而不能写成string a="to\m";

如果想写的方便些可以在字符串的前面加上@,string a=@"to\m"; @只跟\转义符有关

一旦要表示"就不能加上@ string a=@"tom\"gao";而应该写成string a="tom\"gao";

string a="tom\"gao"; 内存中保存的是tom"gao即处理后保存在内存中。

string a = Console.ReadLine();

Console.WriteLine(a);//转义符只用在C#代码中,而在程序中直接扫描。

转义符的长度,其中 string s = "gao\nshan";Console.WriteLine(s.Length);

长度为8 回车换行也属于一个字节。

5.string a="hellow" 在C#字符串的长度是5,而在VC++,里面的长度是6,(\0结束标志在C#存在但不考虑)。

6.隐式转换 显式转换

(1)char 的最大值和最小值 char对应的Ascii的值 a 97

Console.WriteLine(char.MaxValue); ?

Console.WriteLine(char.MinValue);

(2)char与int的转换

隐式转换:

小的字节类型可以在大的字节类型里隐式转换。

int a='c'; 把c的Ascii赋值给a

char a = 'b';

int i = a;

Console.WriteLine(i); 输出的是对应元素的Ascii (98)

显式转换:

char a=(char)5; 把5对应的Ascii赋值给a

char j = (char)65;

Console.WriteLine(j);

Console.ReadKey(); 输出的是对应元素的字符 (A)

7.类型转换

(1)string——int

string a = "10";

int s = Convert.ToInt32(a);

Console.WriteLine(s);

(2)int——string

int b = 50;

string s1 = b.ToString();

Console.WriteLine(s1);

(3)Console.WriteLine(Convert.ToInt32(Console.ReadLine()) + Convert.ToInt32(Console.ReadLine()));

8.命名规则

C#命名必须以字母或者下划线_开头,

后面可以是任意字母,数字或下划线_,但不能是关键字(如class,new,int 在VS中蓝色的为关键字)。

int a3=9;

int _a3=9;

9.变量

变量在声明后才能使用

int i3=10;

Console.WriteLine(i3);

变量在使用之前必须赋值

int i1; i1 = 10; Console.WriteLine(i1);

10.运算符

(1)整除求余

int i = 3;

int a = 5;

Console.WriteLine(i /a); 0

Console.WriteLine(i%a); i的值

(2)运算优先级 多括号的由内向外进行运算

int i = 1;

int a = 3;

int s = i + a * 5;

Console.WriteLine(s);

11.bool的是否运算

(1)bool b=(i==9);

int i = 5;

bool b=(i==9);

Console.WriteLine(b);

(2)b = !(i > 9);

int i = 5;

bool b=true;

b = !(i > 9);

Console.WriteLine(b);

(3)C#中没有判断True 1 False 0 必须自己写程序进行判断。

(4)&&与||

&&必须同时满足两个条件才为真

||只要满足一个条件即为真

12.

if 语句 如果下述情况的话,if执行空语句,没有任何值,所以接着往下执行。

if (false) { }; //规范的空语句执行格式

Console .WriteLine("hellow");

Console .ReadKey();

则输出 hellow

执行完if的条件,则不需要再去执行else的语句。

13.课后练习

提示用户输入用户名,然后再提示输入密码,如果用户名是“admin”并且密码是“888888”,

则提示正确,否则提示错误,如果用户名不是admin还提示用户用户名不存在

Console.WriteLine("请输入您的用户名");

string name = Console.ReadLine();

Console.WriteLine("请输入您的密码");

string pwd = Console.ReadLine();

if (name != "admin")

{

Console.WriteLine("该用户不存在");

}

else if(name=="admin")

{

if (pwd == "888888")

{

Console.WriteLine("恭喜您,登录成功!!!");

}

else

{

Console.WriteLine("登录错误,请重新登录");

}

}

14.switch() case后面要跟着break,除非第一个和第二个case合并。

string s = Console.ReadLine();

switch (s)

{

case "1": //如果想并列书写格式为: case "1": case"11": case "111":

Console.WriteLine("东");

break;

case "2":

Console.WriteLine("南");

break;

case "3":

Console.WriteLine("西");

break;

case "4":

Console.WriteLine("北");

break;

default:

Console.WriteLine("输入错误"); break; }

15.连续输入一些数,当end时输出最大值。

①能够连续输入字符,无限循环下去

②如果输入的是某一个字符串时,return退出while循环 ③否则就进行其他的处理

int max = 0;

while (true)

{ string s = Console.ReadLine();

if (s == "end")

{

Console.WriteLine("最大的值是{0}",max);

Console.ReadKey();

return;

}

else {

int a = Convert.ToInt32(s);

if (a> max)

{

max = a;

}

}

}

16.while 中的

(1) return break 退出while循环,不再执行 输出"gao", "zhang"

string[] strs = { "gao", "zhang", "wang", "tao" };

foreach (string s in strs)

{

if (s == "wang")

{

Console.ReadKey();

return;

}

Console.WriteLine(s);

}

(2) continue 退出本次while循环,执行下次循环 输出"gao", "zhang","tao"

string[] strs = { "gao", "zhang", "wang", "tao" };

foreach (string s in strs)

{

if (s == "wang")

{ continue; } Console.WriteLine(s); }

17.for循环的执行次序

int s = 0;

for (int i=0; ①

i <= 10; ②

i++)⑤

{

s += i;③

Console.WriteLine(i);④

}

18.枚举enum:和class同级,确定数量、确定值的,变量

enum dir{东风,南京,西北,北京} //设置个确定的枚举

dir d1 = dir.东风; //d1接收枚举里的东风

Console.WriteLine(d1); //输出d1的值

19.数组:

(1)声明的同时赋值

int[] nums={3,9,15,4};

(2)只是声明数组,没有赋值

int[] nums=new int[3]; 3表示的是长度而不是下标 0 1 2

int i1=nums[2]; //去数组为2的值

(3)不能访问超过范围的数组,可以访问未定义的数组,默认的为0

int[] nums = { 0, 15, 22, 50, 9 };

int i1 = nums[2];

Console.WriteLine(i1);

Console.ReadKey();

(4)数组的遍历

int[] nums = { 0, 15, 22, 50, 9 };

for (int i = 0; i < nums.Length; i++)

Console.WriteLine(nums[i]);

Console.ReadKey();

20.数组顺序颠倒 先在一半进行重新排序,然后再输出

string[] strs = { "gao", "haha", "10", "20" };

string temp = "";

for (int i = 0; i < strs.Length/2; i++)

{

temp = strs[i];

strs[i] = strs[strs.Length - 1 - i];

strs[strs.Length - 1 - i] = temp;

}

for (int i = 0; i < strs.Length;i++ )

Console.Write(strs[i]);

21.数组 //每个新数组的启始下标,如数组1 0~n 数组2 0~n 那么数组3的第二次相连

就是原前面数组的长度+本身自增的长度

int[] num1 = { 10, 20, 30, 40 };

int[] num2 = { 90, 80, 70, 60, 50 };

int[] num4 = { 90, 80, 70, 60, 50 };

int[] num3=new int[num1.Length +num2 .Length+num4.Length ];

for (int i = 0; i < num1.Length; i++)

num3[i] = num1[i];

for (int i = 0; i < num2.Length; i++)

num3[num1.Length + i] = num2[i];

for (int i = 0; i < num4.Length; i++)

num3[num3.Length -num4 .Length + i] = num2[i];

for (int i = 0; i < num3.Length; i++)

Console.WriteLine(num3[i]);

Console.ReadKey();

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值