c#入门学习笔记
2024-09-02 11:59:55
Hello World
//打印语句
Console.WriteLine("Hello World");
//暂停
Console.ReadKey();
数据类型
1.值类型 byte,char,short,int,long,bool,decimal,float,double,sbyte,uint,ulong,ushort
2.引用类型: 储存的不是值实际数据而是一个内存地址 object、dynamic 和 string。
3.对象类型:Object 类型检查是在编译时发生的
4.动态类型: 可以存储任何类型的值在动态数据类型变量中,类型检查在运行时
5.字符串(String)类型
6.指针类型(Pointer types)类型转换
- 隐式转换
- 显式转换
- Toxxx方法
变量
常量
- 整数常量
- 整数常量可以是十进制、八进制或十六进制的常量。前缀指定基数:0x 或 0X 表示十六进制,0 表示八进制,不带前缀则默认表示十进制。
- 整数常量也可以带一个后缀,后缀是 U 和 L 的组合,U 表示无符号整数(unsigned),L 表示长整数(long)。后缀可以是大写,也可以是小写,U 和 L 的顺序任意。
- 浮点常量(小数必须包含整数)
- 字符常量
- 字符串常量
- 定义常量
- 整数常量
运算符
- 算数运算符
- 关系运算符
- 逻辑运算符
- 位运算符
- 赋值运算符
- 其他运算符
- sizeof():数据类型的大小
- typeof():返回 class的类型。
- &: 返回变量的地址
- *:变量的指针
- ?: :三元表达式
- is:判断对象是否为某一类型
- as: 强制转换,即使失败也不会抛出异常
判断:
- if
- switch
循环:
- while循环
- for/foreach
- do…while
封装:
- public:允许一个类将其成员变量和成员函数暴露给其他的函数和对象。任何公有成员可以被外部的类访问
- private:允许一个类将其成员变量和成员函数对其他的函数和对象进行隐藏。只有同一个类中的函数可以访问它的私有成员。即使是类的实例也不能访问它的私有成员。
- protected:该类内部和继承类中可以访问。
- internal:同一个程序集的对象可以访问。
- protected internal:3 和 4 的并集,符合任意一条都可以访问。
可空类型
//默认值为0
int a;
//默认值为null
int? b=123;
//如果b为null就赋值2否则c=b
int c= b?? 2;
Console.WriteLine("c的值为{0}", c);
Console.ReadLine();
数组
//初始化数组逐个赋值
int[] arr = new int[10];
arr[0] = 12312;
//初始化数组并赋值
int[] arr1 = { 321, 312, 12312, 12312312, 12312312 };
//使用for循环赋值
for (int i = 0; i < 10; i++)
{
arr[i] = i + 100;
}
//使用forEach取值
foreach (int i in arr)
{
Console.WriteLine("元素的值为{0}", i);
}
结构体
struct Books{
public string id;
public string name;
public string price;
}
static void Main()
{
Books book1;
book1.id = "123";
book1.name = "aaa";
book1.price = "23131";
Console.WriteLine("书信息:书id{0},书名{1},书价格{2}",book1.id,book1.name,book1.price);
Console.ReadLine();
}
- 类与结构的不同点
- 类是引用类型,结构是值类型
- 结构不支持继承
- 结构不能声明默认的构造函数
- 结构体中无法实例属性或赋初始值
- 类与结构的选择
- 当我们描述一个轻量级对象的时候,结构可提高效率,成本更低。数据保存在栈中,访问速度快
- 当堆栈的空间很有限,且有大量的逻辑对象或者表现抽象和多等级的对象层次时,创建类要比创建结构好一些;
- 类与结构的不同点
枚举
//枚举列表中的每个符号代表一个整数值,一个比它前面的符号大的整数值,可自定义每个符号
enum Days { Sun, Mon, Tue, Wed, Thu, Fri, Sat };
static void Enum()
{
int a = (int)Days.Wed;
Console.WriteLine(a);
Console.ReadLine();
}
析构函数
class Test
{
string id;
public Test()
{
Console.WriteLine("构造函数");
}
~Test()
{
Console.WriteLine("析构函数");
}
static void Main()
{
Test test = new Test { id = "123" };
Console.WriteLine("id为{0}", test.id);
Console.ReadLine();
} }
多态性
- 通过在类定义前面放置关键字 sealed,可以将类声明为密封类。当一个类被声明为 sealed 时,它不能被继承。抽象类不能被声明为 sealed。
- 当有一个定义在类中的函数需要在继承类中实现时,可以使用虚方法。虚方法是使用关键字 virtual 声明的。虚方法可以在不同的继承类中有不同的实现。
运算符的重载
class Test2
{
public int length;
//运算符重载
public static Test2 operator +(Test2 a, Test2 b)
{
Test2 test = new Test2 { length = a.length - b.length };
return test;
}
static void Main(string[] args)
{
Test2 test = new Test2 { length = 12 };
Test2 test1 = new Test2 { length = 213 };
Test2 t = test + test1;
Console.WriteLine("t的值{0}", t.length);
Console.ReadLine();
}
}
预处理器指令
define 预处理器
- 条件指令
异常处理
- try catch finally
- 常见异常:
- IO异常
- 空对象
- 类型转换
- 除以0
文件的输入与输出
//字节流读取文件
static void Main(string[] args)
{
StreamReader streamReader =new StreamReader(@"D:\Document\test.txt");
string line;
//读取文件内容
while ((line = streamReader.ReadLine()) != null)
{
//打印出来
Console.WriteLine(line);
}
Console.ReadLine();
}
windows文件系统操作
static void Main(string[] args) {
GetFile(@"D:\ProgramFiles");
Console.ReadLine();
}
//获得某文件夹下的文件名与大小
static void GetFile(String path) {
DirectoryInfo directoryInfo = new DirectoryInfo(path);
DirectoryInfo[] directoryInfo1 = directoryInfo.GetDirectories();
FileInfo[] files = directoryInfo.GetFiles();
if(directoryInfo1!=null) {
foreach(DirectoryInfo directoryInfo2 in directoryInfo1) {
if(directoryInfo2.Name!="app") {
GetFile(path+@"\"+directoryInfo2.Name);
}
}
}
if(files!=null) {
foreach(FileInfo file in files) {
Console.WriteLine("文件名:{0},文件大小{1}",file.Name,file.Length);
}
}
}
特性
预定义特性
1.AttributeUsage
2.Conditional
3.Obsolete:[Obsolete("过时了")]
编译器会给出警告信息[Obsolete("过时了",true)]
编译器会给出错误信息自定义特性
委托
delegate void ConsoleWrite1();
namespace ConsoleApp1 {
class Program { static void Main(string[] args) {
ConsoleWrite1 consoleWrite1 = new ConsoleWrite1(ConsoleWrite);
consoleWrite1();
Console.ReadLine();
}
static void ConsoleWrite() {
Console.WriteLine("测试");
}
委托的用途
static FileStream fs;
static StreamWriter sw;
// 委托声明
public delegate void printString(string s); // 该方法打印到控制台
public static void WriteToScreen(string str) {
Console.WriteLine("The String is: {0}",str);
}
// 该方法打印到文件
public static void WriteToFile(string s) {
fs=new FileStream(@"D:\Document\test.txt",
FileMode.Append,FileAccess.Write);
sw=new StreamWriter(fs);
sw.WriteLine(s);
sw.Flush();
sw.Close();
fs.Close();
}
// 该方法把委托作为参数,并使用它调用方法
public static void sendString(printString ps) {
ps("Hello World");
}
static void Main(string[] args) {
printString ps1 = new printString(WriteToScreen);
printString ps2 = new printString(WriteToFile);
sendString(ps1);
sendString(ps2);
Console.ReadKey();
}
- 指针变量
static unsafe void Test12() {
int i = 0;
int* p = &i;
Console.WriteLine("i的值为{0},内存地址为{1}",i,(int)p);
Console.WriteLine();
}
- 传递指针作为方法的参数
static unsafe void Main() {
int var1 = 10;
int var2 = 20;
Console.WriteLine("var1:{0},var2:{1}",var1,var2);
int* a = &var1;
int* b = &var2;
Swap(a,b);
Console.WriteLine("var1:{0},var2:{1}",var1,var2);
Console.ReadLine();
}
static unsafe void Swap(int* a,int* b) {
int temp =*a;
*a=*b;
*b=temp;
}
- 使用指针访问数组元素
static unsafe void array() {
int[] list = { 10,100,200 };
fixed (int* ptr = list)
/* 显示指针中数组地址 */
for(int i = 0;i<3;i++) {
Console.WriteLine("Address of list[{0}]={1}",i,(int)(ptr+i));
Console.WriteLine("Value of list[{0}]={1}",i,*(ptr+i));
}
Console.ReadKey();
- 指针变量
out参数:一个方法返回多个不同类型的参数
static void Main() {
string s="asd";
int i=Test7(out s);
Console.WriteLine(s);
Console.WriteLine(i);
Console.ReadLine(); } public static int Test7(out string a) {
a="asddddddddddd";
return 1;
}
- params参数
static void Main() {
Params(213,31231,12312,13231,123,1312,312,312,321,3,12,312,12);
}
static void Params(params int[] array) {
int max = array[0];
for(int i = 0;i<array.Length;i++) {
if(array[i]>max) {
max=array[i];
}
}
Console.WriteLine("最大值为{0}",max);
Console.ReadLine();
}
- ref参数
static void Main() {
int i = 30;
Ref(ref i);
Console.WriteLine(i);
Console.ReadKey();
}
static void Ref(ref int a) {
a+=500;
}
- 连接数据库
static void MysqlConnection() {
//配置连接信息
String connstr = "server=localhost;port=3306;user=root;password=123456;database=ztree";
MySqlConnection mySqlConnection = new MySqlConnection(connstr);
//打开连接
mySqlConnection.Open();
//sql语句
string sql = "select * from city";
MySqlCommand mySqlCommand = new MySqlCommand(sql,mySqlConnection);
//执行sql语句
MySqlDataReader mySqlDataReader = mySqlCommand.ExecuteReader();
while(mySqlDataReader.Read()) {
if(mySqlDataReader.HasRows) {
Console.WriteLine("id:{0};name:{1};pid:{2}",mySqlDataReader.GetString(0),mySqlDataReader.GetString(1),mySqlDataReader.GetString(2));
}
}
mySqlConnection.Close();
}
最新文章
- Java学习笔记--循环总结
- [Tool]Inno Setup创建软件安装程序。
- 基于.net mvc 的供应链管理系统(YB-SCM)开发随笔
- HTML常用标签跟表格
- Linux查看BIOS信息
- 【easuyi】---easyui中的验证validatebox自定义
- Qt刷新机制的一些总结(Qt内部画的时候是相当于画在后台一个对象里,然后在刷新的时候调用bitblt统一画,调用window的api并不会影响到后面的那个对象)
- ERP 能够做什么
- 《将博客搬至CSDN》 分类: 勉励自己 2014-09-05 14:29 43人阅读 评论(0) 收藏
- J2SE基本数据结构
- 连锁不平衡LD
- Scrollview回弹效果自定义控件
- nginx与fastdfs配置详解与坑
- rails 布署
- MySQL左连接时 返回的记录条数 比 左边表 数量多
- cmder里ls、pwd、自定义的alias等一系列命令都无法使用
- C#_根据银行卡卡号判断银行名称
- 学习笔记(二)--->;《Java 8编程官方参考教程(第9版).pdf》:第七章到九章学习笔记
- sql里 where和order by一起使用是怎样的顺序
- [Codeforces-888C] - K-Dominant Character
热门文章
- Flutter移动电商实战 --(52)购物车_数据模型建立和Provide修改
- OpenJudge计算概论-奥运奖牌计数
- VS Code 使用技巧[转载]
- Redis Sentinel 高可用服务架构搭建
- Greenwich.SR2版本的Spring Cloud Zipkin实例
- python 函数、参数及参数解构
- 解决访问github等网站慢或下载失败的问题
- QT OpenGLWidget的surfaceFormat
- CF1210A Anadi and Domino
- 【数据库开发】windows环境下通过c++使用redis