Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

[C #] CSharp basic syntax

2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/03 Report--

I. Foundation

1. Specification:

Except for constants, all variables are named by hump and others by Pascal.

two。 Compile:

First, csc.exe compiles the cs file into MSIL. When you double-click exe, clr's jit (just in time) compiler compiles the cpu instruction again.

Csc location (e.g.): C:\ Windows\ Microsoft.NET\ Framework\ v4.0.30319\ csc.exe

Csc commands (such as): csc / t:library acme.cs (compiled into class library) csc / r:acme.dll test.cs (compiled after referencing acme.dll)

Ngen location (e.g.): C:\ Windows\ Microsoft.NET\ Framework\ v4.0.30319\ ngen.exe (can achieve jit effect)

Ngen command (e.g.): ngen install D:\ SystemTool\ HelloWorld.exe (Note: exe mode)

3. Object oriented (Object-Oriented object oriented programming: Object-Oriented-Programming):

The class is abstract and the object is concrete.

Destructor: ~ ClassName () {}. Actually overrides the Finalize () method of the base class

4. Variables:

Member variable: there is an initial value .string-> null,int- > 0Bol-> false by default.

Local variables: must be assigned before use.

5. Encapsulation:

a. The field is encapsulated as a property b. The method parameters are encapsulated into class c. Method reuse d. Encapsulated into a class library

6. Inheritance:

Single root property

The constructor does not inherit.

7. Polymorphism:

The a.vitual and override methods can be overridden

B.abstract method

c. Interface

8. Access modifier:

Member access modifier:

Private: within the current class

Protected: within the current class and subclass

Internal: within the current assembly

Protected internal: within the current assembly or within the current class and subclass

Public: all will do

Access modifiers under the namespace:

Only public and internal are allowed, but Microsoft uses private.

9. Static:

Static state acts as a .stati modifier that distinguishes it from polymorphism.

Static classes: cannot be instantiated, members are static members, and can have const constants

Static members: when static members are assigned initial values, they are assigned in the static constructor.

Static constructor: called only once before the class is first used. No modifiers, no overloading.

10. Value type and reference type value passing and reference passing:

Value type: int double struct implicitly inherits ValueType.

Reference type: string class array, etc. Implicitly inherit Object.

Value passing: the default is value passing.

Reference passing: adding ref to the method parameters is reference passing.

11.SOLID:

Single: single responsibility

Open: development closure

Lis: Richter scale replacement

Interface: interface separation

Depend: dependency inversion

SOLID principle of object-oriented Design

twelve。 Interface:

a. Only methods can be defined in the interface.

b. Members of an interface cannot have modifiers. Default is public

13. Implementation interface and display implementation interface:

A class implements both the interface and the display implementation interface.

Display implementation interface: when the interface is called, the method of the display interface is called

Implementation interface: the default implementation of the interface method.

14. Type conversion:

Implicit conversion: when a small range of types changes to a large range of types.

Cast: use (type name) or as when a specified wide range of type objects can be converted to a smaller range of types

Convert: semantic transformation

Parse: convert other types to numeric types

15. Unit:

1byte = 8bit

1kb = 1024byte

Int32 = int: 4byte

16. Exception handling:

Error type: syntax error / logic error / run error

Throw;: to execute in catch is to continue throwing the exception up

In try-catch-finally, pay attention to the problem of return. The essence is to compile the return to the bottom. Through the decompilation tool, you can see that the value of return will be accepted by a single variable.

17. Parameter modifier:

Params: variable parameter

Out: outgoing parameter

Ref: reference passing. Essentially, when passing the value of a parameter, it passes the memory address of the variable.

18. String:

1. String immutability: strings in actual double quotes are constant. A commonly declared constant is a variable that declares a constant to accept a constant.

two。 String detention pool: it is precisely because the string is immutable that the technology of this pool is proposed, which actually takes the content of the string as a key and the address of the string as a value.

3.StringBuilder: be sure to use this object when concatenating strings. Otherwise, string objects will be created continuously. And it is highly recommended that the initial value be given.

C # string Detention Pool Mechanism

19. Garbage collection GC:

Recycled object: a heap object in managed resource memory.

Recovery time: uncertain

Garbage collection is divided into three generations. When the first generation is full, recycle the first generation of resources and move the unrecycled objects to the second generation. And so on. Manual garbage collection is highly discouraged.

20. Collection:

The surface of the set and array is an indefinite length and a fixed length. An array is maintained inside the actual essential set. When you add an element, you re-new an array

Common collections:

ArrayList List

Hashtable Dictionary

Stack StackLIFO

Queue Queue

Microsoft recommends using collections of generic classes. The essential reason is also to reduce the number of times of packing and unpacking.

21. Packing and unpacking

Packing and unpacking occurs between parent and child classes.

Boxing: a value type is converted to a reference type. Often converted to Object, the interface implemented by the value type.

Unboxing: a reference type is converted to a value type. Often occurs in casting to a value type

22.Path File Directory FileInfo DirectoryInfo Stream StreamWriter StreamReader

Path file directory information directory information stream write stream read into stream

The pressure on the equipment will be reduced in the form of flow. It does not occur to read all at once before performing follow-up operations.

23. Coding

ASCII GB2312 GBK UNICODE UTF-8

Encoding.GetEncodings () fetches all the computer codes

The actual strings are all digits stored on the computer and are stored in byte [].

The reason for the garbled code is the inconsistency between the stored code and the read code.

24. Serialization

Serialization steps: create a serializer, serialize or deserialize.

Xml serialization: XmlSerializer class (System.Xml.Serialization)

Js serialization: JavaScriptSerializer class (System.Web.Script.Serialization)

Binary serialization: BinaryFormatter class (System.Runtime.Serialization.Formatters.Binary)

Serialization: saves the state of the object to the storage device.

25. Delegate / event

The essence of delegation is a type.

An event is a delegate object, essentially a private delegate object and two public methods. So events can only be called inside the class!

twenty-six。 Anonymous

Anonymous method: delegate (parameter) {method body} is mostly used for delegating object assignment, essentially a name given by the compiler.

Lambda: statement: (parameter) = > {method body} expression (when there is only one parameter and the method body has only one sentence) for example: X = > xroom2

Anonymous type example: new {Code = ViewBag.Code}; the properties of anonymous types are read-only! Essentially anonymous fields are private readonly, encapsulated as read-only properties.

twenty-seven。 Generics

Generic support: class method delegate interface

Generic constraint: where T: class struct new () class name interface name

twenty-eight。 Expansion method

Extension method: static class static method the first parameter type is the type to be extended

The essence is that the compiler passes the extended type as a parameter to the static method.

twenty-nine。 Assembly

The exe and dll generated by. Net are assemblies.

Including type metadata, assembly metadata, resource files, MSIL intermediate language.

The common Assembly is in GAC, so when compiling, the imported assembly is not output by default.

30.Type Assembly

Describes the type of the class. Through the Type object, you can instantiate an object of the type referred to by this Type.

Type: through Typeof (class name), GetType (object name)

Assembly:Assembly.LoadFrom (assembly name) returns an Assembly object. GetType (the qualified name of the class) gets the Type object of the specified class.

Create an object from Type: Activator.CreateInstance (Type object name)

31.XML

Extensible markup language: case-sensitive, a root node, attribute values in double quotes, CDATA areas, comments and html, the document states that the encoding should be consistent with the actual encoding.

thirty-two。 Deep copy shallow copy

A deep copy is a copy made by all members in memory. A deep copy can be done through serialization.

Shallow copy: except for deep copy, all are shallow copy, MemberwiseClone () can copy directly.

thirty-three。 Regular expression

A regular expression is an expression that describes the characteristics of a string

Metacharacter (backslash):

. [] | () {nheroin m} *? +\ d\ s\ w\ b ^ $

.: a single arbitrary character except the newline character

[]: take any character in parentheses

| |: or has a very low priority |

{nmagnetic m}: the preceding characters appear n to m times

*: 0 or more times

0 or 1 time

+: 1 or more times

\ d: 0-9

\ s: blank

\ w: 0-9aMuzAMuZZ _ single character

\ b: word boundary

^: head

$: end

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report