Basic knowledge of C #

Source: Internet
Author: User

The keywords used to define the interface in 1.c# are: interface
2.c# is a purely object-oriented language. The object-oriented language has the following characteristics: inheritance, encapsulation, polymorphism.
3. Difference between a value type and a reference type:

    CLR支持两种类型,引用类型和值类型。这两种类型的不同之处是,他们在.NET类层次结构中的位置不同,那么.NET为其分配的内存的方式也是不同的。    引用类型和值类型都继承自Systerm.Object类。不同之处,几乎所有的引用类型都是直接从Systerm.Object继承,而值类型则是继承Systerm.Object的子类Systerm.ValueType类。    引用类型和值类型的内存分配,差别在于CLR是如何处理它们的内存需求。值类型在运行是(托管栈)中分配,而引用类型放在(托管堆)中,托管栈将引用托管堆。

4. The differences between classes, objects, and structs:

(1)struct 是值类型,class 是对象类型(2)struct 不能被继承,class 可以被继承(3)struct 默认的访问权限是public,而class 默认的访问权限是private.(4)struct总是有默认的构造函数,即使是重载默认构造函数仍然会保留。这是因为Struct的构造函数是由编译器自动生成的,但是如果重载构造函数,必需对struct中的变量全部初始化。并且Struct的用途是那些描述轻量级的对象,例如Line,Point等,并且效率比较高。class在没有重载构造函数时有默认的无参数构造函数,但是一被重载些默认构造函数将被覆盖。(5)struct的new和class的new是不同的。struct的new就是执行一下构造函数创建一个新实例再对所有的字段进行Copy。而class则是在堆上分配一块内存然后再执行构造函数,struct的内存并不是在new的时候分配的,而是在定义的时候分配参考博客:(1)http://blog.csdn.net/dyllove98/article/details/9336367(2)http://www.cnblogs.com/liuzijing688/archive/2008/01/11/1034893.html

5. Characteristics of abstract class, enclosing class, Static class

(1) Abstract classes are closely related to interfaces, they cannot be instantiated, and are often partially or simply not implemented. One of the main differences between an abstract class and an interface is that a class can implement an infinite number of interfaces, but can inherit from only one abstract (or any other type) class. Classes derived from abstract classes can still implement interfaces. You can use abstract classes when you create components, because they enable you to specify invariant levels in some methods, but you do not implement other methods until you need a specific implementation of the class. Abstract classes also develop versions, because if additional functionality is required in a derived class, it can be added to the base class without interrupting the code. In C #, use the abstract modifier. (2) Closed class is sealed class, mainly in order not to be inherited and designed.    Decorated with sealed in front of class, representing private. Sealed classes cannot be used as base classes. Therefore, it cannot be an abstract class. Sealed classes are primarily used to prevent derivation.     Because the sealed class is never used as a base class, some run-time optimizations can make calls to sealed class members slightly faster. A class Member, method, field, property, or event on a derived class that overrides a virtual member of a base class can declare the member as a sealed member. When used in later derived classes, this cancels the virtual effect of the member. The sealed keyword is placed in front of the override keyword in the class member declaration. (3) Static classes and class members are used to create data and functions that can be accessed without creating an instance of the class. Static class members can be used to detach data and behaviors that are independent of any object identity: No matter what changes are made to the object, the data and functions do not change.    Static classes can be used when there is no data or behavior in the class that depends on the identity of the object. A class can be declared as static to indicate that it contains only static members. You cannot use the New keyword to create an instance of a static class.    Static classes are loaded automatically by the. NET Framework Common Language Runtime (CLR) when the program or namespace containing the class is loaded. Use static classes to include methods that are not associated with a particular object. For example, it is a common requirement to create a set of methods that do not manipulate instance data and are not associated with specific objects in your code.    You should use static classes to include those methods.        The main functions of static classes are as follows: They contain only static members.        They cannot be instantiated.        They are sealed.    They cannot contain instance constructors (C # Programming Guide). Therefore, creating a static class is roughly the same as creating a class that contains only static members and private constructors.    The private constructor prevents the class from being instantiated. The advantage of using static classes is that the compiler is able to perform checks to ensure that instance members are not accidentally added.    The compiler will guarantee that this class will not be created for real profit. Static classes are sealed and therefore cannot be inherited. Static classes do notCan contain constructors, but you can still declare a static constructor to assign an initial value or set a static state. 

6. How to understand static variables and static methods? What is the function of the static constructor?
Refer to the official statement: http://msdn.microsoft.com/zh-cn/library/79b3xss3.aspx

    非静态类可以包含静态的方法、字段、属性或事件。 即使没有创建类的实例,也可以调用该类中的静态成员。 始终通过类名而不是实例名称访问静态成员。 无论对一个类创建多少个实例,它的静态成员都只有一个副本。 静态方法和属性不能访问其包含类型中的非静态字段和事件,并且不能访问任何对象的实例变量(除非在方法参数中显式传递)。    静态方法可以被重载但不能被重写,因为它们属于类,不属于类的任何实例。    虽然字段不能声明为 static const,但 const 字段的行为在本质上是静态的。 这样的字段属于类型,不属于类型的实例。 因此,可以同对待静态字段一样使用 ClassName.MemberName 表示法来访问 const 字段。 不需要对象实例。

(Not finished ... )

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Basic knowledge of C #

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.