首页 > 编程知识 正文

c语言语法大全手册,C1

时间:2023-05-04 03:15:51 阅读:115431 作者:3692

文章目录C#光速学习1、 net2、C#3.net的作用3.2种交互模式4.VS的使用5.HelloWorld6.VS基本设置7 .使用基础语法a .类型b .号c .字符串的占位符d.e .转义字符和嵌套

C#光速学习1, net

. net通常是指. net平台和. net框架。

框架包含在平台中。

. net框架包括CLR和. net类库。

2、C#在C#上开发基于. net平台的APP应用。

3 .网络角色桌面APP应用程序(WinForm )、ASP.net )、手机开发、Unity3D游戏开发和虚拟现实。

3.2种交互模式c/s(winform )、b/s (互联网APP应用)。

4.VS的使用注意书写. net,先安装. net组件。

解决方案包含项目的包含类. cs

模拟公司-部门-员工

项目的目录结构

. sln解决方案文件。

每个项目都有对应的项目文件

5.hello全球使用系统; using system.collections.generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespaceconsoleapp1{ class program { staticvoidmain (string [ ] args ) console.writeline ) Helloworld! ' ); //打印字符串并换行的Console.ReadKey (; //块等待按键输入,防止退出} }在CW中输入2次tab即可立即打印。

F5快速调试运行。

6.VS首选项多个项目如何启动当前项目

选择当前选择内容即可;

7.C#的评论

单行注释//多行注释/* */文档注释。 一般用于类方法//

7.VS快捷键

ctrl s保存

ctrl z撤销

shitfhomeshiftendhome句首,快速选择end语句末。 可以上下左右对齐。

折叠代码#region #endreigon

7 .基本语法a .类型int整数、char字符、string字符串、双浮点数、decimal。

也可以使用字符串。 所有语言都是通用的。 小写的字符串是cs独有的。

b .将符号的使用字符串与整数连接起来时,整数会自动转换为字符串。

staticvoidmain (字符串[ ] args ) { //int a=10; 字符串s=' 123 '; 控制台. writeline (5s; 控制台. read key (; }//5123 c .字符串的占位符int a=10; int b=20; int c=30; console.writeline('{0}{2}{1}、a、b、c ); 控制台. read key (; 输入//103020 d.strings=console.readline (); e .转义字符和本机字符串与c语言类似。

字符串用@表示

strings=@ ' www.adadac sadasdasddsadsad '; 控制台. writeline (s; 控制台. read key (; //www.adadac sadasdasddsadsadf .类型转换string s='12313 '; doublev=convert.todouble(s; inta=convert.toint32(s; 控制台. writeline (v; 控制台. writeline (a; 控制台. read key (;使用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();/*输入:123输出:ok: Trueres: 123*/ 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();/*输入:abc1231231输出格式错误00*/ 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();/*xiaomingnew_number: 2name: xiaoming*/

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; }/*1020*/ 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]); } }/*Herio123Herio12456*/ 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/

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。