C # knot in Visual C # programming Primer

Source: Internet
Author: User
The key concepts of program structure in visual| programming C # are programs, namespaces, types, members, and assemblies. A C # program includes one or more source files. A type is declared in a program, the type contains members, and can be organized into namespaces. Classes and interfaces are examples of types. Fields, methods, properties, and events are examples of members. When C # programs are compiled, they are physically packaged into assemblies. The assembly's file name extensions are generally. exe or. dll, depending on whether they are implemented as an application (application) or a class library (library).
Example:

Using System;
Namespace Acme.collections
{
public class Stack
{
Entry top;
public void Push (object data) {
Top=new Entry (Top,data);
}

public Object Pop () {
if (top==null) throw new InvalidOperationException ();
Object Result=top.data;
Top=top.next;
return result;
}
Class Entry
{
Public Entry Next;
public object data;

Public Entry (Entry next,object data) {
This.next=next;
This.data=data;
}
}
}
}

In a namespace called Acme.collections, declare a class named Stack, the fully qualified name of the class is Acme.Collections.Stack. It includes several members: A field called Top, two methods named Push and Pop, and a nested class named entry. The entry class further includes three members: A field named Next, a field named data, and a constructor. Assuming that the source program for this example is saved as a Acme.cs file, the command behavior:

Csc/t:library Acme.cs

Compile this example into a class library (code without the main entry point) and produce an assembly named Acme.dll.

Assemblies include executable code in the form of intermediate language (intermediate Language,il) directives, as well as symbolic information in the form of metadata (metadata). Before it executes, the IL code for the assembly is automatically converted to a specific processor code by the. NET Common language runtime (Common Language runtime,clr).

Because an assembly is a self-describing unit of functionality that includes both code and metadata, #i nclude directives and header files are not required in C #. If a C # program needs to refer to public types and members in a particular assembly, then simply referencing that assembly at compile time is OK. For example, the following program uses the Acme.Collections.Stack class from the Acme.dll assembly:

Using System;
Using Acme.collections;

Class Test
{
static void Main () {
Stack s=new stack ();
S.push (1);
S.push (10);
S.push (100);
Console.WriteLine (S.pop ());
Console.WriteLine (S.pop ());
Console.WriteLine (S.pop ());
}
}

If the program is saved as a test.cs file, then Acme.dll can be referenced through the/R option when Test.cs is compiled:

Csc/r:acme.dll Test.cs

This allows you to create an executable assembly named Test.exe, which results in the following operation:

100
10
1


C # allows the source text of a program to be saved as several source files. When a multiple-file C # program is compiled, all source files are processed together, and each source file is conceptually free to reference each other, as before processing, all of the source files are connected to a large file. It is not necessary to declare forward in C # because the order of declarations is irrelevant. C # does not limit a source file to declaring only one common type, nor does it require that the source file name must match the type in the file. 1.


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.