Basic _ C # general program structure

Source: Internet
Author: User
C # general program structure

C # A program may consist of one or more files. Each file can contain zero or more namespaces. In addition to other namespaces, a namespace can also contain classes, structures, interfaces, enumerations, delegates, and other types. The following is the backbone of the C # program, which contains all these elements.

 

// Reference other namespaces
Using system;
// Current namespace
Namespace yournamespace
{
// Class
Class yourclass
{
}

// Structure
Struct yourstruct
{
}

// Interface
Interface iyourinterface
{
}

// Delegate
Delegate int yourdelegate ();

// Enumeration
Enum yourenum
{
}

// Subnamespace
Namespace yournestednamespace
{
Struct yourstruct
{
}
}

// Main class
Class yourmainclass
{
// Main method
Static void main (string [] ARGs)
{
// Your program starts here
System. Console. writeline ("Hello world! ");
System. Console. Readline ();
}
}
}

 

 

Main Method

C # the program must containMainMethod, and the program control starts and ends in this method. InMainCreate an object and execute other methods.

MainA method is a static method residing in a class or structure. In front of "Hello world !" In the example, it resides inYourMainClass . Declare using either of the following methodsMainMethod:

  • This method returns void:

    Static void main ()
    {
    //
    }
  • It can also return INT: static int main ()
    {
    //
    Return 0;
    }
  • Because there are two return types, it can contain the parameter: static void main (string [] ARGs)
    {
    //
    }

    Or

    Static int main (string [] ARGs)
    {
    //
    Return 0;
    }

    MainThe method parameter isstringArray, which indicates the command line parameters used to activate the program. Note that, unlike C ++, this array does not contain executable (exe) file names.

    • The main method is the entry point of the program. The program control starts and ends in the method.

    • This method is declared inside the class or structure. It must be a static method, rather than a public method. (In the above example, it accepts the default access level private .)

    • It can have the void or Int return type.

    • When declaring the main method, either the parameter or the parameter can be used.

    • The parameter can be read as a command line parameter that starts from scratch.

    • Unlike C and C ++, the program name is not treated as the first command line parameter.

  • For more information about using command line parameters, see main () and command line parameters (C # Programming Guide) and how to: create and use C # DLL (C # programming guide).

Input and Output

C # programs generally use the input/output service provided by the. NET Framework Runtime Library. StatementSystem.Console.WriteLine("Hello World!");The writeline method is used.ConsoleClass output method. It displays the string parameters used by the standard output stream, followed by a new line. Other console methods are used for different inputs.Console. Readline ()And OutputConsole. writeline ("Hello world! ")Operation. If the program containsusing System;Command, you do not need to fully limit the System Classes and methods to use them directly. For example, you can callConsole.WriteLineWithout specifyingSystem.Console.Writeline:

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.