• C#编程语言简介



    一 关于 C# 及 .NET


    1、发展史及作用

    发展史:
        2002年2月 C#1.0    .NET1.0
        2003年4月 C#1.1    .NET1.1
        2005年10月 C#2.0  .NET2.0  
        2007年8月 C#3.0    .NET3.0

    作用:

      1)Windows 窗口应用程序
      2)Web应用程序
      3)网络数据库等应用程序
      4)Web 服务等各种分布式应用程序


    2、控制台的输入/输出

    输出方式:
       Console.WriteLine("字符串常量");
       Console.WriteLine("常量");
       Console.WriteLine("字符串{0},{1},{2}",表达式1,表达式2,表达式3);

    输入:
      string name=Console.ReadLine();


    3、注释

      1)单行://
      2)多行:/* */
      3)文档:///

    4、相关练习

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Data.SqlClient;

    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                Console.WriteLine("哈哈哈哈哈哈哈哈哈哈哈哈");

                第一题

                string str;
                Console.Write("请输入当前课程名称:");
                str = Console.ReadLine();
                Console.WriteLine("请输入当前课程名称:"+str );
                Console.WriteLine("你当前的课程名称是:{0}",str );
                
                第二题

                string stuName, stuAge;
                Console.Write("请输入你的姓名:");
                stuName = Console.ReadLine();
                Console.Write("请输入你的年龄:");
                stuAge = Console.ReadLine();
                Console.WriteLine("姓名是:{0},年龄是:{1}",stuName,stuAge);
                Console.WriteLine("姓名是:"+stuName+",年龄是:"+stuAge);

                第三题
                string name, color, age;
                Console.Write("请输入歌星的姓名:");
                name = Console.ReadLine();
                Console.Write("请输入他所喜欢的颜色:");
                color = Console.ReadLine();
                Console.Write("请输入他的年龄:");
                age = Console.ReadLine();

                Console.WriteLine("你喜欢的歌手是:{0},他喜欢的颜色是:{1},他的年龄是:{2}",name,color,age);
                Console.WriteLine("你喜欢的歌手是:"+name+"他喜欢的颜色是:"+color+"他的年龄是:"+age);


                第四题
              
                double r;
                Console.Write("请输入圆的半径");
                r=double.Parse(Console.ReadLine());
                Console.WriteLine("圆的面积是:" + 3.1415926 * r * r + "周长是:" + 2 * 3.1415926 * r);
            }
        }
    }


    二、C#语言基础

    1、变量与常量

    变量:

     1)声明:数据类型 变量名;
     2)例子:int num;
     3)赋值:num=10;

    常量:
     1)声明: const 数据类型 常量名 = 常量值;
     2)例子:const int maxCount =100;

    2、类型转换
     
    隐式转换:一般不会失败

      1)例子:int num = 100;
       long num1 = num;
      2)遵循原则:
        a) 由低向高转换 
        b) 两种数据类型必须兼容

    显示转换:
      1)例子:double num = 10.2;
       int num1 =(int)num;
      2)方法:
        a) 利用Parse方法转换
           语法:被转换成的类型.Parse();
           例子:string str = "45";   //声明一个字符串变量
          int num = int.Parse(str);   //将字符串转换为整数  
        b) 使用convert 方法进行强制转换
           语法:convert.To需转换的类型(被转换成的类型);
           例子:string str = "45.2";  
          double num = convert.Todouble(str);
     
    3、运算符和表达式

    1)算术运算 操作数: a  b

      

    运算符名      说明              表达式         
    + 加法运算 a+b
    - 减法 a-b
    * 乘法 a*b
    / 除法 a/b
    % 取余 a%b
    ++ 操作数加1 a++或++a
    -- 操作数减1 a--或--a
    ~ 一个数按位取反 ~a

    2)赋值运算符 操作数:num=20

    运算符    表达式       计算方式         结果          
    += num+=10 num=num+10  30
    -= num-=10 num=num-10  10
    *= num*=10 num=num*10  200
    /= num/=10 num=num/10  2
    %= num%=10 num=num%10  0

     

    3)关系运算符  操作数: a   b

    关系运算符           表达式             
    == a==b
    != a!=b
    < a<b
    > a>b
    <= a<=b
    >= a>=b

    4)逻辑运算符
      a) && (逻辑与) :一假则假
      b) || (逻辑或) :一真则真
      c) ! (逻辑非) :假变真,真变假 

    5)优先级

    运算符优先级(由高到低)  结合性             
    ()  从左到右
    ++     --  从左到右
    *     /    %  从左到右
    +     -  从左到右
    <     <=      >      >=  从左到右
    =       !=  从左到右
    &&  从左到右
    ||  从左到右
    =      +=      *=     /=      -=  从左到右

    4、选择语句

    1) if  else 语句

    判断单个条件

    if(表达式)
    {
      计算结果为真时的语句块 
    }
    else
    {
      计算结果为假时的语句块
    }

    判断多个条件时

    if(表达式1)
    {
      代码块1
    }
    else if(表达式2)
    {
      代码块2
    }
    else if(表达式n)
    {
      代码块n
    }

    if.... else if...例子:

    int num1 ;
    int num2 ;
    Console.Write("请输入num1的值:");
    num1 = int.Parse(Console.ReadLine());
    Console.Write("请输入num2的值:");
    num2 = int.Parse(Console.ReadLine());
    Console.WriteLine("num1="+num1+"    num2="+num2);
    if (num1 < num2)
    {
        Console.WriteLine("num1<num2");
    }
    else if (num1 == num2)
    {
        Console.WriteLine("num1=num2");
    }
    else
    {
        Console.WriteLine("num1>num2");

    2)switch case 语句

       
    语法:
    switch(表达式)
    {
       case 常数表达式1:
     代码块1
     跳转语句(break/return)
       case 常数表达式2:
     代码块2
     跳转语句(break/return)
       case 常数表达式3:
     代码块3
     跳转语句(break/return)
       default:
     代码块4
     跳转语句(break/return)
    }

    例题
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    namespace ConsoleApplication2
    {
        class Program
        {
            static void Main(string[] args)
            {
                Console.WriteLine("请输入今天星期几(1-7):");
                int weekday = int.Parse(Console.ReadLine());
                switch (weekday)
                {
                    case 1:
                        Console.WriteLine("今天星期一,是工作日");
                        break;
                    case 2:
                        Console.WriteLine("今天星期二,是工作日");
                        break;
                    case 3:
                        Console.WriteLine("今天星期三,是工作日");
                        break;
                    case 4:
                        Console.WriteLine("今天星期四,是工作日");
                        break;
                    case 5:
                        Console.WriteLine("今天星期五,是工作日");
                        break;
                    case 6:
                        Console.WriteLine("今天星期六,休息");
                        break;
                    case 7:
                        Console.WriteLine("今天星期天,休息");
                        break;
                    default :
                        Console.WriteLine("输入错误,请输入正确的星期范围(1-7)");
                        break;
                }
            }
        }
    }

    5、循环语句

    1)while 循环

    语法:

    while(条件)
    {
       //语句
    }


    例题:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    namespace ConsoleApplication2
    {
        class Program
        {
            static void Main(string[] args)
            {
                int count = 0;
                while (count < 10)
                {
                    count++;
                    Console.WriteLine("循环次数:" + count);
                }
            }
        }
    }

    2)do while 循环
    语法:
    do
    {
       //代码
    }while(条件);

    例题:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    namespace ConsoleApplication2
    {
        class Program
        {
            static void Main(string[] args)
            {
                int count = 0;
                do
                {
                    count++;
                    Console.WriteLine("循环次数:" + count);
                } while (count < 10);
            }
        }
    }

    3)for 循环
    语法:
    for(初始值;条件;表达式)
    {
       //代码
    }

    例题  输出1-200之间的偶数

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    namespace ConsoleApplication2
    {
        class Program
        {
            static void Main(string[] args)
            {
                for (int i = 1; i < 200; i++)
                {
                    if (i % 2 == 0)
                    {
                        Console.WriteLine(i);
                    }
                }
            }
        }
    }

    4)  break 和continue 在循环中的作用

    1)break :跳出循环

    2)continue:结束当前循环开始下一循环

    3)例题:

     using System;

    using System.Collections.Generic;

    using System.Linq;

    using System.Text;

    namespace ConsoleApplication1

    {

        class Program

        {

            static void Main(string[] args)

            {

                //声明变量

                int count = 1;

                while (true)

                {

                    count++;

                    if (count % 3 == 0)

                    {

                        Console.WriteLine("count="+count);

                        continue;

                    }   

                  if (count > 20)

                    {

                        Console.WriteLine("count=" + count);

                        break;  

                   }

                }

            }

        }

    }

    6、数组

    1)声明:

    语法:数据类型 [数组大小] 数组名称;

    例:  

    a) int[] arr1;arr1=new int[] {1,2,3};

      b) int[] arr2=new int[] {1,2,3};

      c) int[] arr3=new int[3] {1,2,3};

    例题: 使用for循环打印数组元素

    using System;

    using System.Collections.Generic;

    using System.Linq;

    using System.Text;

    namespace ConsoleApplication1

    {

        class Program   

      {        

       static void Main(string[] args)     

        {            

         //声明变量

                string[] num = new string[4] {"罗马","米兰","马德里","巴塞罗那"};

                int length = num.Length;  

               Console.WriteLine("我喜欢的欧洲城市有"+length+"个");

                for (int i = 0; i < length; i++)

                {    

                   Console.WriteLine(num[i]);   

                }    

         }   

      }

    }

    2)foreach 循环

    语法:

    foreach(类型 变量名 in 集合或数组)

    {   

       //代码

    }

    例题:

    using System;

    using System.Collections.Generic;

    using System.Linq;

    using System.Text;

    namespace ConsoleApplication1

    {    

      class Program

        {

            static void Main(string[] args)

            {  

               string[] num = new string[4] { "罗马", "米兰", "马德里", "巴塞罗那" };

                int length = num.Length;

                Console.WriteLine("我喜欢的欧洲城市有" + length + "个");

                foreach (string city in num)

                {  

                   Console.WriteLine(city);

                }

            }

        }

    }

    3)字符串处理

    常用方法:

    方法        说明

    trim      去掉字符串首位的空格

    tolower     将字符串转成小写形式

    toupper     将字符串转化为大写

    inset      把字符串插入到另一字符串的指定索引处

    indexof      得到字符串中第一次出现指定字符串的索引位置

    lastindexof   得到字符串中最后一次出现指定字符串的索引位置 

    join        将两个字符串合并成一个新字符串

    replace       替换字符串

    copyto      复制

    split            根据指定字符将字符串拆分为一个数组

    substring     获取给定位置的字符串 format  格式化字符串

    示例:

    substring(index+1,index-index1-1)

    index+1:开始截取的位置

    index-index1-1:截取的长度

    例题:

    (一)

    using System;

    using System.Collections.Generic;

    using System.Linq;

    using System.Text;

    namespace ConsoleApplication1

    {    

      class Program

        {

            static void Main(string[] args)

            {

                string str = "   Welcome Mysec! ";

                //去掉空格前

                Console.WriteLine("原字符串"+str);

                //使用trim()去掉空格

                Console.WriteLine("去掉空格后:"+str.Trim());

                //转化为大写后的字符串

                Console.WriteLine("转化为大写后:" + str.ToUpper());

                //转化为小写后  

               Console.WriteLine("转化为小写后:" + str.ToLower());

            }

        }

    }

    (二)  验证邮箱地址

    using System;

    using System.Collections.Generic;

    using System.Linq;

    using System.Text;

    namespace ConsoleApplication1

    {

        class Program

        {  

           static void Main(string[] args)

            {  

               Console.Write("请输入你邮箱地址:");

                string mail = Console.ReadLine();

                int index1 = mail.IndexOf('@');

                int index2 = mail.IndexOf('.');

                if (index1 < 0)

                {

                    Console.WriteLine("非法地址,邮箱中应包含@字符");

                }

                if (index2 < 0)

                {  

                   Console.WriteLine("非法地址,邮箱中应包含.字符");

                }

                if (index1 >= 0 && index2 >= 0)

                {

                    Console.WriteLine("邮箱地址合法   你输入的邮箱为:"+mail);

                }

            }

        }

    }

    (三) 使用string的方法处理字符串( 连接 拆分 截取 格式化)

    using System;

    using System.Collections.Generic;

    using System.Linq;

    using System.Text;

    namespace ConsoleApplication1

    {  

       class Program

        {

            static void Main(string[] args)

            {

                string str1 = "Hello";

                string str2 = "Girl";

                //声明一个数组  

               string[] num = new string[] { "!", "!", "!" };

                //将str1和str2 用+连接后,再与数组进行连接

                string str= string.Join(str1+str2,num);

                Console.WriteLine(str);

                //格式化字符串

                string str3 = string.Format("{0},{1}连接后的字符串为{2}",str1,str2,str);

                Console.WriteLine(str3);

                //使用substring截取字符串

                string str4 = str.Substring(6,5);

                Console.WriteLine(str4);

                //使用split拆分

                string ip = "10.0.0.2";

                string[] ips = ip.Split('.');

                foreach (string s in ips)

                {  

                   Console.Write("{0}\t",s);

                }

                Console.WriteLine();

            }

        }

    }

  • 相关阅读:
    [dubbo实战] dubbo+zookeeper伪集群搭建 (转)
    [Dubbo实战]dubbo + zookeeper + spring 实战 (转)
    DUBBO本地搭建及小案例 (转)
    【Dubbo实战】 Dubbo+Zookeeper+Spring整合应用篇-Dubbo基于Zookeeper实现分布式服务(转)
    Quartz集成springMVC 的方案二(持久化任务、集群和分布式)
    【Quartz】Quartz的搭建、应用(单独使用Quartz)
    Javascript判断Crontab表达式是否合法
    给Java程序员的几条建议
    使用maven编译Java项目
    使用Docker运行Java Web应用
  • 原文地址:https://www.cnblogs.com/DBtwoer/p/3057394.html
Copyright © 2020-2023  润新知