C # basic knowledge

Source: Internet
Author: User

C # basic knowledge

The keyword used by the interface defined in 1. C # Is: interface
2. C # is a pure object-oriented language. Object-oriented languages have the following features: inheritance, encapsulation, and polymorphism.
3. Differences between the value type and the reference type:

CLR supports two types: reference type and value type. The difference between the two types is that they have different positions in the. NET class hierarchy, and the memory allocated by. NET is also different. Both the reference type and value type inherit from the Systerm. Object Class. In contrast, almost all reference types are inherited directly from Systerm. Object, while the value type is the Systerm. ValueType class that inherits the subclass of Systerm. Object. The difference between memory allocation for reference and value types is how CLR handles their memory requirements. The value type is allocated in the running (managed stack), and the reference type is placed in the (managed stack), the managed stack will reference the managed stack.

4. Differences between classes, objects, and struct:

(1) struct is a value type, class is an object type (2) struct cannot be inherited, class can be inherited (3) struct default access permission is public, the default access permission of class is private. (4) struct always has a default constructor, Which is retained even if the default constructor is reloaded. This is because the Struct constructor is automatically generated by the compiler. However, if you reload the constructor, you must Initialize all the variables in struct. The purpose of Struct is to describe lightweight objects, such as Line and Point, with high efficiency. Class has no default parameter constructor when there is no overload constructor, but some default constructor will be overwritten once it is overloaded. (5) struct's new and class's new are different. Struct's new is to execute the constructor to create a new instance and then Copy all fields. While the class allocates a block of memory on the heap and then executes the constructor. The struct memory is not allocated at the time of new, but allocated at the time of definition. Refer to the blog: (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 classes, closed classes, and static classes

(1) abstract classes are closely related to interfaces. They cannot be instantiated and are often partially or simply not implemented. A major difference between an abstract class and an interface is that a class can implement an unlimited number of interfaces, but can only inherit from an abstract (or any other type) class. Classes derived from abstract classes can still implement interfaces. You can use abstract classes when creating components, because they allow you to specify unchanged-level functions in some methods, but do not implement other methods until a specific implementation of the class is required. The abstract class also has a version, because if you need to add a function to the derived class, you can add it to the base class without interrupting the code. In C #, use the abstract modifier. (2) closed classes are mainly designed to avoid inheritance. Use sealed in front of the class to indicate private. The sealing class cannot be used as the base class. Therefore, it cannot be an abstract class. The seal class is mainly used to prevent derivation. Since the seal class is never used as the base class, some runtime optimization can make the call to the seal class members slightly faster. The class member, method, field, attribute, or event on the derived class that overwrites the Virtual Member of the base class can be declared as a sealed member. When used in a later derived class, this will cancel the virtual effect of the member. The method is to place the sealed keyword before 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 instances of classes. Static class members can be used to separate data and behavior independent of any object identifier: No matter what changes occur to the object, the data and functions will not change. When the class does not depend on the data or behavior of object identifiers, you can use static classes. Class can be declared as static to indicate that it only contains static members. You cannot use the new keyword to create a static class instance. When a static class loads a program or namespace containing the class, it is automatically loaded by the. NET Framework Common Language Runtime Library (CLR. Static classes are used to include methods that are not associated with specific objects. For example, it is common to create a group of methods that do not operate on instance data and are not associated with a specific object in the code. You should use static classes to include those methods. The main functions of static classes are as follows: they only contain 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 only contains static members and private constructor. Private constructors prevent classes from being instantiated. The advantage of using static classes is that the compiler can perform checks to prevent accidental addition of instance members. The compiler will ensure that such benefits will not be created. Static classes are sealed and therefore cannot be inherited. A static class cannot contain constructor, but 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 role of a static constructor?
Reference official statement: http://msdn.microsoft.com/zh-cn/library/79b3xss3.aspx

Non-static classes can contain static methods, fields, attributes, or events. You can call static members in the class even if no instance of the class is created. You can always access static members by class name instead of Instance name. No matter how many instances are created for a class, its static members have only one copy. Static methods and attributes cannot access non-static fields and events in their contained types, and cannot access instance variables of any objects (unless explicitly transmitted in method parameters ). Static methods can be overloaded but cannot be overwritten because they belong to a class and do not belong to any instance of the class. Although the field cannot be declared as static const, the behavior of the const field is static in nature. Such fields belong to the type and do not belong to the type instance. Therefore, you can use ClassName. MemberName notation to access the const field like static fields. No object instance is required.

 

Related Article

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.