• c#基础知识-2


    1.在控制台接受数据时可以这样输入:

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 
     6 namespace 基础知识2
     7 {
     8     class Program
     9     {
    10         static void Main(string[] args)
    11         {
    12             double a, b, c;
    13             Console.WriteLine("计算两数之和——");
    14             Console.Write("请输入a:");//不用WriteLine,输入后以Enter键结束
    15             a = double.Parse(Console.ReadLine());
    16             Console.Write("请输入b:");
    17             b = double.Parse(Console.ReadLine());
    18             c = a + b;
    19             //{N[,M][:格式化字符串]},M表示输出到变量所占的字符个数
    20             //M为负时按左对齐,为正时按右对齐。
    21             //格式化字符串以Xn的形式指定格式,X指定数字格式,D整形,E科学计数法,F浮点型
    22             //n表示数字精度
    23             Console.WriteLine("a+b={0,5:E4}",c);
    24         }
    25     }
    26 }

    2.if-else可以用条件运算符代替

    3.结构体变量存储在不同位置

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 
     6 namespace 结构体变量
     7 {
     8     class Program
     9     {
    10         struct Student          //声明一个结构体变量,放在Main函数外面
    11         {
    12             public int xh;      //学号;这些成员被称为字段,字段与变量不同,字段可以同时存储很多类型相同的数据
    13             public string xm;   //姓名
    14             public string xb;   //性别
    15             public int nl;      //年龄
    16             public string bh;   //班号
    17         }
    18         static void Main(string[] args)
    19         {
    20             Student s1, s2;//结构体变量s1和s2存储在不同位置
    21             s1.xh = 101; s1.xm = "李明"; s1.xb = "";
    22             s1.nl = 20; s1.bh = "BY161311";
    23             Console.WriteLine("学号:{0},姓名:{1},性别:{2},年龄:{3},班号:{4}",
    24                 s1.xh, s1.xm, s1.xb, s1.nl, s1.bh);
    25             s2 = s1;
    26             s2.xh = 108; s2.xm = "王华";
    27             Console.WriteLine("学号:{0},姓名:{1},性别:{2},年龄:{3},班号:{4}",
    28                 s2.xh, s2.xm, s2.xb, s2.nl, s2.bh);
    29             Console.WriteLine("学号:{0},姓名:{1},性别:{2},年龄:{3},班号:{4}",
    30                 s1.xh, s1.xm, s1.xb, s1.nl, s1.bh);//发现没有改变s2的值
    31         }
    32     }
    33 }

    4.checked可以检查数据有无溢出

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 
     6 namespace 检查溢出
     7 {
     8     class Program
     9     {
    10         static void Main(string[] args)
    11         {
    12             ////表达式仅仅包含常数值,且产生的值在目标值之外则会溢出并编译错误
    13             //int j = int.MaxValue + 10;
    14             //Console.WriteLine(j);
    15             //如果表达式包括一个或多个非常数值,则编译器不检查溢出
    16             int N = 10;
    17             int I = int.MaxValue + N;
    18             Console.WriteLine(I);
    19             checked//这样就可以检查下面代码块是否溢出了
    20             {
    21                 int n = 10;
    22                 int i = int.MaxValue + n;
    23                 Console.WriteLine(i);
    24             }//unchecked取消溢出检查
    25         }
    26     }
    27 }

    5.Convert静态方法将数据类型转换

    其中比较容易忽略的是To.Number

    6.用out关键字 来 返回值

    1 // TryParse returns true if the conversion succeeded
    2 // and stores the result in j.
    3 int j;
    4 if (Int32.TryParse("-105", out j))
    5     Console.WriteLine(j);
    6 else
    7     Console.WriteLine("String could not be parsed.");
    8 // Output: -105

    7.检验代码并抛异常 https://msdn.microsoft.com/en-us/library/bb397679.aspx

    1 try
    2 {
    3     int m = Int32.Parse("abc");
    4 }
    5 catch (FormatException e)
    6 {
    7     Console.WriteLine(e.Message);
    8 }
    9 // Output: Input string was not in a correct format.

    8.if-else语句,单独的if-else语句可以用 “条件表达式?满足则执行此句:不满足则执行此句”条件运算符。

    嵌套的if-else中的else总是和最后一个出现的还没有else与之配对的if匹配。

    if……else if……else if……else语句适合于区间判断,比如根据分数判断等级。

    9.switch语句也成为开关语句,适合于判断定值多重选择情况。

    1             switch (表达式)
    2             {
    3                 case 常量表达式1:语句1;
    4                 case 常量表达式1:语句1;
    5                     ……
    6                 default:
    7             }
     1             string ch = Console.ReadLine();
     2             switch (ch)
     3              {
     4                 case 'm':case 'M':case  'w': case 'W'://每个case块和default块后都需有break,除非case块后没有语句。
     5                 Console.WriteLine("8学分");
     6                 break;
     7                 case 'p':case 'P':case  'c': case 'C'://当满足四个条件中的一个时就会执行输出6学分的语句。
     8                 Console.WriteLine("6学分");
     9                 break;
    10                 default :
    11                 Console.WriteLine("输入的代号不正确");
    12                 break;
    13             }

    10.数组的定义

    在某些情况下我们为了将文本文档中规则数列的数字放入数组中,那么我们就需要事先定义出数组,这个数组长度为多少呢,我们可以通过读取文档的行数n,然后根据n来定义数组:

    1             int n = 5;//定义变量n,比如可以为文档当行数
    2             int[] myarr = new int[n];//指定数组myarr的长度为n

    数组的动态初始化就是有new关键字,new构建的时候可以先调用构造函数。没有new的定义为静态定义,静态定义时,定义和初始化要放一块。

     1             //定义一维数组并静态初始化
     2             int[] c = {1,2,3 };//正确
     3             ////下面将定义和静态初始化分开是错误的。
     4             //int[] c;
     5             //c={1,2,3};
     6 
     7             //写了数组长度,给初值时要给出全部元素初值,多给少给都不行
     8             int[] b = new int[10] { 1,2,3,4,5,6,7,8,9,10};
     9              
    10             int n = 5;//定义变量n,比如可以为文档当行数
    11             //int[] myarr = new int[n];//指定数组myarr的长度为n,正确
    12             int[] myarr = new int[n] {1,2,3,4,5 };//错误,静态初始时前面长度要给常量而非变量

    二维数组:

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 
     6 namespace 二维数组
     7 {
     8     class Program
     9     {
    10         static void Main(string[] args)
    11         {
    12             int[,] myArr1=new int[2,3]{{1,2,3},
    13                                       {4,5,16}};
    14             //也可以省略数组长度
    15             int[,] myArr2 = new int[,]{{1,2,3},
    16                                       {4,5,6}};
    17             //foreach访问myArr1元素
    18             foreach (var item in myArr1 )
    19             {
    20                 Console.WriteLine(item );       
    21             }
    22             //for访问myArr2
    23             Console.WriteLine("myArr2:");
    24             for (int i = 0; i < myArr2 .GetLength (0); i++)
    25             {
    26                 for (int j = 0; j < myArr2.GetLength (1); j++)
    27                 {
    28                     Console.Write("myArr2[{0},{1}]={2}	",i,j,myArr2 [i,j]);
    29                 }
    30                 Console.WriteLine(); 
    31             }
    32             //二维数组的静态初始化
    33             int[,] b = { { 1, 2, 3 }, { 4, 5, 6 } };
    34         }
    35     }
    36 }

    11.交错数组:交错数组的每一个子组都是独立的多维数组,每个子组可以有不同的长度,为数组的每一维使用一对中括号。

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 
     6 namespace 交错数组
     7 {
     8     class Program
     9     {
    10         static void Main(string[] args)
    11         {
    12             int[][] a = new int[3][];//定义了一个包含三个独立子数组的交错数组
    13             //下面初始化该元素,必须初始化a的元素后才能使用
    14             a[0] = new int[5];//交错数组a的每个元素都是一个一维整数数组
    15             a[1] = new int[4];//第2个元素是有4个整数 组成的数组
    16             a[2] = new int[2];//在内存中,a变量名存放在栈中,堆中又有空间存放a[0],a[1],a[2]变量名,a[0]等分别指向对应的一维数组
    17             //也可以如下初始化
    18             a[0] = new int[] { 1, 3, 5, 7, 9 };
    19             a[1] = new int[] { 0, 2, 4, 6 };
    20             a[2] = new int[] { 11, 12 };
    21             //还有速记格式
    22             int[][] b1 = new int[][] { new int[] { 1, 3, 7, 9 }, new int[] { 0, 2, 4, 6 }, new int[] { 11, 12 } };
    23             ////注意 不能在交错数组的初始化中指定除顶层数组之外的数组大小
    24             //int [][] b2=new int[3][4];//错误
    25             //应该为
    26             int[][] b3 = new int[3][];
    27 
    28             //访问交错数组中的元素
    29             Console.WriteLine(a[0][1]);//输出3
    30             for (int i = 0; i < b1.Length; i++)
    31             {
    32                 Console.Write("d({0}):", i);
    33                 for (int j = 0; j < b1[i].Length; j++)
    34                 {
    35                     Console.Write("{0}	", b1[i][j]);
    36 
    37                 }
    38                 Console.WriteLine();
    39             }
    40         }
    41     }
    42 }
    
    
  • 相关阅读:
    C winpcap 网络抓包 并获取IP TCP 协议的相关信息
    python基础
    k8s部署测试实例
    node节点的部署
    k8s集群之master节点部署
    k8s集群之Docker安装镜像加速器配置与k8s容器网络
    k8s集群部署之环境介绍与etcd数据库集群部署
    docker存储管理
    docker 镜像仓库的安装与使用
    docker 镜像管理
  • 原文地址:https://www.cnblogs.com/zhubinglong/p/5855992.html
Copyright © 2020-2023  润新知