c# basic introduction

c# basics



1. What is an abstract class and what is the difference between it and an interface

In C#, an abstract class refers to a class that cannot be instantiated, and its purpose is to be inherited and implemented by other derived types. An abstract class can contain abstract methods, virtual methods, and non-virtual methods, and the subclass must implement all the abstract methods in the parent class, otherwise the subclass must also be declared as an abstract class.

In contrast, an interface is a special abstract class that only contains the signatures of members such as methods, properties, events, and indexers without any implementation code. A class that implements an interface needs to provide the concrete method code that implements these members. A class can implement more than one interface, so it can get the implementation code for all the members defined in the interface.

In terms of usage, abstract classes are designed with commonality in mind, and cannot be instantiated because they do not own actual objects. The interface pays more attention to behavior and defines a set of rules so that different objects can have common behaviors, which is convenient for developers to program.

2. What is polymorphism

Polymorphism is an important concept in object-oriented programming, which means that the same method or operation can produce different behaviors on different objects. In simple terms, when the same method is called on different objects, it will have different performance or results.

Polymorphism needs to be based on inheritance and interface implementation. In C#, run-time polymorphism can be realized through the inheritance relationship, that is, a derived class object is referenced through the base class, and a function call is made, and the corresponding function implementation can be called according to the specific object type.

For example, suppose there is a base class Animal, which has a virtual member function Speak(), and two derived classes Dog and Cat, which override the Speak() function. In Main(), we can define an Animal type variable animal, and then point it to a concrete Dog or Cat object. In this way, when calling the Speak() function of animal, what is actually called is the Speak() function of the specific object, resulting in different behaviors.

In addition, C# also implements compile-time polymorphism through interfaces, that is, different classes can implement the same interface, thereby using the methods defined by the same interface, so that polymorphic behavior can be achieved through related operations of the interface.

3. Do you know the database index, briefly

A database index is a data structure used to speed up the retrieval and operation of data in a database table. It can be compared to an index in a dictionary, allowing us to find the information we need more easily.

The index in the database usually builds a data structure such as a binary tree or a B+ tree according to a certain column or a combination of columns, and these data structures allow fast positioning of data. When we query the data in this table, if an index is used, the database will first use the index to find qualified records instead of scanning the entire table row by row, thus greatly improving query efficiency.

In addition to queries, database indexes can also speed up INSERT, UPDATE, and DELETE operations. Because these operations will affect the index in the table, the database also needs to maintain the corresponding index structure when performing related operations.

Of course, the more indexes, the better, because each index needs to occupy storage space, and when modifying the table, all associated indexes need to be updated, so too many indexes will affect the performance of INSERT, UPDATE and DELETE Operational performance, and waste computer resources, increase the complexity of the database. Therefore, careful consideration is required when designing and using indexes to avoid abuse.

4. The advantages and disadvantages of array and arraylist and the difference between them

Array (Array):

Advantages: Because all elements are of the same type, the access speed is fast and the memory utilization is high.
Disadvantages: The size cannot be modified, the length needs to be determined in advance, and it is directly stored in continuous memory bits, and the insertion and deletion operations are inefficient.

Array List (ArrayList):

Advantages: The length can be increased or decreased at any time, and many convenient methods are provided, such as Add, Remove, etc., with good flexibility.
Disadvantages: memory consumption is relatively large, because its element types can be different, so each element needs an additional type pointer and garbage collection marker.
The main difference between the two is that Array has a fixed size and cannot be changed once declared, while ArrayList can be dynamically resized. At the same time, Array can only store data of the same type, while ArrayList can store data of different types. In addition, limited by the different storage methods, the efficiency of the two operations in various operations is also different.

For the case where only the same type of data needs to be stored, the length is fixed and the best performance is required, it is more appropriate to use Array; for the case where the length is uncertain or different types of data need to be stored, it is more flexible and convenient to use ArrayList.

It should be noted that due to the problem of boxing and unboxing, ArrayList is less efficient. Therefore, after .NET Framework 2.0, Microsoft released generic collections, such as List, Dictionary<TKey, TValue>, etc., to replace the original non-generic collection classes. These generic collections provide better performance and type safety sex.

back to the top


Guess you like

Origin blog.csdn.net/weixin_47410172/article/details/130579278