Bright Sword. NET Chapter II

Source: Internet
Author: User
Tags character classes

The second chapter mainly explains the various obscure concepts, usages, types and so on in C #.

1.Equals () and = = Difference

When comparing two value types, the two are basically the same, when comparing reference types, = = compares the addresses of reference types, that is, whether two references point to the same heap, and Equals () compares the contents of the heap space to which two references refer to the consistency. Note: When using a reference type object A to assign a value to another reference type Object B, instead of copying a copy of the heap, and then pointing the B to the heap, the value of a is directed to the same heap as the address of a to b,ab.

The difference between 2.const and readonly

A const is a compile-time deterministic value, so it can only be assigned a constant value. Object,array,struct cannot be declared as Const.

ReadOnly is a run-time deterministic value that can be assigned with a variable, which can be assigned to a constructor, enabling dynamic constant representation and greater flexibility.

3.private,protected,public,internal

(1) Private can only be called within the class itself

(2) Protected can only class itself and derive from its own subclass call

(3) public is fully open

(4) Internal can only be accessed within the same assembly, which is not the same solution within the same project.

(5) Protected internal can only be called on the same assembly's class itself and derived from its own subclass

4.

Sealed the class into a sealed class and cannot be inherited. Turn the method into a sealed method and cannot be overloaded

New shows hidden members that inherit from the base class and can be hidden without using new, but warns.

Virtual declaration virtual method, not specifically implemented, implemented in subclasses.

Abstract declares abstractions, abstract methods, and derived subclasses must implement methods that are not implemented in all abstract classes.

Override a method that overrides virtual or abstract modification

New and override the same point: all methods of the base class can be hidden, and the members of the base class may be called with the base keyword

New and Override differences: (1) Override hides only virtual or abstract modified methods, new unnecessary (2) when using a base class to invoke the virtual method, if you override the method with override, The overriding method is called, but the virtual method of the base class is called with new override

5.abstract Class and interface

6. Differences between public variables and attributes

If you use a public variable, you can control the member variable outside of the class with discretionary access, violating the encapsulation idea in object-oriented.

With attributes, access to properties is limited to the class's own set of properties, not directly manipulating the data content of the class, but rather through accessors, such as the use of get{}set{} to read and write data, as well as a number of other uses, such as validating data before changing it, and so on.

A property is an encapsulation of a field (data).

Properties can control read and write, variables are not available.

Therefore: when we want to set a value in a class to public, use the property instead of the variable whenever possible.

7.params,out,ref

Params: The number of arguments to the function is variable.

void f (params int[] list) {..} The following uses are possible. f (+); F (int[); ints={1,2,3};f (ints);

void f (params object[] list) {..} F (1, 2.23, "123");

Out: a reference pass

When you use the out modifier parameter, the definition and invocation of the function are explicitly added to the Out keyword, void f (out int i) {...} int i;f (out i); it doesn't matter if I assign a value, because I will be emptied when I enter the function. Therefore I cannot pass in the value all inside the function, can only take the value out from within the function. It must be assigned inside the function, because it does not have a value, C # does not accept an address is not worth the variable, will be an error.

Ref: Just an address

It is also necessary to explicitly add the REF keyword to the definition and invocation of the function, which must be initialized before the parameter is passed with a ref-modifier, because it does not empty after passing in the value, so it must be initialized because it already has a value, so it can not be changed inside the function that references it.

8. Difference between a value type and a reference type

The value type stores the data. Allocated in the stack, = operation is a copy of the ontology.

A reference type is a behavior. Allocated in the heap, = operation is a copy of the pointer to the heap.

9. The difference between a struct and a class

struct is a value type

Class is a reference type

The difference between 10.string and StringBuilder

The string object is immutable, string i= "a"; i+= "B"; At this point I is AB, it seems that string can be changed, in fact, when the + = operation is re-created a string object, assignment AB, the original string is discarded, String cannot use the new string () to construct a string object.

The StringBuilder is mutable and is specifically designed to handle dynamically changing character classes. StringBuilder str=new StringBuilder ("a"); Str. Append ("B");

Relatively speaking, if you want to make a lot of dynamic changes to the string, it is best to use StringBuilder, because the use of string will not stop the creation of the destruction of the string object, the system overhead is very high, the use of StringBuilder more efficient.

The difference between abstract class and interface is not quite understood.

Bright Sword. NET Chapter II

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.