C # foundation

The pictures below are from C# and .NET+4 Advanced Programming + Version 5 and Baidu

.NET is understood as a runtime environment and a comprehensive basic class library.

.NETThree key entities (building blocks):CLR, CTS, CLS

The common language runtime layer isCLR . Function: Positioning, loading and management.NETtype. It is also responsible for low-level work such as memory management, processing threads, and so on.

Common Type System:CTS . Describes all possible data types and programming structures supported by the runtime library.

Common Language Specification:CLS. Define all .NET a subset of common types and programming structures that are supported.

ReflectorCheck the assembly and decompile.

.Net

CallJSFunction<script language =”javascript” type=”text/javascript”>

There are two ways to transmit form data:POST < span style="font-family: 宋体;">andGET

POST method, form data is for the outside world It is not directly visible, and there is no character length limit.

GET method, the form data will be in & separated. Characters are restricted.

according to the originalHTTP request to create an assembly , It will be reused by all subsequent requests without compiling again. Subsequent requests for the same page will be much faster. < /span><%@page%>in the instructionTraceThe feature is set totrue span> to enable tracking support.

viaTrace.Write() record the log.

JavascriptOperation Mechanism

One of the major features of JavaScript is single-threaded, which can only do one thing at a time.

JavaScript divides tasks into two types, one is synchronous tasks and the other is asynchronous tasks.

Synchronous: Queue to execute tasks

Asynchronous: notify the main thread through the task queue, and only enter the main thread for execution when a certain asynchronous can be executed.

Asynchronous operation mechanism:All tasks are executed on the main thread , Forming an execution stack. There is a “task queue” outside the main thread (asynchronous task produces a result, and an event is placed in the task queue). After the task in the execution stack is executed, the task is called from the task queue to see what events are in it , Put the asynchronous task on the execution stack and start execution.

Timer: setTimeoutandsetInterval

setTimeout(function() {{console.log(i); }, 1000); The second parameter indicates how many milliseconds to execute.

Three-tier architecture

The three-tier architecture is divided into UI(Presentation layer),BLL(Business logic layer) DAL(data access layer)

< img alt="Share a picture" src="/wp-content/uploads/images/language/csharp/1626812562178.png" width="189" height="271" >

< p>

The purpose of using the three-tier architecture is to decouple . A change in any one layer will not affect the other layer. The structure is clear and the maintainability is high, which is conducive to synchronous development.

How to get web.configthethe connection string

Ifname=conString

then pass the codestring connectionString;
connectionString= ConfigurationSettings. AppSettings[“conString”];AvailableconString the string content inside.

I checked the stored procedure mentioned in today’s meeting

stored procedure (Stored Procedure) is in largedatabase system, a set of SQL statement set in order to complete a specific function, it is stored in the database, and it is permanently valid after a compilation, and the user can store it by designation The name of the procedure and given parameters (if the stored procedure has parameters) to execute it. The stored procedure is an important object in the database.

The stored procedure is similar to a method and may have parameters, which can be called.

C#+The symbol is processed by the compiler asString.Concat()< /span>Call.

Strings are immutable.

StringThe method of type actually returns a new string object in the modified format.

After calling the capitalization,Stringstring Still no change.

Static methods can be called directly without creating an instance of the class.

Member overloading: When defining a group of members with the same name (their number of parameters or types are different), so The members of is called overloaded.

enumeration

in When creating a system, it is convenient to create a set of symbolic names to correspond to known numeric values ​​(symbols instead of numbers). For example:

Enum EmpType

{

Manager,/ /The default is from0At the beginning, you can also assign values ​​by yourself, but they can’t be discontinuous

Grunt,//1

Contractor,//2

VicePresident ,//3

}

Use enumeration

In the above enumeration, the enumeration name (EmpType) to set the value.

SettingsGrunt

According to the enumeration name EmpType emp=EmpType.Grunt; emp The value isGrunt.

Five data types: class structure enumeration interface delegate

value type (placed on the stack, the base class is ValueType, assign a value ) and reference type (placed on the stack, except for the base class span>ValueTypeAnything. Point to a memory location)< /p>

Value types disappear when they go beyond the defined scope, and reference types when the heap is garbage collected.

When a value type is assigned to another, it is to copy the field members.

The reference type is to redirect the reference variable in memory.

The value type defined in C# includes the original type (Sbyte span>,Byte,Short,UshortInt,Uint, Long,Ulong ,Char,Float,Double,Bool,Decimal), enumeration(enum) , structure(struct)

Reference types include: Object,Classes, arrays, interfaces, delegates, strings(String) etc.

Refand< span style="font-family: Calibri;">outWhat’s the difference?

OutThe output parameters do not need to be initialized,RefThe reference parameter must be initialized

Overloading (the same method name, different return types and parameters) and overloading The difference between writing?

Ref returns multiple values.

P2pointingP1< span style="font-family: Song Ti;"> memory. WhenP1change P2The same change.

C#Nullable typenull is used to create a null object reference, the value type cannot be assigned null.

? ? Operator. The value obtained is actuallynull, can we use it? ? Assign a value to a nullable type.

The default constructor will not accept any parameters,e.g.

Public person(){

Weight= 10;

span>

Height=10;

}

As an initialization function.

The constructor can also be customized

Public person(int wei)

{

Weight=wei;

< p>}

No value assigned is the default value0;

Summary: The difference in the constructor is the number and type of parameters. The constructor has no return type Eachclassclass has a default constructor, But once a custom constructor is defined, the default one will be automatically removed.

ThisKeywords

Resolve scope ambiguity when the name of the incoming parameter is the same as the data type.

Share a picture

When the variable name is the same as the member variable type name, the scope will point to the variable instead of the field

share picture

JoinThis eliminates this ambiguity.

ThisThere is another usage of keywords. For example:

A certain parameter of the constructor needs to be checked and verified.

Class Motocycle{

Int goodspeed;

Int drivername;

//Called every time the constructor is called This method

Public Motocycle(int speed)

{

Setgoodspeed(speed);

}

Public void Setgoodspeed(int speed)

{

If(speed>10)

{

speed= 10;

}

Goodspeed=speed;}}

UseThisKeywords can chain constructors: the structure is as follows

 Share picture

Staticstatic

Static fields are shared by all objects. If you need to share data, you can use static members.

About the static constructor

It cannot be overloaded. The static constructor will only be executed once, and the execution priority is the highest.

Static class

Share a picture

UseprivateThe keyword defines the default constructor private to prevent the creation of an instance.

Static classes are not allowednewExamples, functions open .

OOPpillar< /span>

3 core: package Inheritance Polymorphism

Encapsulation is to hide the implementation details, It also plays a role in data protection.

Inheritance is to create a new class based on an existing class to inherit the core functions of the base class.

Another type of code reuse is: include/commissioned, this Reuse is not an inheritance relationship between parent and child classes, but one class is defined as a member variable of another class.

Share pictures

One A simple example is a car and a radio. There is a radio in a car. It cannot be said to be inherited. It is illogical. It can only be said that one object contains another object. The method in the radio object is called in the car object.

Polymorphism

The ability to handle related objects in the same way.

The same request operation is different.

Two principles: Richter’s substitution principle: Subclass objects can be used in place of their base class objects.

Principle of openness and closedness: open to the outside world, and closed to internal modification.

Define an abstract method, and the derived class overrides the abstract method for concrete realization.

Share a picture

Share a picture

Aniaml an=new Cat();

You can point to by creating an Multiple derived class objects.

an= new dog();

an=new chicken();

Create cat object, base class points to subclass

The realization of polymorphism:

Share pictures

Visit Write down three commonly used modifiers

share pictures

PublicPublic without restrictions

PrivatePrivate in this category is available< /span>

Protected Use of this class and subclasses

The default modifiers are all private.

Set getThe method is the most basic package.

.NETThe language advocates the use of attributes to force encapsulation of state data. Attributes will always be mapped to “actual” access methods and modification methods. After the attribute is created, the corresponding get andset to encapsulate.

I checked the meaning of the package

Share a picture

One more thing is

share picture

You can control its functions

< /p>

Use class attributes (not clear meaning)

Internal representation of attributes

If the package has been writtenset getthe method has been redefinedset get method then compile errors, the basic class library will use type attributes instead of traditional access and modification methods.

Class employee

{
Public
static companyName{get;set;}
Simply written
}
Class employee
{
Private
static string companyName;//Encapsulate fields
Public staric
string company//The package has no brackets ()
{
Get{
return companyName;}
Set(companyName
=value;)
}
}
Main(){
Employee.company
="My company";
}

Automatic attributes

Share picture

More convenient. However, the automatic attribute compulsory also supports reading and writing functions.

ConstUsed to define Chang Liang

For example, I define a π value

Public const double pai=3.14;

Ps:The keywords are all Add word data type before

ReadonlyRead-only fields and constants cannot be similar Change after initial value is assigned.

But there are also differences from constants.

The value assigned to the read-only field can be determined at runtime,So it is legal to assign a value in the constructor. For example:

Class{

Public readonly double pi;

Public class(){

Pi=3.14;

}

}

Inheritance and polymorphism< /span>

In C# Define the colon: between operation classes Relationship.

e.g.:MiVanInheritance< span style="font-family: Calibri;">CarClass

Class MiVan:Car

{

}

MiVanCan call the public attributes of the parent class, inheritance protects the package, and cannot access private members.

.NETMultiple inheritance is not allowed

You can split the definition of a class, structure or interface into two or more source files, and add partialKeywords are sufficient.

SealedKeywords

Prevent inheritance

Create subclass objects to access base class functions and extend them.

BaseKeywords

Static members can only be accessed by the class, not by the object.

BaseandthisAll are instances of access classes

General base The default constructor of the class will be called automatically

share picture< /p>

The above example shows that the appropriate custom base class constructor is called. Solve the read-only problem and inefficiency. If a custom constructor is added, the default constructor is automatically removed, so you have to redefine the default constructor.

share picture

base class Adding the Protected keyword

Benefits: The derived class can directly access the information of the base class.

Disadvantage: It is possible to bypass the business rules set by public attributes.

Protected data is considered private.

Include/delegate programming

Has-a relationship

Simply put, delegation is to add public members to the containing class in order to use the functions of the contained object.

I don’t understand the commission

Non-nested classes cannot be declared with the private keyword.

Public class OuterClass

{

//Public nested types can be used by anyone Use

Public class PublicInnerClass()

//Private nested types can only be used by members of the containing class

Private class PrivateInnerClass()

}

p>

C# polymorphism

The same request makes different responses.

If the base class wants to define a method overridden by a subclass, the virtual keyword must be added.

If the subclass wants to change the implementation details of the virtual method, it must use the override keyword.

You can use the base key to use the default behavior

For example:

Base.GiveBonus( );

The default behavior of the base class is used.

Abstract class

To prevent the creation of ambiguous base classes, use abstract keywords.

Abstract class Employee

{

}

Building a polymorphic interface

Abstract methods can only be in abstract classes

dynamic is similar to the Var weak type in javascript (used when dealing with the underlying layer)

Interface

Interface is a group Collection of abstract members.

C# Use interface to define an interface .

Not in the interface There are fields, no constructors, and interfaces cannot provide implementations.

Interface can achieve multiple inheritance
as keyword to determine whether to support an interface null
is keyword Determine whether to implement an interface false

Interface is particularly useful in the following situations:
There is only one hierarchical structure, but only some of the derived classes support a certain public behavior
Need to build Public behavior spans multiple levels. No understanding yet

The interface has not been specifically written yet (the understanding is not deep yet)

For example: interface class ICamera

VirtualCamera inherits ICamera( );

ICamera camera=new VirtualCamera();

Call the camera method.

Generic

Class employee

{
Public
static companyName{get;set;}
Simply written
}
Class employee
{
Private
static string companyName;//Encapsulate fields
Public staric
string company//The package has no brackets ()
{
Get{
return companyName;}
Set(companyName
=value;)
}
}
Main(){
Employee.company
="My company";
}

Leave a Comment

Your email address will not be published.