DotNet knowledge-thirty

  1. Note the use of the interface:

The interface can not add members access modifier

Interface members can not have any implementation

Subclass that implements the interface must implement all the members of the interface

While a class can inherit a class and implement multiple interfaces, if a subclass inherits the parent class A , and implements the interface IA , then the grammar A must be written in IA front, because the class is single inheritance, and The interface can be achieved and more

Issues to consider when implementing an interface explicitly and implicitly implement interfaces (we usually implicit realization)

Class implements an interface, the interface can be implemented explicitly in the method, but note: once explicitly implement the interface in the method, the method can only be called interface variable

IFlyable fly = new Bird();

fly.Fly (); // correct

 

Bird fly = new Bird();

fly.Fly (); // error

 

 

Upcast

Single Responsibility Principle (defined avoid bulky interface, as this can cause "Interface pollution", only the definition of a group of members associated to a interface)

2 , the difference between abstract classes and interfaces:

Same point:

Abstract classes and interfaces can not instantiated

difference:

Abstract base class defines fields, properties, methods. Interface can only define the properties, indexers, events, and methods

An abstract class is a class that is not complete, need to be further refined, and the interface is a specification behavior. MicroSoft custom interfaces always leash able field, prove it is a kind of expression, "I can do ......"

Multiple interfaces can be achieved, it can only be a single abstract class inheritance

More abstract class is defined between a series of closely related classes, class relationships and the interface is loose but most of all to achieve a certain function in

An abstract class is abstracted from the concept of a series of related objects, and therefore reflects the internal common things; a function in order to meet the agreed upon interfaces defined external calls, thus reflecting the external characteristics of things

Any specific characteristics of the interface basically do not have inherited, it can only promise a method call

Interface can be used to support the callback, but does not have to inherit this feature

Specific methods abstract class implements the default is virtual, but the implementation of the interface class interface methods, but the default is non-empty, of course, you can also be declared as virtual

If the abstract class implements an interface, the interface can be mapped to the abstract class methods as abstract methods without having to implement, while the method of subclasses of the abstract class interface to achieve

      3.  Type Conversion

 

Conversion Category

Copy the code

// implicit conversion

int num = 1;

string str = num.toString();

 

 

 

// cast

int a = 5;

(double) b = a;

Copy the code

Conversion Conditions

ToString () method

It is Object a virtual method object, you can override this method themselves. If you do not override the return type name

The Parse () and the TryParse () method

The same point: for the type of value is converted to a string

difference:

The return value is not the same: the Parse () is the return type to be converted, and TryParse () Returns whether a successful conversion bool value

Parse () conversion failed will throw an exception, but TryParse () conversion failed to return false , will not throw an exception

Parse()直接传入要转换的类型的值,而TryParse()需要一个out输出参数输出转换的结果

       4.静态成员

 

加载时机(什么时候加载静态成员?)

在该静态成员第一次被调用的时候加载

适用情形(什么时候用静态成员)

在整个程序内部共享的数据才定义为静态的。通常被作为工具类使用。比如DBSqlHelper

在普通类和静态类中的区别

静态类需要用static,静态类不能被实例化

静态类中只能包含静态成员

在实例方法中可以直接调用静态成员,但在静态方法中不能直接调用实例方法

静态类和静态变量创建后始终使用同一块内存,而使用实例的方法会创建多个内存空间

静态构造函数不能有参数,也不能与访问修饰符(默认是private

      5.静态类的继承

 

从子类的角度:静态类不能被任何类继承

从父类的角度:静态类只能继承Object类,不能继承其他类

      6.类和成员的访问修饰符

 

类的默认访问修饰符是internal

类的成员的默认访问修饰符是private

类的访问修饰符只有两种:publicinternal(默认)

类的成员的访问修饰符有:publicprotectedprivate(默认)

      7.结构

 

本质是值类型

值类型和引用类型的选择

值类型:主要是用来封装一组数据,并为数据提供一种简单的处理方式

引用类型:

主要用来封装数据和行为

使用面向对象的特征

当类型中的成员比较多的时候用结构(存在堆里)

new关键字的作用

在使用new关键字创建对象后,所有的成员变量都已经存在,并有默认值(值类型)

如果没有用new关键字,则需要程序员手动为所有的用到了的成员变量赋值,之后才能调用结构对象里的方法属性

结构不new也可以使用,但是必须给使用的结构成员赋值才能使用

      8.类和结构的区别

 

结构是值类型,是分配在内存的栈上的,而类是引用类型,是分配在内存的堆上的

结构不能被继承,因为结构是值类型,隐式继承自System.ValueType

结构是是值传递的(赋值传递),而类是引用传递的

      9.访问级别的约束

 

子类的访问级别不能比父类高

方法参数的访问级别   >=   方法的访问级别(比如当方法的参数传递的是一个类的对象时,那么此时这个类对象的访问级别要高于当前方法的访问级别)

      10.析构函数

 

一个类只能有一个析构函数

无法继承或重载析构函数

无法手动去调用析构函数,因为它是被GC(垃圾回收机制)自动调用的

析构函数不能有访问修饰符,也不能有参数

不能在结构体中定义析构函数,因为结构是值类型,而值类型是存储在栈中,栈中的数据用完之后就立即销毁了,而析构函数的目的就是用来释放资源的,一般存储在堆中的引用类型才需要GC去释放,而因结构体中不能定义析构函数,所以只能对类使用析构函数

复制代码

//析构函数语法

 

class MyDispose

{

      ~MyDisPose()

    {

          ……//释放资源的代码

    }

}

复制代码

 

 

 

 

      11.字符串

 

属性:length

静态方法

常用:

LastIndexOf:用来查找某个字符或字符串,在一个特定字符串对象里的下标

SubString 截取

Split() 根据特定字符来分割字符串,并返回分割后的字符串的数组,可以用foreach读取

Join静态方法

Format() 静态方法

Replace(),替换完要接收,产生一个新的字符串

Replace().Replace()链式编程

Trim()去首尾空格

      12.“==”运算符好 “Equals()”方法的区别

 

“==”比较时:

如果比较是值类型,则比较两个对象的值

如果比较的是引用类型,则比较两个对象的引用地址是否相同(比较堆地址)

“Equals()”比较时:

此方法是Object类里的一个虚方法,默认就是用的“==”及进行比较,是对“==”进行的一个封装,但是,大部分MicroSoft的类及用户自定义的类,都重写了该虚方法,也就是MicroSoft和用户各自为自己编写的Object的子类定义了相等比较规则

     注意:这里有一个特例,因为string是一个引用类型,所以按理说string.Equals("……")方法比较的是地址,而这里比较的是字符串的值

 

      13.字符串的恒定性:

 

    当字符串在内存中已经被创建后,程序员再次创建相同值的字符串对象时,CLR做了优化,直接把第一个字符串的引用赋给了第二个变量,也就是说,前后两个字符串变脸保存了相同的字符串对象应用

 

      14.StringBuilder对象

 

    String 在进行运算时(如赋值、拼接等)会产生一个新的实例,而 StringBuilder 则不 会 。 所以在大量字符串拼接或频繁对某一字符串进行操作时最好使用 StringBuilder , 不要使用 String。如果要操作一个不断增长的字符串,尽量不用 String , 改用 StringBuilder 类。

 

    两个类的工作原理不同 :String 类是一种传统的修改字符串的方式 , 它确实可以完成把一个字符串添加到另一个字符串上的工作没错 , 但是在 .NET 框架下 , 这个操作实在是划不来 。 因为系统先是把两个字符串写入内存 , 接着删除原来的 String 对象 , 然后创建一个 String 对象 , 并读取内存中的数据赋给该对象。这一来二去的,耗了不少时间。而使用 System.Text 命名空间下面的StringBuilder 类就不是这样了,它提供的 Append 方法,能够在已有对象的原地进行字符串的修改,简单而且直接。当然 , 一般情况下觉察不到这二者效率的差异 , 但如果你要对某个字符串进行大量的添加操作,那么 StringBuilder 类所耗费的时间和 String 类简直不是一个数量级的。

Guess you like

Origin www.cnblogs.com/Mr-Prince/p/12104465.html