您的当前位置:首页>全部文章>文章详情

【C#】C#超急速举例入门-适用有C/C++语言基础

CrazyPanda发表于:2024-01-30 23:00:43浏览:350次TAG:

前提

编程环境:vs2022

电脑系统:win10

学习目的:

能看懂c#,不纠结各种细节,快速适应开发。

入门篇

程序结构


1.png

变量类型

类似c语言,不掌握细节,int,float,double都有。

输入输出

Console.WriteLine("变量0:{0}", para0);
var a=Console.ReadLine();

1.png

 运算符

几乎相同。

sizeof();

typeof();

取地址,取值:&,*;

三元运算符: ? :

判断类型:is

强制转换:as。

注释

多行与当行还有文档

两种交互模式

.net 平台:c/s,b/s。

中级篇

方法

基本同函数。

参数

引用参数,输出参数,值传递参数。

体现为ref,out以及普通参数。

void getnames( int id,out name,ref nickname)


成员:字段、方法

属性:例子:public string name{ get;set;}

扩充:public string name{ get{return name;}set{name=value;}}

name是此类中的字段。

属性用于保护字段。

属性(Property)的访问器(accessor)包含有助于获取(读取或计算)或设置(写入)属性的可执行语句。访问器(accessor)声明可包含一个 get 访问器、一个 set 访问器,或者同时包含二者。

总而言之,我们给属性赋值,最终去向了字段。

派生类

class 派生类 :父类(基类)

抽象

抽象关键字:abstract

未完成的方法或者类等等。

抽象类,抽象属性,抽象方法。

接口

定义类似于类,可以搭配实现多继承。

多态与重载

方法多态

同一个事件发生在不同的对象上会产生不同的结果。

运算符重载

方法重载

public int Add(int a, int b, int c)  
        {  
            return a + b + c;  
        }  
        public int Add(int a, int b)  
        {  
            return a + b;  
        }


对象的多态特性,以及多继承举例

 1.png

 在参数类型为fulei1的情况下,输入进去的是zilei也可,说明了zilei也是fulei1,但是反之不行,也就是zilei类型的参数不能填入fulei1.

    internal class Program
    {
        static void Main(string[] args)
        {
            zilei zl = new zilei();
            zl.draw2(zl);
        }
    }
 
    public abstract class fulei1 {
 
        public abstract void draw();
         
    }
    public class fulei2 { 
    public virtual void draw2() {
            Console.WriteLine("虚方法");
        }
    }
    public interface jiekou {
        void  draw2(fulei1 );
    }
 
    public class jiekou2 {
        void draw2() { }
    }
 
    public class zilei:fulei1,jiekou
    {
        public override void draw() {
            Console.WriteLine();
        }
        public void draw2(fulei1 zl) {
            Console.WriteLine();
 
        }
    }


高级篇

文件读写

static void Main(string[] args)
        {
            FileStream fs = new FileStream("text.txt",FileMode.Open,FileAccess.ReadWrite);
            byte[] buffer = new byte[4096];//4kb读取
            int r=fs.Read(buffer,0,buffer.Length);
            Console.WriteLine(r);
            string s=Encoding.UTF8.GetString(buffer,0,r);
            Console.WriteLine("内容1:"+s);
            fs.Close();
            Console.ReadLine();
            fs.Dispose();
            //写入
            FileStream fs2 = new FileStream("text.txt", FileMode.Open, FileAccess.ReadWrite);
            string str = "my test2!";
            byte[] bufferout = Encoding.UTF8.GetBytes(str);
            fs2.Write(bufferout,0,str.Length);
            Console.WriteLine("写入"+str);
            fs2.Close();
            Console.ReadLine();
            fs2.Dispose();
 
            FileStream fs3 = new FileStream("text.txt", FileMode.Open, FileAccess.ReadWrite);
            byte[] buffer3 = new byte[4096];//4kb读取
            int r1 = fs3.Read(buffer, 0, buffer.Length);
            Console.WriteLine(r1);
            string s1 = Encoding.UTF8.GetString(buffer, 0, r1);
            Console.WriteLine("内容2:"+s1);
 
            fs3.Close();
            Console.ReadLine();
            fs3.Dispose();
        }

函数具体参数可以百度。

读取大文件,复制文件

            string filesource = @"录音.m4a";
            string target = @"复制.m4a";
 
            FileStream fs = new FileStream(filesource,FileMode.Open,FileAccess.Read);
 
            FileStream fsout=new FileStream(target,FileMode.Create,FileAccess.Write);
 
            fs.CopyTo(fsout);
            
            
            
            
            
            fs.Close();
            fsout.Close();
            fs.Dispose();
            fsout.Dispose();

复制文件2

internal class Program{static void Main(string[] args){string filesource = @"录音.m4a";string target = @"复制.m4a";//using会自动释放资源using (FileStream fs = new FileStream(filesource, FileMode.Open, FileAccess.Read)){//读//写using (FileStream fsout = new FileStream(target, FileMode.OpenOrCreate, FileAccess.ReadWrite)){//缓存字节流数组byte[] buffer = new byte[4*1024*1024];//循环读取4mbwhile (true){//实际读取字节数int r = fs.Read(buffer, 0, buffer.Length);if (r == 0) break;//输出fsout.Write(buffer, 0, r);}}}}} internal class Program
    {
        static void Main(string[] args)
        {
            string filesource = @"录音.m4a";
            string target = @"复制.m4a";
            //using会自动释放资源
            using (FileStream fs = new FileStream(filesource, FileMode.Open, FileAccess.Read))
            {
                //读
 
                //写
                using (FileStream fsout = new FileStream(target, FileMode.OpenOrCreate, FileAccess.ReadWrite))
                {
                    //缓存字节流数组
                    byte[] buffer = new byte[4*1024*1024];
                    //循环读取4mb
                    while (true)
                    {
                        //实际读取字节数
                        int r = fs.Read(buffer, 0, buffer.Length);
                        if (r == 0) break;
                        //输出
                        fsout.Write(buffer, 0, r);
                    }
                   
 
                }
 
 
            }
        }
    }

文字文本读写:操作文本streamreader与streamwriter

namespace streamwr
{
    internal class Program
    {
        static void Main(string[] args)
        {
            string path = @"小说.txt";
            using (StreamReader sr=new StreamReader(path)) 
            {
                //读全文
                string quanwen=sr.ReadToEnd();
                //从头开始
                sr.BaseStream.Seek(0, SeekOrigin.Begin);
               
                //读一行
                string yihang=sr.ReadLine();
                //同上
                sr.BaseStream.Seek(0, SeekOrigin.Begin);
 
                //读一个字符,结果为-1时说明文件结尾。还有其他用法,自行百度。
                string zifu=sr.Read().ToString();
                Console.WriteLine("全文:\n"+quanwen+"\n一行:\n"+yihang+"\n字符:\n"+zifu);
            }
            Console.ReadKey();
        }
    }
}

1.png

 1.png

 关于read()的多种用法,以及就基本流返回文件头的探讨:

 
namespace streamwr
{
    internal class Program
    {
        static void Main(string[] args)
        {
            string path = @"小说.txt";
            using (StreamReader sr=new StreamReader(path)) 
            {
                //读全文
                string quanwen=sr.ReadToEnd();
                //从头开始
                sr.BaseStream.Seek(0, SeekOrigin.Begin);
               
                //读一行
                string yihang=sr.ReadLine();
                //同上,但是根据结果,这里是无效的
                sr.BaseStream.Seek(0, SeekOrigin.Begin);
                //Console.WriteLine("\n回到开头\n");
                string yihang2 = sr.ReadLine();
 
                //读一个字符,结果为-1时说明文件结尾。还有其他用法,自行百度。
                sr.BaseStream.Seek(0, SeekOrigin.Begin);
                char[] buff=new char[2];
                //这里应该也是从头开始读
                char zifu= (char)sr.Read();               //(char)sr.Read(buff,0,2);
 
                sr.BaseStream.Seek(0, SeekOrigin.Begin);
                int a1=sr.Read();
                
                int a2=sr.Read();
                Console.WriteLine(a1+"\n"+a2);
                char zifu2 = (char)a1;
                char zifu3 = (char)a2;
 
 
                Console.WriteLine("全文:\n"+quanwen+"\n回到开头读\n一行:\n"+yihang+"\n回到开头\n读一行:\n" + yihang2 + "\n字符1:\n"+zifu+ "\n字符2:\n" + zifu2 + "\n字符3:\n" + zifu3);
            }
            Console.ReadKey();
        }
    }
}

1.png

 1.png

 1.png

也就是说,read是会读到换行和回车的。并且返回文件头部的方法只在第一次read to end起效了,虽然一般来说不应该读完一行想回头再读就是了。

在文件后添加还是覆盖文件的选项在path后的true上体现。true为添加,false为覆盖。

 using (StreamWriter sw = new StreamWriter(path,true)) {
                sw.WriteLine("我喜欢你");
            
            
            }

访问权限修饰

可以修饰类的:public和internal。

其他:private,protect,internal。

public:所有对象都可以访问;
private:对象本身在对象内部可以访问;
protected:只有该类对象及其子类对象可以访问
internal:同一个程序集的对象可以访问;
protected internal:访问限于当前程序集或派生自包含类的类型。

序列化与反序列化

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using System.Threading.Tasks;
 
namespace xuliehua
{
    internal class Program
    {
        static void Main(string[] args)
        {
            person p = new person();
            p.Age = 1;
            p.Sex = "man";
            p.Name = "wuyi";
            using (FileStream fsw = new FileStream("text.txt", FileMode.OpenOrCreate, FileAccess.ReadWrite)) 
            {
                //开始序列化对象
                BinaryFormatter binaryFormatter = new BinaryFormatter();
                binaryFormatter.Serialize(fsw, p);
                
            }
            Console.WriteLine("序列化成功");
 
            //接受文件,反序列化
            using (FileStream fsr = new FileStream("text.txt",FileMode.Open,FileAccess.Read)) 
            {
                BinaryFormatter binaryFormatter = new BinaryFormatter();
                person p2=(person)binaryFormatter.Deserialize(fsr);
                Console.WriteLine(p2.Name);
            }
            Console.WriteLine("反序列化成功");
            Console.ReadKey();
 
        }
    }
 
    [Serializable]
    class person {
        string _name;
        int age;
        string sex;
        public string Name{ get { return _name; } set { _name = value; } }
        public int Age { get { return age; }set { age = value; } }
        public string Sex { get { return sex; } set { sex = value; } }
    }
}

部分类

partial关键字

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace 部分类
{
    internal class Program
    {
        static void Main(string[] args)
        {
        }
    }
     public partial class person{
        string myname;
    }
    public partial class person
    {
        string getname() {
            return myname;
                }
    }
}

密封类

拒绝继承

sealed关键字

1.png

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace 部分类
{
    internal class Program
    {
        static void Main(string[] args)
        {
        }
    }
     public sealed  class person{
        string myname;
    }
    
    public class person2 :person
    { }
}

 接口的显式实现

 
namespace 显式实现接口
{
    internal class Program
    {
        static void Main(string[] args)
        {
        }
        public class bird : flyable
        {
            public void fly() {
                Console.WriteLine("我会飞");
            }
            //显式实现就可以避免与方法重名
             void flyable.fly(){
                Console.WriteLine("接口的飞");
            }
        }
        public interface flyable {
 
             void fly();
        }
    }
}


原文链接https://blog.csdn.net/m0_54138660/article/details/132327278

猜你喜欢

【C#】C# Winfrom 常用功能整合-2
目录Winfrom 启动一个外部exe文件,并传入参数Winform ListBox用法HTTP下载文件(推荐)Winform HTTP下载并显示进度Winform HTTP下载文件Winform 跨线程访问UI组件Winform 改变文字的颜色和大小Winfrom 启动一个外部exe文件,并传入参数在我们常用的一些软件中,经常有些软件,双击之后根本打不开,这是因为启动时做了限制,我们需要传入一些参数才能打开,在工作中,这个需求也可以用在软件的自动更新上,
发表于:2024-02-02 浏览:339 TAG:
【C#】C# Winform 热更新 基于ECSharp框架
目录一、简介二、 ECSharp热更新演示三、Winform 热更新实战结束一、简介ECSharp (原:EasySharpFrame)github 地址:https://github.com/suxf/ECSharp介绍:1.HTTP服务 2.Websocket服务 3.HyperSocket<自定义Socket服务> 4.TimeFlow<时间流> 5.Sqlserver数据库助手 6.Mysql数据助手 7.Redis数据库助手 8.Log功能 9.热更新
发表于:2024-02-05 浏览:305 TAG:
【C#】c#Windows桌面程序退入托盘及右键菜单
一. 退出托盘功能窗体加组件notifyIcon修改属性,属性中加入要在托盘显示时呈现的图标。添加MouseClick事件编辑代码:private void Form_Main_FormClosing(object sender, FormClosingEventArgs e) {     e.Cancel = true;     this.Hid
发表于:2024-01-28 浏览:401 TAG:
【C#】C#二分查找(迭代与递归)
        二分搜索被定义为一种在排序数组中使用的搜索算法,通过重复将搜索间隔一分为二。二分查找的思想是利用数组已排序的信息,将时间复杂度降低到O(log N)。二分查找算法示例 何时在数据结构中应用二分查找的条件: 应用二分查找算法:         1、数据结构必须是有序的。         2、访问数据结构的任何元素都
发表于:2024-03-11 浏览:315 TAG:
【C#】Winform NanUI 0.88版本 用官方源码搭建原生态开发环境
目录一、需求二、搭建原生开发环境1.导入源码2.解决源码报错错误1错误23.导入其他项目4.官方Demo运行效果三、创建自己的NanUI项目1.新建项目2.导入NanUI.Runtime扩展包3.添加NanUI程序集的引用4.MainIndex主界面相关代码5.Program程序入口相关代码6.读取本地前端文件的处理四、测试项目效果五、结束一、需求NanUI 插件确实很方便,但想改其中的需求怎么办,下面就来自己搭建NanUI 原生开发环境,在此很感谢作者免费的开源。官方源码地址:GitHub -
发表于:2024-02-08 浏览:493 TAG:
【C#】C# 解压zip文件,并覆盖
 使用ZipFile.ExtractToFile方法,并将overwrite参数设置为true,这样可以覆盖同名的目标文件。代码如下:using System; using System.IO; using System.IO.Compression;   namespace ConsoleApplication {     class Program   &nbs
发表于:2024-01-29 浏览:591 TAG:
【C#】C# Winform自动更新
目录一、需求二、更新文件列表生成器三、软件启动器1.判断是否需要更新2.文件下载3.执行 下载,覆盖,删除任务4.执行结果四、搭建更新服务器1.启动服务器2.新建项目本体3.给启动软件加密4.修改版本号五、整体测试1.生成更新文件2.软件更新3.下载最新的版本4.打开软件本体5.总结结束当前项目已停止维护,推荐使用 FTP 版自动更新C# 自动更新(基于FTP)_c# 程序自动升级-CSDN博客一、需求在Unity里面,有XLua,ILRuntime 这样的热更新框架,有Unity经验的人都知道
发表于:2024-02-01 浏览:346 TAG:
【C#】C# 自动更新(基于FTP)
目录一、前言二、功能的实现1.本地黑名单2.读取配置文件3.读取 FTP 文件列表4.读取本地文件5.匹配更新6.版本的切换三、环境搭建四、常见问题2023.12.30 更新结束效果启动软件后,会自动读取所有的 FTP 服务器文件,然后读取本地需要更新的目录,进行匹配,将 FTP 服务器的文件同步到本地Winform 界面一、前言在去年,我写了一个 C# 版本的自动更新,这个是根据配置文件 + 网站文件等组成的框架,以实现本地文件的新增、替换和删除,虽然实现了自动更新的功能,但用起来过于复杂,代
发表于:2024-02-03 浏览:365 TAG:
【C#】C# Winform 相册功能,图片缩放,拖拽,预览图分页
一、前言在一些项目中也会用到预览图片的功能,至于为什么有一个添加图片的按钮,是因为有些项目,比如视觉相关的项目,摄像头拍摄图片,然后显示在界面上,拍一次显示一张。另一个,就是分页功能,当预览图位置不够用时就会用到。当前软件的功能1.添加图片如果8个预览图都满了,会自动分页,就可以点击上一页,或者下一页了。2.点击预览图显示大图点击预览图,之前的拖拽和放大会自动复位3.大图可以拖拽,放大,缩小如果图片比较小,有这个功能就看到图片的更多细节了。4.图片倒序排列最后拍摄的图片,始终显示在前面,方便用户
发表于:2024-02-02 浏览:321 TAG:
【C#】C# Winform GDI+ 绘图
目录一、概述二、绘图1.画直线2.画矩形3.画圆、圆弧4.画扇形5.画多边形6.绘制字符串7.填充图形结束一、概述Graphics类是GDI+技术的一个基本类。GDI+(Graphics Device Interface)是.NET框架的重要组成部分,提供对二维图形图像和文字排版处理的支持。GDI+相关的类分布在下列命名空间中: System.Drawing:提供了最基本的绘图功能(比如画直线、矩形、椭圆等); System.Drawing.Drawing2D: 提供了高级的二维和矢量绘图功能(
发表于:2024-02-18 浏览:387 TAG: