目录
- C#学习记录
- 00. vs ide 基本介绍
- 01. 命名空间、类、主方法
- 02. 注释
- 03. 变量类型 与 数据结构
- 04. 变量的命名规则
- 05. 运算符的优先级及使用
- 06. 占位符的使用
- 07. 选择结构之if else
- 08. 选择结构之switch
- 09. 选择练习
- 10. while循环
- 11. do while 循环
- 12. for循环
- 13. 循环进阶部分
- 14. 九九乘法表
- 15. C# 基础数组的使用和理解
- 16. 类和对象
- 17. 无参方法
- 18. 带参方法
- 19. 流程控制关键字
- 20. 访问修饰符
- 21. vs调试运行
- 22. 引用类型和值类型
- 23. 引用传参
- 24. 类型转换
- 25. 字符串的处理方法
- 26. 对象数组
C#学习记录
00. vs ide 基本介绍
00.1 使用 debug 与 release 的不同
调试文件夹中的文件内容,包括调试文件,编译文件以及配置文件
01. 命名空间、类、主方法
02. 注释
单行注释、多行注释、块注释
/***/多行注释 //单行注释 ///<summary> ///</summary>文档注释 #region 测试方法集合 #endregion
03. 变量类型 与 数据结构
基本的数据类型
int 4个字节
double
float
char类型
拼接
字符与其他的类型的变量用+进行运算表示拼接
占位符
c#支持占位符
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace _04数据类型 { class Program { static void Main(string[] args) { int roomNo; roomNo = 1; double price = 300.1; char sex = '女'; string roomType = "总统套房"; Console.WriteLine("房间类型:{0};价格:{1}", roomType, price); } } }实际上,上述的程序还可以这样写
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace _04数据类型 { class Program { static void Main(string[] args) { int roomNo; roomNo = 1; double price = 300.1; char sex = '女'; string roomType = "总统套房"; //Console.WriteLine("房间类型:{0};价格:{1}", roomType, price); Console.WriteLine($"房间类型:{roomType};价格:{price}" ); } } }
04. 变量的命名规则
pascal命名法 每个单词的首个字母大写
类名、方法名、接口、属性
canel驼峰命名法 从第二个单词开始,首字母大写
变量名
C#的命名只能是数字、下划线、和字母
但是不能使用数字开头
见名知义
05. 运算符的优先级及使用
算数运算符
+ - * / %
关系运算符
> < == != >= <=
逻辑运算符
|| && !
赋值运算符
=
06. 占位符的使用
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _07占位符的使用
{
class Program
{
static void Main(string[] args)
{
string me;
me = Console.ReadLine();
string message;
message = string.Format("今天上班遇到了“{0}”", me);
Console.Write(message);
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _07占位符的使用
{
class Program
{
static void Main(string[] args)
{
string me;
me = Console.ReadLine();
string message;
//message = string.Format("今天上班遇到了“{0}”", me);
message = $"今天上班遇到了“{me}”";
Console.Write(message);
}
}
}
07. 选择结构之if else
08. 选择结构之switch
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _09switch选择语句
{
class Program
{
static void Main(string[] args)
{
int age = 2;
switch (age)
{
case 1:
Console.WriteLine($"小朋友今年一岁了");
break;
case 2:
Console.WriteLine($"小朋友今年两岁了");
break;
default:
break;
}
}
}
}
09. 选择练习
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _10银行账户系统
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("--------------------------------欢迎登录银行账户系统--------------------------------");
Console.Write("请输入帐号:");
string account = Console.ReadLine();
Console.Write("请输入密码:");
string passWord = Console.ReadLine();
if (account == "123456" && passWord == "abc")
{
Console.WriteLine("登录成功!");
Console.WriteLine("请选择服务菜单:1、查询余额;2、取款;3、存款;4转账");
int menuId = int.Parse(Console.ReadLine());
switch (menuId)
{
case 1:
Console.WriteLine("您的用户余额是:50000");
break;
case 2:
Console.WriteLine("请输入取款金额:");
break;
case 3:
Console.WriteLine("请输入存款金额:");
break;
case 4:
Console.WriteLine("请输入转账金额:");
break;
default:
break;
}
}
else
Console.WriteLine("密码或帐号错误,请重新输入!");
}
}
}
10. while循环
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _10while循环
{
class Program
{
static void Main(string[] args)
{
int count = 1;
while(count < 10)
{
Console.WriteLine($"张三简历{count} 姓名:张三");
count += 1;
}
}
}
}
11. do while 循环
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _11dowhile循环
{
class Program
{
static void Main(string[] args)
{
int score;
do
{
Console.WriteLine("请输入张三的成绩:");
score = int.Parse(Console.ReadLine());
if (score<60)
Console.WriteLine("考试不合格,重新补考");
} while (score < 60);
Console.WriteLine("考试通过!");
}
}
}
12. for循环
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _12for循环
{
class Program
{
static void Main(string[] args)
{
for (int i =1;i<=10;i++)
{
Console.WriteLine($"打印第{i}份");
}
}
}
}
13. 循环进阶部分
14. 九九乘法表
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _14九九乘法表
{
class Program
{
static void Main(string[] args)
{
for (int i = 0;i<9;i++)
{
for ( int j = 0;j<=i;j++)
{
Console.Write((j+1) + "x" + (i+1) + "=" + ((i+1)*(j+1)));
Console.Write("\t");
}
Console.Write("\n");
}
}
}
}
15. C# 基础数组的使用和理解
用于存储一组数据类型相同的数据,可以称之为数组
数组的四要素
数组标识符、数组元素、数组下标、数组类型
数组一旦初始化,就不能再修改数组长度
注意不要让数组下标越界
数组的长度等直接封装成方法了
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _15数组的基本理解
{
class Program
{
static void Main(string[] args)
{
string name1 = "张三";
string name2 = "李四";
Console.WriteLine(name1 + name2);
// --------------------------------
string[] names = new string[30];
names[0] = "张三";
names[1] = "李四";
names[2] = name1;
Console.WriteLine(names[2]);
// --------------------------------
string[] names1 = new string[]{"张三","李四","王五"};
Console.WriteLine(names1[2]);
for (int i = 0;i<names1.Length;i++)
{
Console.WriteLine(names1[i]);
}
}
}
}
16. 类和对象
- 类 :抽象的(模板) 不占用内存空间
- 对象:具体的(真是存在的)对象占用内存空间
代码块1
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _16类和对象
{
class Program
{
static void Main(string[] args)
{
//创建一个学生
Student student1 = new Student();
student1.Name = "张三";
student1.Age = 22;
student1.Sex = '男';
student1.Phone = "18889u9897987";
Student student2 = new Student();
student2.Name = "李四";
student2.Age = 212;
student2.Sex= '女';
student2.Phone = "188899897987";
Console.WriteLine($"姓名:{student2.Name}{student2.Age}");
Student[] students = new Student[] { student1, student2 };
for (int i =0;i<students.Length;i++)
{
Console.WriteLine($"学生姓名:{students[i].Name},学生性别:{students[i].Sex}");
students[i].xtxi();
}
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _16类和对象
{
public class Student
{
// 静态特征:用来描述
/// <summary>
/// 学生姓名
/// </summary>
public string Name;
/// <summary>
/// 学生年龄
/// </summary>
public int Age;
/// <summary>
/// 性别
/// </summary>
public char Sex;
/// <summary>
/// 电话
/// </summary>
public string Phone;
// 动态特征
/// <summary>
/// 学习(相当于功能 方法 动态特征)
/// </summary>
public void xtxi()
{
Console.WriteLine(this.Name+"正在看书");
}
}
}
17. 无参方法
如果没有返回值,就用void;如果有返回值,就用int、string等,但是返回的要与定义的类型相一致。
18. 带参方法
方法里面有参数,主要是可以将参数值直接传入方法或者函数中,这种较为常规
主函数
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _18带参方法
{
class Program
{
static void Main(string[] args)
{
Lamp lamp = new Lamp();
lamp.Open();
lamp.Close();
Zhazhiji zhazhi = new Zhazhiji();
string fruit = zhazhi.Zhazhi("苹果");
Console.WriteLine(fruit);
}
}
}
子函数
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _18带参方法
{
/// <summary>
/// 榨汁机类
/// </summary>
public class Zhazhiji
{
/// <summary>
/// 形成榨汁机
/// </summary>
/// <param name="fruit">水果</param>
/// <returns></returns>
public string Zhazhi(string fruit)
{
string result = fruit + "汁";
return result;
}
}
}
19. 流程控制关键字
break
可以用于switch while if 中
continue
循环中
return
通常是在方法中使用
静态方法中不能调用非静态的任何成员?
结束当前方法、可以返回计算结果 都是用来控制整个程序运行过程中的命令
20. 访问修饰符
private 属性和方法默认的是这个 只能自己类的内部访问
protected 可以被继承
internal 类默认是这个 在同一个命名空间可以使用 或者说在同一个项目中可以使用
public 可以随便调用
继承之后 用 base.Name
主程序
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using testProject;//引用另外一个项目
namespace _20访问修饰符
{
class Program
{
static void Main(string[] args)
{
// --------------------------------教师类实例化
Teacher tea1 = new Teacher();
tea1.Test();
// --------------------------------学生类实例化
Student stu1 = new Student();
stu1.StudentName="张x三";
Console.WriteLine(stu1.StudentName);
}
}
}
同命名空间 类1 基类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _20访问修饰符
{
/// <summary>
/// 定义雇员类
/// </summary>
class Employee
{
protected string Name;
}
}
同命名空间 类2 子类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _20访问修饰符
{
/// <summary>
/// 定义继承于雇员的教师类
/// </summary>
internal class Teacher:Employee
{
/// <summary>
/// 定义公有方法,test
/// </summary>
public void Test()
{
Name = "张三";
Console.WriteLine(Name);
}
}
}
其他命名空间 引用
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace testProject
{
/// <summary>
/// 定义学生类
/// </summary>
public class Student
{
public string StudentName;
}
}
21. vs调试运行
F5调试运行
F10逐步调试
F11逐句调试 如果有方法,会直接调入到方法之中
22. 引用类型和值类型
值类型一般都是基于两个基类,分别是System.ValueType和Object
System.ValueType
值类型:int short long float double decimal char bool enum struct
注意:在声明变量的时候,编译器始终会给他分配内存空间 存储于栈
Object
引用类型:object string Array(int[]) class interface 委托delegate
注意:在声明或者定义变量的时候,编译器会存储一个地址,地址存放于栈 在信息存储于堆
主函数
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _22引用类型和值类型
{
class Program
{
static void Main()
{
Dog dog = new Dog();//实例化
dog.age = 8;
ChangeDogAge(dog);
Console.WriteLine(dog.age);
int age = 19;
ChangeAge(age);
Console.WriteLine(age);
Console.Read();
}
public static void ChangeAge(int age)
{
age = 20;
}
public static void ChangeDogAge(Dog dog2)
{
dog2.age = 5;
}
}
}
同命名空间的子类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _22引用类型和值类型
{
class Dog
{
public string Name;
public string Color;
public int age;
public void Read()
{
Console.WriteLine($"{this.Color}的{this.Name}在学习");
}
}
}
23. 引用传参
对于值类型,其在方法中传递的时候,通常是值传递,如果想要变成地址传递,则需要在前面加上ref ,从而实现地址传递
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _23引用传参
{
class Program
{
static void Main(string[] args)
{
int num1 = 1;
int num2 = 2;
Console.WriteLine($"交换前:{num1}-{num2}");
Swap(ref num1, ref num2);
Console.WriteLine($"交换后:{num1}-{num2}");
Console.Read();
}
public static void Swap(ref int num1,ref int num2)
{
int temp = num1;
num1 = num2;
num2 = temp;
}
}
}
24. 类型转换
数字类型之间的转换
包括隐式转换和显示转换,其中隐式转换也成为自动转换,显示转换称之为强行转换
字符串转换为数字类型 Parse TryParse
将任意类型转换为指定类型
Convert.ToInt32()可以实现将任何类型转换为该类型
字符类型
char类型可以直接转换为数字
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _24类型转换
{
class Program
{
static void Main(string[] args)
{
// --------------------------------数字类型之间的相关转换
//int price1 = 100;
//double price2 = price1;// 隐式转换
//int price3 =(int) price2;// 显式转换
// --------------------------------字符串类型转换为数字类型
//Console.Write("请输入一个学生的年龄:");
//int age = int.Parse(Console.ReadLine());
//Console.WriteLine($"学生的年龄是:{age}");
// --------------------------------字符串类型转换为数字类型 异常捕获
//Console.Write("请输入一个学生的年龄:");
//int age;
//bool result = int.TryParse(Console.ReadLine(),out age);
//if (result)
// Console.WriteLine($"学生的年龄是:{age}");
//else
// Console.WriteLine("输入的不是数字!");
//--------------------------------将任意类型转换为指定类型
object str = "123";
int money = Convert.ToInt32(str);
Console.WriteLine(money);
//--------------------------------将字符串转换为时间类型
object str1 = "1993年1月12日";
DateTime money1 = Convert.ToDateTime(str1);
Console.WriteLine(money1);
}
}
}
25. 字符串的处理方法
主要是字符串的一些封装好的方法
.IndexOf() // 从前面开始找
.LastIndexOf() // 从后面开始找
.Substring(index) //从指定的数字之后的字符串
.ToLower() //转换为小写
.ToUpper() // 转换为大写
判断对比的时候 == 对比的是地址 equals 对比的是值
如果是引用类型的话,两个是一样 字符串和对象 最好用equals
.Join() // string 的函数,按照指定的连接方式,将字符串数组进行连接
.Split() //作用于数组,将数组按照指定的字符进行分割
.Trim() // 去掉两端的空格
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _25字符串的处理方法
{
class Program
{
static void Main(string[] args)
{
// -------------------------------- 获取指定的字符所在的index
// 字符串的处理方式
// IndexOf()
string email = "mrxiuhong@163.com";
int index = email.LastIndexOf('@');
//int index = email.IndexOf('d');
if (index < 0)
System.Console.WriteLine("所查找的字符不存在!");
else
System.Console.WriteLine($"@所在的位置是{index}");
string substring = email.Substring(index + 1);
Console.WriteLine(substring.ToUpper());
// --------------------------------字符的对比
if (email.Equals("mrxiuhong@qq.com"))
Console.WriteLine("值相等");
else
Console.WriteLine("值不相等");
// -------------------------------- 数组连接
string[] newStr = new string[] { "abc", "edf" };
string str = string.Join("-",newStr);
Console.WriteLine($"数组连接{str}");
// -------------------------------- 数组分割
string[] newStr1 = str.Split('-');
for (int i =0;i<newStr1.Length;i++)
Console.WriteLine($"数组分割{newStr1[i]}");
// -------------------------------- 空格的问题
string myStr = Console.ReadLine();
if (myStr.Equals("mrxiuhong"))
{
Console.WriteLine("正确");
}
else
{
Console.WriteLine("错误");
}
// -------------------------------- Trim() 把 两端的空格去掉
if (myStr.Trim().Equals("mrxiuhong"))
{
Console.WriteLine("正确");
}
else
{
Console.WriteLine("错误");
}
}
}
}
26. 对象数组
数组可以存放一组相同类型的数据
对象数组可以存放不同类型的数据