栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > .Net

C#光速学习

.Net 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

C#光速学习

文章目录
  • C#光速学习
    • 1、.net
    • 2、C#
    • 3.net的作用
    • 3.两种交互模式
    • 4.VS的使用
    • 5.HelloWorld
    • 6.VS基本设置
    • 7.基础语法
      • a.类型
      • b.+号的使用
      • c.字符串的占位
      • d.输入
      • e.转义符和原生字符串
      • f.类型转换
    • 8.流程控制
      • a.异常捕获
      • b.断点调试
      • c.枚举
    • 9.复杂数据类型
      • a.数组
    • 10.函数
      • a.out参数
      • b.ref参数
      • c.params
    • 11.对象
      • 对象的属性
      • This的使用
    • 参考学习

C#光速学习 1、.net

.net一般指.net平台和.net framework 框架。

框架包含于平台。

.net framework 框架包含CLR和.NET类库。

2、C#

用C#开发基于.net平台的应用。

3.net的作用

桌面应用程序(WinForm) 、ASP.net(Internet应用程序)、手机开发、Unity3D游戏开发和虚拟现实。

3.两种交互模式

C/S(WinForm)、B/S(Internet应用程序)。

4.VS的使用

写.net 注意先安装.net组件。

解决方案 包含 项目 包含 类(.cs)

类比公司-部门-员工

项目的目录结构

.sln 解决方案文件。

每个项目里面有对应的项目文件

5.HelloWorld
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!"); //打印字符串并换行
            Console.ReadKey(); //阻塞等待输入一个键,防止结束
        }
    }
}

输入cw+两次tab 即可快速打印。

F5 快速调试运行。

6.VS基本设置

多项目怎么启动当前的项目

选择当前选定内容即可;


7.C#的注释

  • 单行注释 //
  • 多行注释
  • 文档注释,一般用于类方法 ///

7.VS快捷键

ctrl+s 保存

ctrl+z 撤销

shitf+home shift+end 快速选中 home句首,end句尾。 可以配合上下左右。

折叠代码 #region #endreigon


7.基础语法 a.类型

int整数,char字符,string字符串,double浮点数,decimal。

String 也可以用,是所有语言通用,小写string是cs独有。

b.+号的使用

当字符串和整数用+连接时,整数会自动转为字符串。

        static void Main(string[] args)
        {
            //int a = 10;
            String s = "123";
            Console.WriteLine(5+s);
            Console.ReadKey();
        }
//5123
c.字符串的占位
            int a = 10;
            int b = 20;
            int c = 30;
            Console.WriteLine("{0}  {2}  {1}",a,b,c);
            Console.ReadKey();
// 10 30 20
d.输入
string s = Console.ReadLine();
e.转义符和原生字符串

与C语言类似。

字符串用@表示

            string s = @"www.adadacsadasdasddsadsad";
            Console.WriteLine(s);
            Console.ReadKey();
//  www.adadacsadasdasddsadsad
f.类型转换
            string s = "12313";
            double v = Convert.ToDouble(s);
            int a = Convert.ToInt32(s);
            Console.WriteLine(v);
            Console.WriteLine(a);
            Console.ReadKey();

使用TryParse方法:

            string s1=Console.ReadLine();
            int res = 0;
            bool ok = int.TryParse(s1,out res);
            Console.WriteLine("ok: "+ok);
            Console.WriteLine("res: "+res);
            Console.ReadKey();

8.流程控制 a.异常捕获
            string s1=Console.ReadLine();
            string s2= Console.ReadLine();
            double v = 0;
            int a = 0;
            try
            {
                 v = Convert.ToDouble(s1);
                 a = Convert.ToInt32(s2);
            }
            catch
            {
                Console.WriteLine("格式错误");
            }
            Console.WriteLine(v);
            Console.WriteLine(a);
            Console.ReadKey();

b.断点调试

灰色区域左键即可添加端点,F11分步调试。

c.枚举
    public enum gender
    {
        男,
        女
    }
    public enum name
    {
        herio,
        harris
    }

           gender g1 = gender.男;
            name n1 = name.herio;
            Console.WriteLine("name: {0} gender: {1}",n1,g1);
            Console.ReadKey();

枚举能与int类型相互转换。

所有类型都能转换为string类型,使用toString()。

            int number = 2;
            name n1 = (name)number;
            Console.WriteLine(n1);
            int new_number = (int)n1;
            Console.WriteLine("new_number: "+new_number);
            string s = n1.ToString();
            Console.WriteLine("name: "+s);
            Console.ReadKey();

string 转换为enum 使用parse

            string s1 = "0";
            string s2 = "男";
            gender g1 = (gender) Enum.Parse(typeof(gender), s1);
            gender g2 = (gender)Enum.Parse(typeof(gender), s2);
            Console.WriteLine("g1:" +g1);
            Console.WriteLine("g2: " + g2);
            Console.ReadKey();
9.复杂数据类型 a.数组
            int[] a = new int[10];
            int[] b = { 10, 9, 8, 7, 6 };
            for(int i = 0; i < 10; i++)
            {
                a[i] = i;
                Console.WriteLine(a[i]);
            }
            for(int i = 0; i < 5; i++)
            {
                Console.WriteLine(b[i]);
            }
            Console.ReadKey();
10.函数
        static void Main(string[] args)
        {
            Console.WriteLine(max(3,10));
            Console.WriteLine(Program.max(20, 10));
            Console.ReadKey();
        }

       public static int max(int a,int b)
        {
            return a > b ? a : b;
        }

a.out参数
        static void Main(string[] args)
        {
            int res;
            max(3, 10,out res);
            Console.WriteLine(res);
            Console.ReadKey();
        }

       public static void max(int a,int b,out int c)
        {
            c = a > b ? a : b;
        }
b.ref参数

ref就是传引用。

        static void Main(string[] args)
        {
            int a = 10, b = 20;
            swap(ref a, ref b);
            Console.WriteLine(a);
            Console.WriteLine(b);
            Console.ReadKey();
        }

        public static void swap(ref int a,ref int b)
        {
            int c = a;
            a = b;
            b = c;
        }
c.params

可变参数

        static void Main(string[] args)
        {
            string a = "Herio";
            int[] b = { 1, 2, 4, 5, 6 };
            fun(a, 1, 2, 3);
            fun(a, b);
            Console.ReadKey();
        }

        public static void fun(string a ,params int [] b)
        {
            Console.WriteLine(a);
            for(int i = 0; i < b.Length; i++)
            {
                Console.WriteLine(b[i]);
            }
       
        }

11.对象

定义一个Person类

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

namespace ConsoleApp1
{
    class Person
    {
        public string _name;
        public string _password;
        public int _age;

        public void fun()
        {
            Console.WriteLine("name:{0},password:{1},age:{2}",_name,_password,_age);
        }
    }
}

引用

        static void Main(string[] args)
        {
            Person p = new Person();
            p._name = "Herio";
            p._password = "123456";
            p._age = 18;
            p.fun();
            Console.ReadKey();
        }
对象的属性

使用get,set方法。

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

namespace ConsoleApp1
{
    class Person
    {
        private string _name;
        public string _password;
        public int _age;
        public string Name
        {
            get{return _name;}
            set { _name = value; }
        }
        public void fun()
        {
            Console.WriteLine("name:{0},password:{1},age:{2}",Name,_password,_age);
        }
    }
}


            Person p = new Person();
            p.Name = "Herio";
            p._password = "123456";
            p._age = 18;
            p.fun();
            Console.ReadKey();
This的使用
  • 表示当前对象
  • 作为构造函数
        private string _name;
        public string _password;
        public int _age;
    
        Person(string name,string password,int age)
        {
            this.Name = name;
            this._password = password;
            this._age = age;
        }
    
        Person(string name) : this(name, "123", 20) { }
参考学习
  • 学习视频

https://www.bilibili.com/video/BV1FJ411W7e5?p=108

  • 插件下载地址

https://marketplace.visualstudio.com/

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1023572.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号