c#基础

第一个c#项目

visual studio -> 文件 -> 新建 -> 项目 -> 选择C# -> 控制台应用程序 -> 下一步...

解决方案 > 项目 > 类

  • .sln:解决方案文件,包含了整个解决方案的基本信息
  • .csproj:C#项目文件,包含了整个项目的基本信息
  • .cs:C#脚本文件,纯文本文件
// 引入命名空间
using System;

/// <summary>
/// 当前命名空间
/// </summary>
namespace FirstCSharp
{
    /// <summary>
    /// 当前类
    /// </summary>
    class Program
    {
        /// <summary>
        /// 入口函数
        /// </summary>
        /// <param name="args"></param>
        static void Main(string[] args)
        {
            // 单行注释
            /* 
                多行注释
            */
            /// 文档注释(命名空间、类、方法)
            Console.WriteLine("Hello World!");
        }
    }
}

变量

  • 必须以"字母"、"_"、"@"开头,不能以"数字"开头,后面可以跟任意"字母"、"数字"、"下划线"
  • "_"开头:一般为私有变量,"@"开头:后面可以跟c#关键字
  • 变量名不能与c#系统中的关键字重复
  • 大小写敏感
  • 同一个变量名不允许重复定义
  • 命名规范:小写字母开头的驼峰
using System;

namespace Test
{
    class Program
    {
        static void Main(string[] args)
        {
            // 整数,取值范围:-2,147,483,648 到 2,147,483,647
            int num = 123;
            Console.WriteLine(num);
            // 单精度小数类型, 既能存储整数,又能存储小数,值后面需要加上一个f
            // 取值范围:小数点后面的位数是7位
            float num1 = 3.14f;
            Console.WriteLine(num1);
            // 双精度小数类型,既能存储整数,又能存储小数
            // 取值范围:小数点后面的位数是15~16位
            double num2 = 3.14;
            Console.WriteLine(num2);
            // true or false
            bool yesOrNo = true;
            Console.WriteLine(yesOrNo);
            string str = "这是一个字符串";
            Console.WriteLine(str);
            // 字符类型,用来存储单个字符,最多最少只能有一个字符,不能存储空,字符类型的 值需要用单引号引起来
            // 一个字符:1 个数字,1 个字母,1 个汉字,1 个符号
            char c = 'A';
            Console.WriteLine(c);
            Console.ReadKey();
        }
    }
}

常量

常量名的命名一般是全大写,单词与单词之间用下划线分割

using System;

namespace Test
{
    class Program
    {
        static void Main(string[] args)
        {
            const double PI = 3.1415926;
            Console.WriteLine(PI);
            const string SERVER_IP = "127.0.0.1";
            Console.WriteLine(SERVER_IP);
        }
    }
}

运算符与表达式

  • +-*/%前++前--后++后--+=-=*=/=%=
  • ><>=<===!=&&||!{}
using System;

namespace FirstCSharp
{
    class Program
    {
        static void Main(string[] args)
        {
            // 加法
            Console.WriteLine(1 + 2);
            // 拼接
            Console.WriteLine('a' + "b");
            // 字符转ascii码相加
            Console.WriteLine(1 + 'a');
            // 拼接
            Console.WriteLine(1 + "b");
            // 拼接
            Console.WriteLine("b" + 1);
            // 字符转ascii码相加
            Console.WriteLine('a' + 1);
            // 占位符,拼接
            int x = 1;
            string y = "yyy";
            char z = 'z';
            Console.WriteLine("数字:{0},字符串:{1},字符:{2}。", x, y, z);
        }
    }
}

控制台交互

using System;

namespace Operator
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Write("请输入您的姓名:");
            string name = Console.ReadLine();
            Console.WriteLine("你的姓名叫{0},对吗?", name);
            Console.ReadKey();
        }
    }
}

常用转义符

  • \n:表示换行
  • \":表示一个英文半角的双引号
  • \t:表示一个 tab 键的大空格
  • \b:表示一个 BackSpace 退格键,会把前面的一个字符删掉
  • \:表示一个\
  • @:用在字符串的前面,有两种用途(多用于路径、sql语句等)
    • 取消"\"在字符串中的转义作用,使其单纯的就表示一个斜线
    • 将字符串按照原格式输出

类型转换

隐式类型转换(自动类型转换)

  • 两种类型兼容
  • 原类型要小于目标类型(小的转大的)

显式类型转换(强制类型转换)

  • 两种类型兼容
  • 原类型大于目标类型(大的转小的)

表达式中的类型转换

  • 比如:int和double相加,结果就会被提升为double

Convert类型转换

  • 如果两个变量的类型不兼容,比如string和int,string和double,这个时候我们可以用Convert进行转换

toString()类型转换

  • Varable.toString();
using System;

namespace Test
{
    class Program
    {
        static void Main(string[] args)
        {
            // 隐式转换
            int num1 = 1;
            float num2 = num1;
            double num3 = num2;
            Console.WriteLine(num3.GetType());

            // 显示转换
            num3 = 3.14;
            num1 = (int)num3;
            Console.WriteLine(num1.GetType());
            // 显示转换也写可以小的转大的,但是不推荐这么写
            num3 = (double)num1;

            // 表达式中的类型转换
            Console.WriteLine((1 * 3.14f).GetType());

            // Convert类型转换
            string num = "3.1415926";
            double dnum = Convert.ToDouble(num);
            Console.WriteLine(dnum.GetType());
            // toString()类型转换
            Console.WriteLine(dnum.ToString().GetType());
            Console.ReadKey();
        }
    }
}

条件判断

using System;

namespace Test
{
    class Program
    {
        static void Main(string[] args)
        {
            int a = 1;
            int b = 2;
            if (a > b)
            {
                Console.WriteLine(1);
            }
            else if (b == 3)
            {
                Console.WriteLine(2);
            }
            else
            {
                Console.WriteLine(3);
            }

            Console.ReadKey();
        }
    }
}
using System;

namespace SwitchStruct
{
    class Program
    {
        static void Main(string[] args)
        {
            string opt = Console.ReadLine();

            switch (opt)
            {
                case "+":
                    Console.WriteLine("加法");
                    break;
                case "-":
                    Console.WriteLine("减法");
                    break;
                default:
                    Console.WriteLine("算力不足");
                    break;
            }

            Console.ReadKey();
        }
    }
}

循环

using System;

namespace Test
{
    class Program
    {
        static void Main(string[] args)
        {
            for (int i = 0; i < 10; i++)
            {
                if (i == 1)
                {
                    continue;
                }
                if (i == 5)
                {
                    break;
                }
                Console.WriteLine(i);
            }

            int j = 0;
            while (j < 0)
            {
                // 不会执行
                Console.WriteLine(j);
                j++;
            }

            int k = 0;
            do
            {
                // 会执行一次
                Console.WriteLine(k);
                k++;
            } while (k < 0);

            Console.ReadKey();
        }


    }
}

数组

using System;

namespace ArrayStruct
{
    class Program
    {
        static void Main(string[] args)
        {

            int[] intArr;
            intArr = new int[5];
            intArr[0] = 1;

            Console.WriteLine(intArr);

            double[] doubleArr = new double[3];
            doubleArr[0] = 1;
            Console.WriteLine(doubleArr);

            int[] intArr2 = new int[] { 1, 2, 3 };
            int[] intArr3 = new int[3] { 2, 3, 4 };
            Console.WriteLine(intArr2[0]);
            Console.WriteLine(intArr3[1]);


            Console.ReadKey();
        }
    }
}

遍历

using System;

namespace ArrayStruct
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] intArr2 = new int[] { 1, 2, 3 };

            foreach(int i in intArr2)
            {
                Console.WriteLine(i);
            }

            Console.ReadKey();
        }
    }
}

值类型与引用类型

using System;

namespace Test
{
    class Program
    {
        static void Main(string[] args)
        {
            int a = 3;
            int b = a;
            a = 4;
            // 4
            Console.WriteLine(a);
            // 3,值类型,a的赋值不会影响到b
            Console.WriteLine(b);

            int[] arrA = new int[] { 1, 2, 3 };
            int[] arrB = arrA;

            arrA[0] = 10;
            // 10
            Console.WriteLine(arrA[0]);
            // 1,引用类型,arrA和arrB指向同一个索引
            Console.WriteLine(arrB[0]);

            Console.ReadKey();
        }
    }
}

二维数组及其遍历

using System;

namespace Test
{
    class Program
    {
        static void Main(string[] args)
        {

            int[,] intArr;
            intArr = new int[3, 3];
            intArr[0, 0] = 1;

            int[,] intArr2 = new int[2, 3];
            intArr2[0, 1] = 2;

            int[,] intArr3 = new int[2, 3]
            {
                {1,2,3 },
                {4,5,6 }
            };

            foreach (int i in intArr3)
            {
                Console.Write(i);
            }

            Console.WriteLine();

            for (int i = 0; i < intArr3.GetLength(0); i++)
            {
                for (int j = 0; j < intArr3.GetLength(1); j++)
                {
                    Console.WriteLine(intArr3[i, j]);
                }
            }

            Console.ReadKey();
        }
    }
}

函数

命名规则:大写字母开头的驼峰

/// 如果不需要参数可以留空,如果需要参数,要以:参数类型 参数名的形式书写
static 返回值类型 函数名(参数列表)
{
    函数体代码;
}
using System;

namespace Test
{
    class Program
    {
        static void Main(string[] args)     //Main 入口函数.
        {
            HelloWorld();
            string zhangsan = Hello("张三");
            Console.WriteLine(zhangsan);

            Console.ReadKey();
        }

        static void HelloWorld()
        {
            Console.WriteLine("hello world");
        }

        static string Hello(string name)
        {
            return "Hello" + name;
        }

    }
}

函数的重载:函数的名称相同,但是参数列表不同

  • 如果参数的个数相同,那么参数的类型就不能相同
  • 如果参数的类型相同,那么参数的个数就不能相同
using System;

namespace Test
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine(Add(1, 2));
            Console.WriteLine(Add(1.1, 2.2));
            Console.WriteLine(Add(1.1, 2));
            // 会调用到double add double
            Console.WriteLine(Add(1, 2.2));

            Console.ReadKey();
        }
        static int Add(int a, int b)
        {
            Console.WriteLine("int add int");
            return a + b;
        }
        static double Add(double a, double b)
        {
            Console.WriteLine("double add double");
            return a + b;
        }
        static double Add(double a, int b)
        {
            Console.WriteLine("double add int");
            return a + b;
        }
    }
}

引用参数

ref参数:函数外必须为变量赋值,而函数内可以不赋值,形参和实参前面都要加ref关键字

using System;

namespace Test
{
    class Program
    {
        static void Main(string[] args)
        {
            int num = 5;
            Add(ref num);
            // 15
            Console.WriteLine("num:" + num);
            Console.ReadKey();
        }

        static void Add(ref int num)
        {
            num += 10;
            // 15
            Console.WriteLine(num);
        }
    }
}

out参数:一个函数中如果返回多个不同类型的值,就需要用到out参数

函数外可以不为变量赋值,而函数内必须为其赋值,形参和实参前面都要加out关键字

using System;

namespace outParam
{
    class Program
    {
        static void Main(string[] args)
        {
            int a = 10;
            int b = 5;

            int m_max;
            int m_min;
            int m_total;
            double m_avg;

            Number(a, b, out m_max, out m_min, out m_total, out m_avg);
            Console.WriteLine("Max:{0},Min:{1},Total:{2},Avg:{3}", m_max, m_min, m_total, m_avg);

            Console.ReadKey();
        }

        static void Number(int a, int b, out int max, out int min, out int total, out double avg)
        {
            max = a > b ? a : b;
            min = a > b ? b : a;
            total = a + b;
            avg = total / 2;
        }

    }
}

@开头的变量

using System;

namespace WebRequestTest
{
    class Program
    {
        static void Main(string[] args)
        {
            bool @bool = true;
            @class.@static(@bool);
            Console.ReadKey();
        }

        class @class
        {
            public static void @static(bool @bool)
            {
                if (@bool)
                {
                    System.Console.WriteLine("true");
                } else
                {
                    System.Console.WriteLine("false");
                }
            }
        }
    }
}

TODO

结构体、继承、类、对象、虚方法、visual studio配置

results matching ""

    No results matching ""