从头学C# 7(笔记)

2019-04-15  本文已影响0人  老哥深蓝

数据类型

具有某种数据类型的数据,称为这个数据类型的实例。如学生是类型,王小二是实例。
类型可以拥有自己的成员(属性和方法等)。 类型type指的是类、结构、接口、委托、枚举。
C#的类型包括值类型和引用类型,其中重要的类型定义成基元类型。
C#的预定义类型:8种整数型,2种2进质浮点型,1种金融十进制浮点型,1种布尔型,1种字符型。

int long char double datetime 属于结构 枚举emnu
string class 接口 委托
引用类型只可以为null,值类型不可以
string str=null;
string str1='//n';//转义

实际对象的计算机语言表示,拥有许多成员,最重要的是属性和方法。
对象是类的实例
属性存储信息和数据
方法是供我们可用的函数

运算符和分支结构

//一元运算符
+ - ++ --
int i=i++  i=i-- //放后是执行完再加一
//二元运算符
+ - * / %
== != < > <= >= //关系运算
&& ||
+= -= *= /= %=
//三元运算符
? :
//运算符重载

//分支结构
if else
switch case

方法与参数

方法method

用来把一些相关的语句收集在一起的代码块。方法必须有方法名,拥有0个或多个参数,可以有0或1个输出,C#7后可以有多个输出,在这之前借助Out关键字实现多输出。
一般定义不返回函数值得叫方法,返回值的叫函数。
C#所有数据结构:类 结构 接口,可以有方法。 枚举 不可以写方法,委托的方法是固定的不可以自定义

方法的定义/签名signature

//定义
public int a(params int[] b){
    return 0;
}
//调用
a(1,2,3)//可变长度

数组与循环

面向对象和类

public class car{
    public colors color{
        get{
            return this;
        }
        //set打开后,get也要打开
        };
        set{
           this=value;//默认value
        };
    }
}

结构

继承inheritance

public class a{
    int x;
    int y;
    public a(){}
    public a(int x,int y){
        this.x=x;
        this.y=y;
    }
}
public class b:a{
    public b(int x,int y):base(x,y){}
}

多态和接口

第三部分 数据结构

  • C# 2.0 泛型
  • C# 3.0 linq
  • C# 4.0 动态语言

集和与泛型

public a<T,K>(T b,K c) where T: new() where K:struct

算法介绍

CLR(公共语言运行时)

委托和事件

委托实际上就是函数指针,将不同函数像变量一样传递,供不同参数调用。

匿名类型、var关键字和扩展方法

//匿名类型
var s = new { name = "小李", age = 15 };
var b=s.name;
Console.WriteLine(s.GetType());//返回<>f__AnonymousType0`2[System.String,System.Int32]
//扩展方法
public static class StringExtension //1、在静态类中,2、一般用Extension做为后缀
    {
        public static bool isHuiwen(this string input)
        {
            //算法
            return true;
        }
    }

bool a = "string".isHuiwen//调用

匿名方法和lambda表达式

//old:泛型委托实现字符串判定
static void Main(string[] args)
{
    var data = new List<string> { "string", "this", "the", "a", "dog" };
    var func = new Func<string, bool>(Predicate);//泛型委托实现,string输入的参数,bool返回的类型
    
    //使用匿名方法的方式,不用在单独定义Predicate这个方法
    var func1 = new Func<string, bool>(delegate (string input)
    {
        return input.Length >= 4;
    });
    //lamdbe表达式写法
    Func<string, bool> func2 =(input)=>{
    return input.Length >= 4;
    };
    
    foreach (var word in data)
    {
        if (func(word))
        {
            Console.WriteLine(word);
        }
    }
    Console.ReadKey();
}
public static bool Predicate(string input)//使用匿名方法就这用写这个了
{
    return input.Length >= 4;
}

LinQ Language-Integrated Query语言结构化查询

反射和动态语言运行时

C#5

C#2泛型
C#3LinQ
C#4动态语言运行时
C#5多线程

C#6-7

C#6-2015年7月,Vs2015 .net framework4.6语法糖
C#7-2017年3月,Vs2017 .net framework4.6.2增加元组和模式匹配,更具有函数式语言特性

string name="张三";
string s="123";
//old
string.Format("{0}-{1}",name,s);
//new
$"{name}-{s}";
上一篇 下一篇

猜你喜欢

热点阅读