前段时间学C#的上转型,泛型,lambda表达式这些应用的理解很费劲。学过之后我多多的练习了几天,接下来继续复习C#的其他一些概念,说实在的这些知识点学过之后很容易忘,但是都是很重要的,所以发表在博客上没事可以多看看复习一下。

第一:扩展方法-----在不更改原来类的基础上,为类添加的方法。扩展方法的行为和静态方法非常的类似,你只能在静态类中声明它们。为声明一个扩展方法,你需要给该方法的第一个参数指定this关键字,而且你的第一个参数必定是你所扩展的类型实例。简单点说就是给现在已经存在的类,在不修改该类源代码的情况修改,向该类中添加某些方法实现特定的功能。其实本人不会喜欢扩展方法,我觉得直接定义一个普通函数就可以实现的。或许我现在接触的实例还少吧。

需要注意的是: 1,扩展方法必须写静态类中

         2,扩展方法必须是静态方法,虽然是静态方法,但是这个扩张方法是为对象扩展的,只能由对象调用。

他的写法如下: public static class 类名

{  public static 返回值 方法名(this 要扩展的类型 对象名[,参数列表])

             { }

}

这里的  this 要扩展的类型 对象名[,参数列表] 这个参数只起到一个说明性作用。

下面是一个扩展方法的实例:

using System;
using System.Collections.Generic;
using System.Linq;
public class StudyExtendMethod
{public static void Main(){string file =  @"E:\FTPPUBLISH\学习资料\KindEditor\kindeditor-v4.0.3\examples\colorpicker.html";Console.WriteLine(file.GetFileType());string  sss = "78.9.09.mp3";Console.WriteLine(sss.GetFileType());People pp = new People();pp.WatchTime("www");string od = pp.GetInfo("张三");Console.WriteLine(od);List<int> list = new List<int>();list.Add(1);list.Add(19);list.Add(34);list.Add(56);list.Add(2);list.Add(90);list.Add(23);list.Add(27);var c = list.GetBigTen(10);foreach(int d in c){Console.WriteLine(d);}}
}
public static class ExtendMethod
{/*this string ss这个参数只起到一个说明性作用。这个扩展方法是为string的对象扩展的,只能有string得对象来使用str值得是使用这个扩展方的对象。*/public static string GetFileType(this string str){string[] strs = str.Split('.');return strs[strs.Length-1];}public static void WatchTime(this People p,string name){Console.WriteLine(name +"  "+DateTime.Now);}public static string GetInfo(this People p,string name){return name+"sssss";}public static IEnumerable<int> GetBigTen(this List<int> list,int a){return list.Where(p=>p>a);}}
public class People
{}

第二:Linq----Linq是语言集成化查询

基础语法有: from 元素 in 集合;where 元素条件;orderby 元素.属性 ascending; group 元素 by 元素.属性;select 元素   注意:如果使用group by语句,则不需要select。

下面是Linq的一个简单的实例:

View Code

View Code
using System;
using System.Collections.Generic;
using System.Linq;
public class StudyLinq3
{public static void Main(){List<Student> list = new List<Student>();list.Add(new Student(){Age=10,Name="Jack",Address="bj"});list.Add(new Student(){Age=67,Name="Mack",Address="郑州"});list.Add(new Student(){Age=23,Name="Dack",Address="USA"});list.Add(new Student(){Age=56,Name="Cack",Address="bj"});list.Add(new Student(){Age=8,Name="Eack",Address="郑州"});list.Add(new Student(){Age=34,Name="Hack",Address="bj"});list.Add(new Student(){Age=18,Name="小红",Address="USA"});var result = ( from p in listwhere p.Age >30select p).OrderByDescending(pp=>pp.Age);foreach(Student s in result){Console.WriteLine(s.Age);    }/*string[] str = {"a","bb","abc","中华民谣","USA"};IEnumerable<string> result = from p in strwhere p.Length>3select p;foreach(string s in result){Console.WriteLine(s);    }IEnumerable<string> result = str.Where<string>(p=>p.Length>3);foreach(string s in result){Console.WriteLine(s);    }*/}
}public class Student
{public int Age{get;set;}public string Name{get;set;}public string Address{get;set;}
}

第三:IO using---I/O的意思是input/output输入和输出他的参照物是程序。输入:值得是通过外接设备向程序输入;输出:程序箱外界设备输出内容。

特别需要理解的是:  File类是一个重要的IO的应用。需要理解以下几个概念:

          FileStream是一个双向流,既可以读也可以写;

           StreamWriter:单向流---输出;

          StreamReader:单向流---输入。

下面是在老师的领导下写的一个综合的实例,其中8个练习的Test()函数,有助于自己深刻理解运用File类相关知识:

View Code

View Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Security.AccessControl;
namespace StudyIO
{class Program{static void Main(string[] args){// Directory.CreateDirectory("d:\\ss");//string[] strs = Directory.GetFiles("d:\\mp3");//foreach (string str in strs) //{//    Console.WriteLine(str);//}string[] strs = Directory.GetFileSystemEntries("e:\\ftppublish");foreach(string s in strs){if (Directory.Exists(s)) {string[] ss = Directory.GetFileSystemEntries(s);foreach(string fs in ss){Console.WriteLine(fs);}}}}static void Test8() {Stream streams = File.Open("D:\\1.cs", FileMode.Open);Stream streamw = File.Open("D:\\1.cs", FileMode.Open);BufferedStream stream = new BufferedStream(streamw);// stream.Write();//   stream.Read();
        }static void Test7() {FileStream stream = File.Open("d:\\1.cs", FileMode.Open);BinaryReader reader = new BinaryReader(stream);byte[] buffer = new byte[1024];int i = 0;string str = "";while ((i = reader.Read(buffer, 0, buffer.Length)) != 0){str += Encoding.UTF8.GetString(buffer, 0, i);}reader.Close();stream.Close();Console.WriteLine(str);}static void Test6(){//FileStream stream = File.Open("D:\\1.cs", FileMode.Append);//StreamWriter writer = new StreamWriter(stream);//writer.Write("你好");//writer.Close();// Test5();
        }static void Test5() {//只是单独操作字符StreamReader reader = new StreamReader("d:\\1.cs",Encoding.UTF8);//先读string str = reader.ReadLine();while (str != null){Console.WriteLine(str);str = reader.ReadLine();}reader.Close();}static void Test4() {//1,读源文件FileStream read = File.Open(@"E:\FTPPUBLISH\学习资料\[电影天堂-www.dy2018.net]龙门飞甲.720p.HD中文字幕.rmvb", FileMode.Open);long count = read.Length;//2,写文件FileStream write = File.Open("d:\\23.rmvb", FileMode.OpenOrCreate);byte[] buffer = new byte[1024 * 1024 * 4];int i = 0;int r = 0;while ((i = read.Read(buffer, 0, buffer.Length)) != 0){r = r + i;string s = (((double)r / count) * 100).ToString("0.00") + "%";Console.WriteLine(s);write.Write(buffer, 0, i);}write.Close();read.Close();}static void Test3() {FileStream fs = File.Open("d:\\1.cs", FileMode.Open);//默认缓冲区为1024,每次从文件(文件流)读1024个字节byte[] buffer = new byte[1024];//当程序读文件的收,把读取的自己从缓冲流的第一个元素开始放。//已经读取的字节数,如果i=0的话,说明已经读取到末尾了,从而结束循环int i = 0;string result = "";while ((i = fs.Read(buffer, 0, 1024)) != 0){Console.WriteLine(i);result += Encoding.UTF8.GetString(buffer, 0, i);}fs.Close();Console.WriteLine(result);}static void Test2() {//写操作FileStream fs =  File.Open("d:\\1.cs",FileMode.Open);//向文件中写内容string str = "这是C#的IO操作";byte[] buffer = Encoding.UTF8.GetBytes(str);fs.Write(buffer,0,buffer.Length);fs.Close();}static void Test() {//File.Create("d:\\1.cs");//File.Copy("d:\\12.mp3", "E:\\刀剑如梦.mp3");//File.Delete("E:\\刀剑如梦.mp3");//File.Move("d:\\12.mp3", "E:\\刀剑如梦.mp3");//  FileAttributes fa = File.GetAttributes("E:\\刀剑如梦.mp3");// Console.WriteLine(fa);//  DateTime dt = File.GetCreationTime("E:\\刀剑如梦.mp3");// Console.WriteLine(dt.ToString());
        }}
}

第四:多线程----进程:一个应用程序就是一个进程,一个进程中可以包含多个线程。以前在学校学操作系统的时候学过有关多线程的一些知识,不过似乎现在学C#涉及的并没有以前学的那么多和深,或许还没达到那个水平吧。

下面是一个很简单很容易理解的多线程源代码:

View Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
namespace StudyThread
{class Program{static void Main(string[] args){//创建一个线程
Thread thread = new Thread(new ThreadA().test);//线程处于就绪状态,没有执行
            thread.Start();for (int i = 0; i < 100; i++){//让当前线程休息500毫秒,当阻塞结束处于就需状态// Thread.Sleep(500);Console.WriteLine("我是主线程" + i);}         }}public class ThreadA {public void test(){for (int i = 0; i < 100; i++){//Thread.Sleep(1000);Console.WriteLine("我是子线程" + i);}}}
}

转载于:https://www.cnblogs.com/lan-net/archive/2012/08/11/2633827.html

C#中的扩展方法,Linq,IO和多线程的定义和实例相关推荐

  1. C#中的扩展方法学习总结

      版权声明:本文由秦元培创作和发表,采用署名(BY)-非商业性使用(NC)-相同方式共享(SA)国际许可协议进行许可,转载请注明作者及出处,本文作者为秦元培,本文标题为C#中的扩展方法学习总结,本文 ...

  2. 技术图文:C# 语言中的扩展方法

    背景 前段时间,在知识星球立了一个Flag,在总结 Leetcode 刷题的第五篇图文时遇到了扩展方法 这个知识点,于是先总结一下. 1.扩展方法概述 扩展方法能够向现有类型"添加" ...

  3. .Net 2.0中使用扩展方法

    大家都知道扩展方法是不能直接在2.0中使用的 需要引用一个‍System.Core的dll 不过现在有更加简单的方法了 只要在工程项目中加入以下代码就OK啦 ‍namespace System.Run ...

  4. 在.net 2.0/3.0程序中使用扩展方法

    .NET Framework 从2.0升级至3.0/3.5中,增加了不少编译器级别的语法糖,如var关键字.自动属性.Lambda表达式.扩展方法等等. 如果使用vs2008发布.net2.0程序时, ...

  5. 在Asp.net MVC framework中使用扩展方法创建Html Helper

    HtmlHelper提供了一些帮助的方法返回一个字符串来生成html. 在System.Web.Mvc.Html命称空间下有一些表单,控件,局部视图Helper方法.我将创建一个生成标签<inp ...

  6. C#参数列表中的this(扩展方法)

    参数列表中this的这种用法是在.NET 3.0之后新增的一种特性---扩展方法.通过这个属性可以让程序员在现有的类型上添加扩展方法(无需创建新的派生类型.重新编译或者以其他方式修改原始类型). 扩展 ...

  7. (转)c# 扩展方法

    扩展方法能够向现有类型"添加"方法,而无需创建新的派生类型,重新编译或以其他方式修改原始类型.扩展方法必须是静态方法,可以像实例方法一样进行调用.且调用同名中实际定义的方法优先级要 ...

  8. 如何使用 C# 扩展方法

    译文链接:https://www.infoworld.com/article/3130492/how-to-work-with-extension-methods-in-c.html C# 在 3.0 ...

  9. 用 .NET 3.5 创建 ToJSON() 扩展方法

    今年早些时候,我通过blog介绍了 C# 和 VB 语言的一项新的扩充特性"扩展方法". 扩展方法让开发者可以向已有的 CLR 类型的公共契约中添加新的方法,而不需要子类化或重新编 ...

最新文章

  1. 一文打尽目标检测NMS | 精度提升篇
  2. python max()_Python Decimal max()用法及代码示例
  3. 关于文献检索的一些思考
  4. 基于当前分支的某一个commit号创建分支
  5. 详细解析Raid0、Raid0+1、Raid1、Raid5四者的区别
  6. .idl与.odl的区别
  7. CreateThread函数
  8. esp虚拟服务器,esp32搭建web服务器
  9. LAMP结构-访问日志
  10. 信号检测与估计理论_永磁同步电机无传感器控制的新策略,提升转子位置检测精度...
  11. 2013Esri全球用户大会QA之ArcGIS未来发展
  12. 电脑上怎么绘制流程图?三分钟快速绘制流程图的秘诀
  13. 【干货总结】分层强化学习(HRL)全面总结
  14. python椭圆形骨料_一种基于python再生混凝土三维随机球形骨料模型的构建方法与流程...
  15. Flink部署 完整使用 (第三章)
  16. 在虚拟机上安装mysql
  17. 小白入门miniconda安装教程
  18. lag与lead函数
  19. 计算机应用技术教程计应吗,计算机应用技术教程
  20. 网页要让它自适应各种手机屏幕宽度大小要怎么设置

热门文章

  1. 这个太有意思了,程序员可以消遣娱乐
  2. docker命令及挂载
  3. Myeclipes连接Mysql数据库配置
  4. rabbitmq学习——队列
  5. [php入门] 4、HTML基础入门一篇概览
  6. 数据探查_数据科学家,开始使用探查器
  7. php模板如何使用,ThinkPHP如何使用模板
  8. java建立tcp服务器长连接_B/S 架构下后端能否建立 TCP 长连接?
  9. gitlab设置邮件服务器_如何设置您自己的一次性电子邮件服务器
  10. vsc 搜索特定代码_特定问题的通用解决方案:何时编写代码以及何时编写代码...