zoukankan      html  css  js  c++  java
  • 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)

  • 相关阅读:
    idea 缺失右侧maven窗口
    SpringCloud
    Java面试题——Java基础
    json对象、json字符串的区别和相互转换
    java中的 private Logger log=Logger.getLogger(this.getClass());
    http网络编程
    ansible和helm
    template模板
    http中get和post请求方式
    session和cookie
  • 原文地址:https://www.cnblogs.com/lq13035130506/p/11553095.html
Copyright © 2011-2022 走看看