要学好C#,基础知识的重要性不言而喻,现将常用到的一些基础进行总结,总结如下:

01. 数据类型转换

  强制类型转换(Chart--> int):

  char cr='A';   int i = (int)(cr);

02. 委托/匿名函数/Lamda表达式:

委托是匿名函数的起源,Lamda表达式又是匿名函数的升华。这些又是如何体现的呢,请看:

委托示例:

namespace Delegate
{
class Program
{
public delegate void TDelegate(int i, int j); static void Caculator(int i, int j)
{
Console.WriteLine(i * j * i * j);
} public static void InvokeDE()
{
TDelegate td = new TDelegate(Caculator);
td.Invoke(3, 5);
} static void Main(string[] args)
{
InvokeDE();
Console.ReadLine();
}
}
}

匿名函数示例:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace CSP
{
class Program
{
public delegate void MyDelegate(int x, int y);
static void Main(string[] args)
{
MyDelegate md = delegate(int x, int y)
{
Console.WriteLine(x + y);
};
md(10, 100);
Console.ReadLine();
}
}
}

Lamda表达式(实际就是一个函数)示例:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace CSP
{
class Program
{
private static void LamdaExpression()
{
int[] InitArr = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
int ResCount = InitArr.Where(n => n > 6).Count();
Console.WriteLine(ResCount);
}
static void Main(string[] args)
{
LamdaExpression();
Console.ReadLine();
}
}
}

03. 泛型Gereric:

泛型是C#一个非常重要的用法,必须熟记于心:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace CSP
{
class Program
{
public static void GenericFunction()
{
int i = 10;
string HI = "Hello World!"; TestGC<int> tg_int = new TestGC<int>(i);
TestGC<string> tg_string = new TestGC<string>(HI); Console.WriteLine(tg_int.t.ToString());
Console.WriteLine(tg_string.t.ToString());
}
static void Main(string[] args)
{
GenericFunction();
Console.ReadLine();
}
} public class TestGC<T>
{
public T t;
public TestGC(T t)
{
this.t = t;
}
}
}

04.  虚方法Virtual:

以前总觉得自己掌握的很好了,最近看了一些文章才对Virtual的执行顺序有了更深的理解,为了加深印象,我添加了示例图并特地将本篇文在此处进行了引用:

class A
{
public virtual void Func() // 注意virtual,表明这是一个虚拟函数
{
Console.WriteLine("Func In A");
}
}
class B : A // 注意B是从A类继承,所以A是父类,B是子类
{
public override void Func() // 注意override ,表明重新实现了虚函数
{
Console.WriteLine("Func In B");
}
}
class C : B // 注意C是从B类继承,所以B是父类,C是子类
{
}
class D : A // 注意D是从A类继承,所以A是父类,D是子类
{
public new void Func() // 注意new,表明覆盖父类里的同名类,而不是重新实现
{
Console.WriteLine("Func In D");
}
}
class E : D // 注意E是从D类继承,所以D是父类,E是子类
{ }
class F : A
{
private new void Func() //注意new关键字前有private修饰符,故该隐藏只在F类内有效
{
Console.WriteLine("Func In F");
} public void Func2()
{
Func(); //在F类内隐藏了基类的Func方法,故此处调用的private new void Func()
}
} static void Main(string[] args)
{
A a; // 定义一个a这个A类的对象.这个A就是a的申明类
A b; // 定义一个b这个A类的对象.这个A就是b的申明类
A c; // 定义一个c这个A类的对象.这个A就是c的申明类
A d; // 定义一个d这个A类的对象.这个A就是d的申明类
A e; // 定义一个e这个A类的对象.这个A就是e的申明类
A f; // 定义一个f这个A类的对象.这个A就是f的申明类
a = new A(); // 实例化a对象,A是a的实例类
b = new B(); // 实例化b对象,B是b的实例类
c = new C(); // 实例化c对象,C是c的实例类
d = new D(); // 实例化d对象,D是d的实例类
e = new E(); // 实例化e对象,E是e的实例类
f = new F(); // 实例化f对象,F是f的实例类
Console.WriteLine("a.Func();");
a.Func(); // 执行a.Func:1.先检查申明类A 2.检查到是虚拟方法 3.转去检查实例类A,就为本身 4.执行实例类A中的方法 5.输出结果 Func In A
Console.WriteLine("b.Func();");
b.Func(); // 执行b.Func:1.先检查申明类A 2.检查到是虚拟方法 3.转去检查实例类B,有重载的 4.执行实例类B中的方法 5.输出结果 Func In B
Console.WriteLine("c.Func();");
c.Func(); // 执行c.Func:1.先检查申明类A 2.检查到是虚拟方法 3.转去检查实例类C,无重载的 4.转去检查类C的父类B,有重载的 5.执行父类B中的Func方法 5.输出结果 Func In B
Console.WriteLine("d.Func();");
d.Func(); // 执行d.Func:1.先检查申明类A 2.检查到是虚拟方法 3.转去检查实例类D,无重载的(这个地方要注意了,虽然D里有实现Func(),但没有使用override关键字,所以不会被认为是重载) 4.转去检查类D的父类A,就为本身 5.执行父类A中的Func方法 5.输出结果 Func In A
Console.WriteLine("e.Func();");
e.Func(); // 执行e.Func:E继承D,E.Func没有重写父类中的方法,相当于执行父类D中的Func方法,输出结果 Func In A
Console.WriteLine("f.Func();");
f.Func(); // 执行f.Func:F类中虽然隐藏了基类中的Func方法,但是有private修饰符,该隐藏只在F类范围内有效。执行f.Func相当于执行其基类中的Func方法,输出结果 Func In A D d1 = new D();
Console.WriteLine("d1.Func();");
d1.Func(); // 执行D类里的Func(),输出结果 Func In D E e1 = new E();
Console.WriteLine("e1.Func();");
e1.Func(); // 执行E类里的Func(),输出结果 Func In D F f1 = new F();
Console.WriteLine("f1.Func();");
f1.Func(); // 执行F类里的Func(),输出结果 Func In A
Console.WriteLine("f1.Func2();");
f1.Func2(); // 执行F类里的Func2(),输出结果 Func In F Console.ReadLine();
}

05. New和Override的用法:

New是新建一个新方法,对旧方法进行了屏蔽,而Override只是对父类中的方法进行了覆盖,具体详细用法参见4. Virtual用法示例;

06. foreach用法

foreach遍历访问的对象需要实现IEnumerable接口或声明GetEnumerator方法的类型;

  MSDN上的例子:

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace CSP
{
public class Person
{
public string firstName;
public string lastName;
public Person(string fName, string lName)
{
this.firstName = fName;
this.lastName = lName;
}
} public class People : IEnumerable
{
private Person[] _people;
public People(Person[] pArray)
{
_people = new Person[pArray.Length]; for (int i = ; i < pArray.Length; i++)
{
_people[i] = pArray[i];
}
} // Implementation for the GetEnumerator method.
IEnumerator IEnumerable.GetEnumerator()
{
return (IEnumerator)GetEnumerator();
} public PeopleEnum GetEnumerator()
{
return new PeopleEnum(_people);
}
} public class PeopleEnum : IEnumerator
{
public Person[] _people; int position = -; public PeopleEnum(Person[] list)
{
_people = list;
} public bool MoveNext()
{
position++;
return (position < _people.Length);
} public void Reset()
{
position = -;
} object IEnumerator.Current
{
get
{
return Current;
}
} public Person Current
{
get
{
try
{
return _people[position];
}
catch (IndexOutOfRangeException)
{
throw new InvalidOperationException();
}
}
}
} class App
{
static void Main()
{
Person[] peopleArray = new Person[]
{
new Person("John", "Smith"),
new Person("Jim", "Johnson"),
new Person("Sue", "Rabon"),
}; People peopleList = new People(peopleArray);
foreach (Person p in peopleList)
Console.WriteLine(p.firstName + " " + p.lastName); Console.ReadLine(); }
}
}

下面例子是对上面的改动,只保留了对GetEnumerator()方法的实现,移除了对IEnumerable接口和IEnumerator接口的继承,执行结果同上例一样:

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace CSP
{
public class Person
{
public string firstName;
public string lastName;
public Person(string fName, string lName)
{
this.firstName = fName;
this.lastName = lName;
}
} public class People
{
private Person[] _people;
public People(Person[] pArray)
{
_people = new Person[pArray.Length]; for (int i = ; i < pArray.Length; i++)
{
_people[i] = pArray[i];
}
} public PeopleEnum GetEnumerator()
{
return new PeopleEnum(_people);
}
} public class PeopleEnum
{
public Person[] _people; int position = -; public PeopleEnum(Person[] list)
{
_people = list;
}
public bool MoveNext()
{
position++;
return (position < _people.Length);
} public Person Current
{
get
{
try
{
return _people[position];
}
catch (IndexOutOfRangeException)
{
throw new InvalidOperationException();
}
}
}
} class App
{
static void Main()
{
Person[] peopleArray = new Person[]
{
new Person("John", "Smith"),
new Person("Jim", "Johnson"),
new Person("Sue", "Rabon"),
}; People peopleList = new People(peopleArray);
foreach (Person p in peopleList)
Console.WriteLine(p.firstName + " " + p.lastName); Console.ReadLine(); }
}
}

PS.

  A.实现实现IEnumerable接口的同时就必须实现IEnumerator接口;

B.不一定要实现IEnumerable接口,但一定要实现GetEnumrator方法

对于上述的功能,可以也尝试使用语法糖(便捷写法)C# yield来进行实现;

07. 静态构造函数

  静态构造函数,也称静态代码块,主要用于初始化静态变量,示例如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace CSP
{
public class StaticBlock
{
public string Title;
static StaticBlock()
{
Console.WriteLine("Here is the static block,only can be called for 1 time!");
} public StaticBlock(string Title)
{
this.Title = Title;
}
}
class Program
{ static void Main(string[] args)
{
StaticBlock sb_morning = new StaticBlock("Good morning!");
Console.WriteLine(sb_morning.Title);
StaticBlock sb_afternoon = new StaticBlock("Good afternoon!");
Console.WriteLine(sb_afternoon.Title); Console.ReadLine();
}
}
}

  静态构造函数具有如下特点(来自网络):

  A.静态构造函数既无访问修饰符亦无参数;

B.如果没有编写静态构造函数,而这时类中包含带有初始值设定的静态字段,那么编译器会自动生成默认的静态构造函数

C.在创建第一个类实例或任何静态成员被引用时,.NET将自动调用静态构造函数来初始化类,即无法直接调用与控制静态构造函数。

D.如果类中包含用来开始执行的 Main 方法,则该类的静态构造函数将在调用 Main 方法之前执行

E.如果类中的静态字段带有初始化,则静态字段的初始化语句将在静态构造函数之前运行。

F.一个类只能有一个静态构造函数,不可以被继承且最多只运行一次

08. 反射typeof/GetType

  typeof:获取类运行时的类型方法列表,参数只能为类名,用法typeof(类名);

GetType:获取类运行时的类型方法列表,由对象调用,用法:obj.GetType();

09. where T : class

   主要用来对接口进行限制,如下所示,限制接口IDataComponentBase<T>中的T必须为一个引用类型,如类,接口,数组;

  public interface IDataComponentBase<T> where T : class

10. Guid对象赋值:

Guid gd = new Guid("3a4f38a3-e064-e611-80d6-080027c84e1f");

11. Dispose():

在使用using方法结束时会自动调用Dispose(),以便显示释放非托管资源(前提是该当前类必须实现接口:IDisposable);

12. 保留两位小数:

Decimal OVNum,NCNum;

... ...

Decimal TotNum = OVNum + NCNum;
      Decimal d = NCNum * 100 / TotNum;
      e.Result = Decimal.Round(d, 2);

13. 利用List自带的Sort进行排序:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace CA
{
class Program
{
static void Main(string[] args)
{
List<Light> lts = new List<Light>();
Light lt0 = new Light();
lt0.LTypeName = "Filament";
lt0.W = ; Light lt1 = new Light();
lt1.LTypeName = "Common";
lt1.W = ; Light lt2 = new Light();
lt2.LTypeName = "Efficient";
lt2.W = ; lts.Add(lt0);
lts.Add(lt1);
lts.Add(lt2);
Console.WriteLine("Before sort:");
foreach (Light l in lts)
{
Console.WriteLine(l.LTypeName+":"+l.W);
} //A~Z
//lts.Sort((x, y) => x.LTypeName.CompareTo(y.LTypeName));
//Z~A
lts.Sort((x, y) => -x.LTypeName.CompareTo(y.LTypeName));
Console.WriteLine("After sort:");
foreach (Light l in lts)
{
Console.WriteLine(l.LTypeName + ":" + l.W);
} Console.ReadLine();
}
} public class Light
{
public string LTypeName { get; set; }
public int W { get; set; }
}
}

14. 数组,ArrayList,List的区别:

  数组的优点是可以存储多个维度的记录,且连续存放,缺点是需要在定义时指定数组的长度,且定义好后不能扩展;

ArrayList在定义时不需要指定长度不需要定义存入的数据的数据类型,可以自由扩展。所以ArrayList可以存放不同类型的数据(以object存入,要进行装箱操作)到ArrayList,所以ArrayList为非类型安全的;

使用如下所示:

  ArrayList al = new ArrayList();

  al.Add(100);

  al.Add("Hello");

   List与ArrayList一样,在定义时不需要指定长度,可以自由扩展。同时,在声明List时,需要定义存入的数据的数据类型,实现了类型安全;

   ArrayList的命名空间:System.Collections.ArrayList

List的命名空间:System.Collections.Generic.List

15. const/readonly

      用const声明的常量为编译时常量,readonly声明的常量为运行时常量;
 
16. 协变/逆变
  
  在使用泛型的场合,把子类的引用赋给父类,参见该文章

  关于C#还有更多内容需要研究,希望自己能再接再厉,继续总结!

C# 基础知识总结的更多相关文章

  1. .NET面试题系列[1] - .NET框架基础知识(1)

    很明显,CLS是CTS的一个子集,而且是最小的子集. - 张子阳 .NET框架基础知识(1) 参考资料: http://www.tracefact.net/CLR-and-Framework/DotN ...

  2. RabbitMQ基础知识

    RabbitMQ基础知识 一.背景 RabbitMQ是一个由erlang开发的AMQP(Advanced Message Queue )的开源实现.AMQP 的出现其实也是应了广大人民群众的需求,虽然 ...

  3. Java基础知识(壹)

    写在前面的话 这篇博客,是很早之前自己的学习Java基础知识的,所记录的内容,仅仅是当时学习的一个总结随笔.现在分享出来,希望能帮助大家,如有不足的,希望大家支出. 后续会继续分享基础知识手记.希望能 ...

  4. selenium自动化基础知识

    什么是自动化测试? 自动化测试分为:功能自动化和性能自动化 功能自动化即使用计算机通过编码的方式来替代手工测试,完成一些重复性比较高的测试,解放测试人员的测试压力.同时,如果系统有不份模块更改后,只要 ...

  5. [SQL] SQL 基础知识梳理(一)- 数据库与 SQL

    SQL 基础知识梳理(一)- 数据库与 SQL [博主]反骨仔 [原文地址]http://www.cnblogs.com/liqingwen/p/5902856.html 目录 What's 数据库 ...

  6. [SQL] SQL 基础知识梳理(二) - 查询基础

    SQL 基础知识梳理(二) - 查询基础 [博主]反骨仔 [原文]http://www.cnblogs.com/liqingwen/p/5904824.html 序 这是<SQL 基础知识梳理( ...

  7. [SQL] SQL 基础知识梳理(三) - 聚合和排序

    SQL 基础知识梳理(三) - 聚合和排序 [博主]反骨仔 [原文]http://www.cnblogs.com/liqingwen/p/5926689.html 序 这是<SQL 基础知识梳理 ...

  8. [SQL] SQL 基础知识梳理(四) - 数据更新

    SQL 基础知识梳理(四) - 数据更新 [博主]反骨仔 [原文]http://www.cnblogs.com/liqingwen/p/5929786.html 序 这是<SQL 基础知识梳理( ...

  9. [SQL] SQL 基础知识梳理(五) - 复杂查询

    SQL 基础知识梳理(五) - 复杂查询 [博主]反骨仔 [原文]http://www.cnblogs.com/liqingwen/p/5939796.html 序 这是<SQL 基础知识梳理( ...

  10. APP测试入门篇之APP基础知识(001)

    前言        最近两月比较多的事情混杂在一起,静不下心来写点东西,月初想发表一遍接口测试的总结,或者APP测试相关的内容,一晃就月底了,总结提炼一时半会也整不完.放几个早年总结内部培训PPT出来 ...

随机推荐

  1. jQuery插件的开发之$.extend(),与$.fn.extend()

        jQuery插件的开发包括两种: 一种是类级别的插件开发,即给jQuery添加新的全局函数,相当于给jQuery类本身添加方法.jQuery的全局函数就是属于jQuery命名空间的函数,另一种 ...

  2. final发布视频

    final版本发布的视频已上传至秒拍,视频地址: http://m.miaopai.com/show/channel/AHqv~VA7lrk144ONLWrveQ__ 以下是功能截图:         ...

  3. [nodejs] Error: unable to verify the first certificate

    Error: unable to verify the first certificate Solution npm config set registry http://registry.npmjs ...

  4. timus 1175. Strange Sequence 解题报告

    1.题目描述: 1175. Strange Sequence Time limit: 1.0 secondMemory limit: 2 MB You have been asked to disco ...

  5. 给linode 替换操作系统核心

    1.Make sure your package repositories and installed packages are up to date by issuing the following ...

  6. 161227、js显示对象所有属性和方法的函数

    要想看到实际效果,可以先声明一些属性跟方法,否则是看不到,仔细往下看有例子的. function ShowObjProperty(Obj) { var PropertyList=''; var Pro ...

  7. openlayer 3 在layer上添加feature

    首先获取layer的source,所以你的source里就先要有内容. vectorLayer.getSource().addFeature(iconFeature); 如果source里面没有定义过 ...

  8. eclipse构建maven+scala+spark工程 转载

    转载地址:http://jingpin.jikexueyuan.com/article/47043.html 本文先叙述如何配置eclipse中maven+scala的开发环境,之后,叙述如何实现sp ...

  9. js 自带的 map() 方法

    1. 方法概述 map() 方法返回一个由原数组中的每个元素调用一个指定方法后的返回值组成的新数组. 2. 例子 2.1 在字符串中使用map 在一个 String  上使用 map 方法获取字符串中 ...

  10. JS_MediaPlayer类

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/T ...