C#学习笔记(二)

控制台应用程序:

using 使用名称空间

 

 

 

 web应用程序:

 输出:Consle,WriteLine(打印到控制台内容);

输出,输入变量方式:

string name = "lisi";
int age = 20;
Console.WriteLine($"姓名:{name},年龄:{age}");$

Consle.ReadLine() 作用:输出后暂停;获取控制台输入的值

变量命名方式:

1,帕斯卡命名法:多个单词组成的变量名,方法名,首字母大写,其他字母小写,常用类名,属性,方法名,接口等 如 GetName

2,驼峰命名法:多个单词组成的变量名等,首个单词字母小写,其他单词首字母大写,常用方法内定义的变量,字段等 如:userName

占位符的使用:

            Console.Write("请输入姓名:");
            string name = Console.ReadLine();
            Console.Write("请输入年龄:");
            int age = Convert.ToInt32(Console.ReadLine());  //数据类型的转换
            string message = $"今天早上遇到了{name}很高兴,他{age}岁";
string message1=$"今天早上遇到了{{{name}}}很高兴,他{{{age}}}岁"; Console.WriteLine(message);
Console.WriteLine(message1); //输出的信息带{} Console.WriteLine(
"press any key to exit"); Console.ReadLine();

数据类型字符串转其他类型的转换的几种方式:

1,隐式转换:默认如 int a=0; double b=a;,转换前类型的存储空间小于转换后的类型的存储空间

2,显式转换(强行转换):必须是兼容性转换,

       强制转换:类型 变量名 = (类型) 变量名

 类型可转换为
byte sbyte,char
sbyte byte,ushort,uint,ulong,char
short sbyte,byte,ushort,uint,ulong,long,char
ushort sbyte,byte,short,char
int sbyte,byte,short,ushort,uint,ulong,char
uint sbyte,byte,short,ushort,int,char
long sbyte,byte,short,ushort,uint,int,ulong,char
ulong sbyte,byte,short,ushort,uint,int,long,char
float sbyte,byte,short,ushort,uint,int,ulong,long,char,float
char sbyte,byte,short
decimal sbyte,byte,short,ushort,uint,int,ulong,long,char,float,double
double sbyte,byte,short,ushort,uint,int,ulong,long,char,float,decimal

 

 

 

 

 

 

 

 

 

 

 3,字符串转换为其他类型:

(1)int m = Int32.Parse(string n),可能会转换失败而导致系统崩溃,例如int.Parse(11m)

(2)bool m= Int32.TryParse(string ,out  n)  ,如果m为true,则将string类型的n成功转换为int类型的n

4,任意类型之间的转换:Convert.Toxxx()方法

数值类型

方法

decimal

Convert.ToDecimal(String)

float

Convert.ToSingle(String)

double

Convert.ToDouble(String)

short

Convert.ToInt16(String)

long

Convert.ToInt64(String)

ushort

Convert.ToUInt16(String)

uint

Convert.ToUInt32(String)

ulong

Convert.ToUInt64(String)

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

posted on 2019-09-19 22:42  不愧下学  阅读(179)  评论(0编辑  收藏  举报

导航