.NET framework and preliminary application in C ++

zhaozj2021-02-16  58

.NET framework and preliminary application in C

In the latest released Visual Studio.net, a new technology is called .NET framework. This is a new programming mechanism, Microsoft is designed to provide a unified object-oriented environment, and ensure transparency of the object (ie, whether this object is in this machine, the Internet, or in a remote environment ); Make the version conflict in various software to minimize; so that the object is independent, it can be used across language. In the .NET framework, there is a large part of the characteristics very similar to Java, such as memory management, class library reference, similar to the byte code MSIL code, and more.

In order to fully use the .NET frame, Microsoft launches the language version of C # .NET. I will use the C # to see the .NET framework mechanism. First look at a complete C # program:

001: using system;

002: using system.drawing;

003: using system.windows.forms;

004:

005:

007:

008: Class Sample

009: {

010: SAMPLE ()

011: {

012: m_nmember = 0;

013:}

014:

015: ~ Sample ()

016: {

017: System.Console.writeline ("I was released.");

018:}

019:

020: Delegate Void Methoddge (String StrMessage);

021:

022: Public Int Member

023: {

024: SET

025: {

026: m_nmember = value;

027: System.console.writeline ("is assigned.");

028:}

029: Get

030: {

031: System.Console.writeline ("is taken.");

032: Return m_nmember;

033:}

034:}

035:

036: Public Void Click (Object Sender, Eventargs E)

037: {

038: System.console.writeline ("is clicked! {"};

039: THIS.MEMBER = 13;

040: System.console.writeLine (Member.toString () ")")

041:}

042:

043: Public void Ownmethod (String StrMessage)

044: {

045: System.console.writeline (StrMessage); 046:}

047:

048: public int m_nmember;

049:

050: public static void main ()

051: {

052: Sample asample = new sample ();

053: Form DLG = New form ();

054: EventHandler Click = New EventHandler (asample.click);

055: DLG.Text = "This is a .NET form";

056: DLG.Click = Click;

057: DLG.ShowDialog (NULL);

058: methoddge inokemethod = new methoddge (asample.OWNMETHOD);

059: Inokemethod ("This is called by the commission." Inokemethod.toString ());

060:}

061:}

This program basically covers most of the characteristics of the .NET. Basically, it is easy to understand, and the following is simple to talk about .NET's characteristics, please look at the example.

.NET class library reference

First, in Java, use import to reference class libraries, and C # is using USING, C uses #using to conduct import class libraries. And these class libraries are MSIL code that is encapsulated in the .dll file, called Microsoft Intermediate Language Code. These MSIL code are executed by the .NET Framework Real Time (JIT) compiler, which is a bit like the interpreter in Java interprets the word code. We write managed code (ie .NET program code) will be compiled into a MSIL code by the compiler, and then written in the PE format according to the PE format. When the system execution file encounters MSIL, use the .NET framework real-time compiler to compile the real-time MSIL to the cost machine code, and then perform the unit code.

.NET memory recycling

There is a garbage recovery mechanism in Java, which can be released automatically from the lost reference object. The public language of the .NET framework also contains this feature, we can do it after the new object, there is no need to worry about DELETE, the public language runtime will know when to release it, and will not affect the resource Efficiency, maybe it will increase. In the main function in the example, asample references a Sample object that is out of New. At the end of the main function, asample lost its role, the public language runtime, when there is no one in the object that new comes, it will be destructed immediately. Thus, at the end of the main function, the destructor of the SAMPLE is executed.

.NET type

1. Object type

Took the type, in .NET, all types (including int, float, double, etc.) are derived from Object classes, so an Object variable can be assigned to any type of value. Since there is a description of the type in Object, there is no need to worry about the type in the object of Object. Such as:

INT A = 13;

Object b = a;

System.console.writeline (b);

WriteLine will know that the value in B is integer, and its output is still 13.2. Value type and reference type

In .NET, the type is divided into value type and reference types (the same existence in Java), reference type behavior is equivalent to pointers in C / C , including class types (with keyword class declaration) , Interface type (type with keyword interface declaration), array type, delegate type (type with keyword delegate declared). The difference between the value type and the reference type is as follows:

Class myclass

{

Int a;

}

Class Example

{

Static void main (string [] args)

{

INT value1 = 12;

INT value2 = value1;

Myclass Citation1 = new myclass ();

Myclass Citation2 = Citation1;

Value1 = 13;

Value2 = 15;

Citation1.a = 13;

Citation2.a = 15

System.console.writeline (Value1.TOSTING () "" Value2.TOSTING ());

System.console.writeline (Citation1.a.toString () "," Citation2.a.toString ());

}

}

Its result

13,15

15,15

When a value type is assigned to another value type, int value2 = value1; is to copy the value of Value1 to Value2. Therefore there is actually two integer variables. So when the value2 value changes, the value of Value1 is not affected. When a reference type is assigned to another reference type, myclass citation2 = Citation1; is the object referenced to Citation2 into Citation1, so there is only one MyClass object. Citation1 and Citation2 points to an object, so when Citation2.a, Citation1.a also changes at the same time, and two of them are the same object. This is the difference between the reference type and value type, which is a more important concept in .NET. However, this is not large in C / C , because .NET is used in C to reach the concept of reference (although there is your own reference syntax in C ), we can still use pointers to use value types. Use.

As in C , Object __gc * Target is equivalent to Object Target in C #;

[C #]

Object target = new Object ();

Target.toString ();

[C ]

Object __gc * target = new object ();

TARGET-> TOSTRING ();

Both are equivalent.

In C __gc is represented as a hosted type (ie .NET type).

3. Entrusted type

In .NET, the delegated behavior is the same as the function pointer of the C / C . If the EventHandler in the first example, Methoddge is a delegate type. Different, methoddge is the type of entrustment in our own, EventHandler is a delegate type defined by the system assembly. A delegate type is why:

[C #]

DELEGATE Return Value Type Name (Parameter List);

[C ]

__delegate Return Value Type Name (Parameter List);

Please see the example of the example of the example.

A delegate object is this:

[C #]

Entrusted type name entrustment object name = new (object. Method) [C ]

Entrusted Type * Entrust Object Name = New (Object Pointer, & Category :: Method))

Please see the example of Chapter 54 and 58.

When the delegate object is called, it is equivalent to the function called the referenced.

[C # & C ]]

Entrusted object name (parameter list);

Please see the example of the example of the example.

Moreover, the delegation type supports accumulation ( =) and accumulation (- =), when the delegate object is called, the effect is accumulated simultaneously to all delegated objects;

[C # & C ]]

Entrusted object name = commission type object name;

Entrusted object name - = commission type object name;

Please see the example of the example of the example.

The entrusted object is mainly used on an event. Event, in Java is implemented using an interface, the .NET framework is implemented by the delegate object. There are several event delegate objects (EventHanLder types in each event), where two parameters are Object Sender, Eventargs E) correspond to the event. If you have a support clicking event, this class must have a delegate object attribute called Click. This delegate object is called when the class object is generated by clicking the event.

IF (Click! = NULL)

Click (this, e);

If you want to subscribe to this event, you can add your own delegate object, and cancel the subscription, it is tired to reduce your own delegate object.

In the first example

.NET's class

1. Attributes

In the first example, class SAMPLE has a statement of Member members. This is a statement of an attribute. When MEMBER is called, call the GET block to return a full value; call the SET block when Member is assigned, the parameters in C # are implicitly declared as Value.

In C is declared with __property

[C ]

__property type GET_ attribute name (); // assignment

__property void set_ attribute name (Type value); // Take the value

Please see the examples of the examples 22nd to 34 lines.

2. Class that cannot be instantiated

Use Keyword Abstract to display the declaration this class as an abstract class, whether it has a pure virtual function. When the class is declared as an abstract class, it does not allow instantiation of the class object. The declaration method is as follows:

[C #]

Abstract Class class name

{

....

}

[C ]

__ABSTRACT __GC Class class name

{

...

}

In C __gc is represented as a managed type (ie .NET type), __ NOGC display declaration class is a non-host type (ie normal C class)

3. Can't be used as a base class

Use Keyword Sealed to declare classes as package classes. When classes are declared as package classes, there is not allowed to have any classes to be based on it. The declaration method is as follows:

[C #]

Sealed Class class name

{

....

}

[C ]

__sealed __gc class class name

{

...

}

The above is the characteristics of .NET behavior, the first example is turned into C managed program code, the functional effect is the same as the C # program:

#using

#using

#using

#using

Using Namespace System;

Using namespace system :: windows :: forms;

__GC Class Sample

{

PUBLIC:

Sample (void)

{

m_nmember = 0;

}

~ Sample (Void)

{

System :: console :: WriteLine ("I was released.");

}

__dlegate void methoddge (string * strmessage);

__property void set_member (int nvalue)

{

m_nmember = nvalue;

System :: console :: WriteLine ("is assigned.")

}

__property int GET_MEMBER (VOID)

{

System :: console :: WriteLine ("is taken.");

Return m_nmember;

}

Void Click (Object * Sender, Eventargs * e)

{

System :: Console :: WriteLine ("Click!");

System :: console :: WriteLine ("{");

MEMBER = 13;

System :: Console :: WriteLine (Member.toString ());

System :: console :: WriteLine ("}");

}

Void Ownmethod (String * StrMessage)

{

System :: Console :: WriteLine (STRMESSAGE);

}

INT m_nmember;

}

void main ()

{

Sample * asample = new sample ();

Form * DLG = New form ();

EventHandler * Click = New EventHandler (Asample, Sample :: Click);

DLG-> Text = "This is a .NET form";

DLG-> Click = Click;

DLG-> ShowDialog (0);

Sample :: methoddge * inokemethod = new sample :: methoddge (asample, sample :: wMETHOD);

InokeMethod (String :: Concat ("This is called.", INOKEMETHOD-> toString ()));

}

The first line of #using is a public run.

To compile the passage on Visual C ., First first compile the compilation in the regular record in the project property page to manage set support.

Specific Category Library Appions View the .NET Framework on MSDN.NET. (. NET Framework -> Reference -> Class Library)

C Extended Hosting Review Visual C . Net on MSDN.NET. (Visual C . Net-> C Hosting Extension Programming)

Blue_atlantis400@hotmail.com

Release snow

2002.11.30

转载请注明原文地址:https://www.9cbs.com/read-24713.html

New Post(0)