• C#和.Ne学习第二天


    (主要是传智播客的赵剑宇老师上课的笔记,自己在加一点实例,和自己的理解,在此感谢赵剑宇老师)

    1、注释符
    作用:
    1)、注销
    2)、解释
    2、C#中3种注释符
    1):单选注释//注释的内容
    例:

    //Console.WriteLine("hello");

    2):单行注释/*注释的内容*/
    例:

    1 /*Console.WriteLine("hello");
    2 Console.ReadKey();*/

    3):文档注释///多用来解释类或得方法
    例:

     1 /// <summary>
     2 /// 获取一个最大值
     3 /// </summary>
     4 /// <param name="t1">第一整型变量</param>
     5 /// <param name="t2">第二整型变量</param>
     6 /// <returns>返回一个最大的变量</returns>
     7 public static int GetMax(int t1, int t2)
     8 {
     9 return t1 > t2 ? t1 : t2;
    10 }

    3、VS中的常用快捷键:
    Ctrl+K+D:快速对齐代码
    注意:代码中出现语法错误无法对齐,比如说少了;。
    Ctrl+Z:撤销
    Ctrl+S:保存
    Ctrl+J:快速弹出智能提示
    Home:移动光标到该行文本的最前面
    End:移动光标到该行文本的最后面
    Shift+Home;选中光标的后面的一段文字
    Shift+End:选中光标的前面一段文字
    Ctrl+Home:跳转光标到当前文本最顶部
    Ctrl+Home:跳转光标到当前文本最底部
    Ctrl+K+C:注释所选代码
    Ctrl+K+U:取消对所选代码的注释:
    F1:转到帮助文档
    #region和#endregion:折叠冗余代码
    作用:
    折叠代码(注意可以折叠注释过的代码,也可以注释没有注释过的代码)
    例:

    1 #region 
    2 Console.WriteLine("hello");
    3 Console.WriteLine("hello");
    4 Console.WriteLine("hello");
    5 Console.WriteLine("hello");
    6 Console.WriteLine("hello");
    7 Console.WriteLine("");
    8 #endregion

    效果:


    可以为折叠添加文字解释
    例:

    1 #region 折叠 
    2 Console.WriteLine("hello");
    3 Console.WriteLine("hello");
    4 Console.WriteLine("hello");
    5 Console.WriteLine("hello");
    6 Console.WriteLine("hello");
    7 Console.WriteLine("");
    8 #endregion

    效果:


    4、
    存储变量的语法:
    变量类型 变量名;
    变量名=值;

    声明并且给变量赋值的简写形式:
    变量类型 变量名=值;

    "="在这并不表示等于的意思,而是赋值的意思,表示把等号右边的值赋值给等号左边的变量。
    "=="
    在这表示等于的意思,多用于判断两数是否相等。

    5、数据类型
    1)、整数类型:int 只能存储整数,不能存储小数。
    2)、小数类型:double 既能存储整数,也能存储小数,小数点后面的位数 15~16位。
    3)、金钱类型:decimal:用来村粗金钱,值后面需要加上一个后缀m或M
    例:decimal d = 1000m;
    decimal d = 1000M;
    4)、字符串类型:string,用来存储多个文本,也可以存储空,字符串类型的值需要被 双引号引来,
    这个双引号必须是英文半角状态下的双引号,String和string功能都一样。
    5)、字符类型:char,用来存储单个字符,最多、最少只能有一个字符,不能存储空。
    字符类型的值需要用 单引号因起来。英文半角状态下的单引号。

    6.波浪线
    1)、红色波浪线:
    代码中出现了语法错误
    2)、绿色的波浪线: 也称警告线,说明你的代码语法并没有错误。只不过提示你有可能会出现错误,但是不一定会出现错误。

    7、变量的使用规则
    如果你要使用变量的话,应该要先声明再赋值再使用。

    8、命名规则:
    1)、必须以“字母”_或@符号开头,--不要以数字开头
    2)、后面可以跟任意“字母”、数字、下划线
    注意:
    1)、起的变量名不要与C#系统中的关键字重复
    2)、在C#中,大小写是敏感的
    3)、同一个变量名不允许重复定义(先这么认为,不严谨)
    两个命名规范:
    1、Camel骆驼命名规范,要求变量名道单词的道字母要小写,其余每个单词的首字母要大写。
    多用于给变量命名
    2、Pascal命名规范:要求每个单词的首字母都要大写,其余字母小写,多用于给类或方法命名。

    8、赋值运算符
    =:表示赋值的意思,表示把等号右边的值,赋值给等号左边的变量。
    由等号连接的表达式称之为赋值表达式。
    注意:每个表达式我们都可以求解除一个定值,对于赋值表达式而言,等号左边的变量的值,就是整个赋值表达式的值。
    int number=10;

    9、+号的作用
    1)、连接
    +号两边有一边是字符串的时候
    2)、相加
    +号两边没有一边是字符串的时候

    练习1


    代码

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 using System.Threading.Tasks;
     6 
     7 namespace ConsoleApplication2
     8 {
     9     class Program
    10     {
    11         static void Main(string[] args)
    12         {
    13             string name = "卡卡西";
    14             string address = "火影村";
    15             int age = 30;
    16             string email = "kakaxi@qq.com";
    17             decimal wage = 2000m;
    18             Console.WriteLine("我叫"+name+
    19                 ",我住在"+address+
    20                 ",我今年"+age+
    21                 "邮箱是"+email+
    22                 "我的工资"+wage
    23                 );
    24         }
    25 
    26     }
    27 }
    1
     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 using System.Threading.Tasks;
     6 
     7 namespace ConsoleApplication2
     8 {
     9     class Program
    10     {
    11         static void Main(string[] args)
    12         {
    13             int age = 18;
    14             age = 81;
    15             Console.WriteLine("年龄是"+age+"岁!");
    16         }
    17 
    18     }
    19 }
    2

    10、占位符
    使用方法:先挖个坑,再填个坑。
    注意:
    1、你挖了几个坑,如果你多填没有效果,但语法不会的错。
    2、输出顺序:按照挖顺序输出
    例(正常):

    View Code

    输出结果:
    第一个数:10,第二个数:20,第三个数:30
    请按任意键继续. . .
    例(特殊):

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 using System.Threading.Tasks;
     6 
     7 namespace ConsoleApplication2
     8 {
     9     class Program
    10     {
    11         static void Main(string[] args)
    12         {
    13             int n1 = 10;
    14             int n2 = 20;
    15             int n3 = 30;
    16 
    17             Console.WriteLine("第一个数:{0},第二个数:{2},第三个数:{1}", n1, n2, n3);
    18         }
    19 
    20     }
    21 }
    View Code

    输出结果:
    第一个数:10,第二个数:30,第三个数:20
    请按任意键继续. . .

    1

    结果:
    我的姓名是007,性别:男,年龄:21,电话:1314520
    请按任意键继续. . .

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 using System.Threading.Tasks;
     6 
     7 namespace ConsoleApplication2
     8 {
     9     class Program
    10     {
    11         static void Main(string[] args)
    12         {
    13             string address = "安徽省合肥市";
    14             string age = "21";
    15             string name = "FangHaiFeng";
    16 
    17             Console.WriteLine("我家在{0},今年{1}岁,我的姓名是{2}", address, age, name );
    18         }
    19 
    20     }
    21 }
    2

    结果:
    我的姓名是007,性别:男,年龄:21,电话:1314520
    请按任意键继续. . .
    3
    代码3.1

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 using System.Threading.Tasks;
     6 
     7 namespace ConsoleApplication2
     8 {
     9     class Program
    10     {
    11         static void Main(string[] args)
    12         {
    13             int number1 = 10;
    14             int number2 = 5;
    15 
    16             int t = number1;
    17             number1 = number2;
    18             number2 = t;
    19 
    20             Console.WriteLine("number1:{0},number2:{1}", number1, number2);
    21         }
    22 
    23     }
    24 }
    View Code

    结果
    number1:5,number2:10
    请按任意键继续. . .
    代码3.2

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 using System.Threading.Tasks;
     6 
     7 namespace ConsoleApplication3
     8 {
     9     class Program
    10     {
    11         static void Main(string[] args)
    12         {
    13             int number1 = 10;
    14             int number2 = 5;
    15 
    16             number1 = number1 - number2;
    17             number2 = number1 + number2;
    18             number1 = number2 - number1;
    19 
    20             Console.WriteLine("number1:{0},number2:{1}", number1, number2);
    21         }
    22     }
    23 }
    View Code

    结果:
    number1:5,number2:10
    请按任意键继续. . .

    11、异常
    异常是指:语法并没有任何错误,只不过在运行期间,由于某些原因出现问题,使程序不能正常运行。

    练习3

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 using System.Threading.Tasks;
     6 
     7 namespace ConsoleApplication4
     8 {
     9     class Program
    10     {
    11         static void Main(string[] args)
    12         {
    13             string fruit;
    14             Console.WriteLine("你喜欢吃什么水果?");
    15             fruit = Console.ReadLine();
    16             Console.WriteLine("哈哈,这么巧,我也喜欢吃{0}。", fruit);
    17         }
    18     }
    19 }
    1

    结果:
    你喜欢吃什么水果?
    苹果
    哈哈,这么巧,我也喜欢吃苹果。
    请按任意键继续. . .

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 using System.Threading.Tasks;
     6 
     7 namespace ConsoleApplication4
     8 {
     9     class Program
    10     {
    11         static void Main(string[] args)
    12         {
    13             string name;
    14             string gender;
    15             string age;
    16 
    17             Console.WriteLine("请输入你的名字:");
    18             name = Console.ReadLine();
    19             Console.WriteLine("性别:");
    20             gender = Console.ReadLine();
    21             Console.WriteLine("年龄:");
    22             age = Console.ReadLine();
    23 
    24             Console.WriteLine("您好:{0},您的年龄是{1}是{2}生。", name,age, gender);
    25         }
    26     }
    27 }
    2

    结果:
    请输入你的名字:
    大日如来
    性别:

    年龄:
    999999
    您好:大日如来,您的年龄是999999是男生。
    请按任意键继续. . .

    12、转义符
    转义符指的就是一个‘’+一个特殊的字符,组成了一个具有特殊意义的字符。
    :换行
    “:表示一个英文”
    :制表符和tab键功能相同
    :与键盘上Backspace功能相同,放在字符串两端没有效果。
    :windows系统不认识 ,只认识 。
    \:表是一个

    @符号
    1、取消在字符串中的转义作用,使基单纯的表示为一个
    例:
    报错:
    Console.WriteLine("acd");
    添加@正确:
    Console.WriteLine(@"acd");
    2、保留原格式输出
    例:
    报错:
    Console.WriteLine("人有悲欢离合
    月有阴晴圆缺");
    加@符号正确:
    Console.WriteLine(@"人有悲欢离合
    月有阴晴圆缺");
    结果:
    人有悲欢离合
    月有阴晴圆缺
    请按任意键继续. . .
    注意:
    如果在 月有阴晴圆缺前面有空格的话也许打印空格。

    13、算数运算符
    + 加
    - 减
    * 乘
    / 除
    % 取余
    优先级:先乘除,后加减,有括号先算括号里的,相同级别的从左至右运算。
    练习4:

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 using System.Threading.Tasks;
     6 
     7 namespace ConsoleApplication5
     8 {
     9     class Program
    10     {
    11         static void Main(string[] args)
    12         {
    13             int a = 100;
    14             int b = 20;
    15 
    16             Console.WriteLine("{0}", a+b);
    17         }
    18     }
    19 }
    1

    输出结果:
    120
    请按任意键继续. . .

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 using System.Threading.Tasks;
     6 
     7 namespace ConsoleApplication5
     8 {
     9     class Program
    10     {
    11         static void Main(string[] args)
    12         {
    13             int r = 5;
    14             double pi = 3.14;
    15             double area = pi * r * r;
    16             double perimeter = 2 * pi * r;
    17             Console.WriteLine("面积:{0},周长:{1}。", area, perimeter);
    18         }
    19     }
    20 }
    2

    输出结果:
    面积:78.5,周长:31.4。
    请按任意键继续. . .

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 using System.Threading.Tasks;
     6 
     7 namespace ConsoleApplication5
     8 {
     9     class Program
    10     {
    11         static void Main(string[] args)
    12         {
    13             int t_shirt = 35;
    14             int trousers = 120;
    15 
    16             int sum = 3 * t_shirt + 2 * trousers;
    17             Console.WriteLine("应该付{0}元", sum);
    18         }
    19     }
    20 }
    3

    输出结果:
    应该付345元
    请按任意键继续. . .

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 using System.Threading.Tasks;
     6 
     7 namespace ConsoleApplication5
     8 {
     9     class Program
    10     {
    11         static void Main(string[] args)
    12         {
    13             int t_shirt = 35;
    14             int trousers = 120;
    15 
    16             double sum = (3 * t_shirt + 2 * trousers)*0.88;
    17             Console.WriteLine("应该付{0}元", sum);
    18         }
    19     }
    20 }
    4

    输出结果:
    应该付303.6元
    请按任意键继续. . .

    14、类型转换
    隐式类型转换(自动类型转换):
    我们要求等号两遍参与运算的操作数的类型必须一致,如果不一致,满足下列条件会发生
    自动类型转换,或者称之为隐式类型转换。
    两种类型兼容
    例如:int 和 double 兼容(都是数字类型)
    目标类型大于源类型
    例如:double > int 小的转大的
    例:
    int number = 10;
    double d = number;


    显示类型转换(强制类型转换):
    1、两种类型相兼容 int--double
    2、大的转成小的 double----int
    语法:
    (待转换的类型)要转换的值;
    例:
    double d = 10.6;
    int number = (int)d;

    总结:
    自动类型转换:int---->double
    显示类型转换:double--->int

    课后练习:

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 using System.Threading.Tasks;
     6 
     7 namespace ConsoleApplication7
     8 {
     9     class Program
    10     {
    11         static void Main(string[] args)
    12         {
    13             int days = 46;
    14             int week = days / 7;
    15             int day = days % 7;
    16 
    17             Console.WriteLine("{0}天是{1}周零{2}天。", days, week, day);
    18             Console.ReadKey();
    19         }
    20     }
    21 }
    1

    输出结果:
    是6周,零4天。
    请按任意键继续. . .

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 using System.Threading.Tasks;
     6 
     7 namespace ConsoleApplication7
     8 {
     9     class Program
    10     {
    11         static void Main(string[] args)
    12         {
    13             int second = 107653;
    14             int day = second/(60*60*24);
    15             int hour = second%(60*60*24)/(60*60);
    16             int minute = second % (60 * 60) / 60;
    17             int second2 = second % 60;
    18 
    19             Console.WriteLine("{0}秒是{1}天{2}小时{3}分钟{4}秒", second, day, hour, minute, second2);
    20             Console.ReadKey();
    21         }
    22     }
    23 }
    2

    输出结果:
    107653秒是1天5小时54分钟13秒
    请按任意键继续. . .

  • 相关阅读:
    react 全局监听报错,监听白屏
    【Python】(八)Python中的set集合(每个人都是唯一的个体)
    【Python】(六)Python数据类型-列表和元组,九浅一深,用得到
    mybatis-spring多数据源配置
    自己动手实现springboot运行时新增/更新外部接口
    自己动手实现springboot运行时执行java源码(运行时编译、加载、注册bean、调用)
    自己动手实现java断点/单步调试(一)
    自己动手实现java断点/单步调试(二)
    Swift 模式下面LLDB 输出对象
    Understanding Swift’s value type thread safety
  • 原文地址:https://www.cnblogs.com/2016Study/p/5454441.html
Copyright © 2020-2023  润新知