Search C # (foreword) a week

zhaozj2021-02-16  52

Search C # (foreword) a week

C # Talent Bird (QQ: 249178521)

Hello everyone! C # As a new language launched in the 21st century, it has an advantage that other languages ​​cannot match. But how to quickly grasp it in a short time, but it is a more difficult problem. But if you read this tutorial, you will understand and master C #.

This tutorial is divided into six parts. He first introduces the basic concepts in C #.

Overall framework

Hiker.cs class name is not necessarily equal to the file name

Using system; // Each program must use this statement at the beginning

Public Sealed Class Hitchhiker

{

Public static void main () // The program starts from MAIN

{

Int result;

Result = 9 * 6;

Int thirteen;

Thirteen = 13;

Console.write (result / thirteen); // Output function

Console.write (Result% Thirteen);

}

}

/ / Will introduce the specific usage of each statement above

/ * This program is used

* Demonstrate the overall framework of C #

* /

Note: In the above program, the symbol // indicates that the comment, the content on the same line behind // is a comment;

/ * And * / this content is comment

You can type in Windows command line prompt: CSC Hiker.cs

Compile to generate executable Hiker.exe

Then type in the Windows command line prompt: hiker, you can see the visual 42 on the screen.

(Note: You must have .NET Framework)

SINGLE-LINE COMMENT

PROGRAM

Execution

Starts

AT

Main

with

Java

Different,

C #

source

File name is not necessarily

C #

source

The class name contained in the file is the same.

C # Sensitive to case, so the first letter of Main is uppercase M (this point everyone should pay attention, especially friends who are familiar with C language).

You can define a main function that the return value is Int, indicates success when the return value is 0:

Public Static Int

Main

() {... return 0;}

You can also define the return value of the main function to void:

Public static void

Main

() {...}

You can also define the main function to receive a String array:

Public static void

Main

(String [] ARGS)

{

Foreach (String args in args) {

System.console.writeline (arg);

}

}

The main function in the program must be static.

2. Identifier

Name of the identifier:

ü Local variables, local constants, non-public examples, function parameters use the Camelcase rules; other types of identifiers use the Pascalcase rules.

PrivateStyle Camelcase Rules (the first letter of the first letter, the first letters of the remaining words)

PublicStyle Pascalcase Rules (All letters of all words)

ü Try not to use abbreviations.

Message, not to use MSG.

ü Don't use Hungarian nomenclature.

Public Sealed Class Grammarhelper

{...

Public QualifiedSymbol Optional (Anysymbol Symbol)

{...}

Private AnymultiPlicity Optional = New OptionalMultiPlicity ();

}

3. Keywords

76 keywords in C #:

Abstract as base bool break

Byte Case catch char checked

Class Const Continue DeciMal Default

DELEGATE DO DOUBLE ELSE ENUM

EVENT Explicit Extern False Finally

Fixed Float for Foreach Goto

IF IMPLICIT INTIT INTERFACE

Internal Is Lock Long Namespace

New Null Object Operator Out

Override Params Private Protected Public PUBLIC

Readonly Ref Return Sbyte Sealed

Short Sizeof Stackalloc Static String

Struct Switch this throw True

Try TypeOf uint Ulong Unchecked

Unsafe ushort using virtual void

While

5 in some cases is keywords:

Get Set Value Add Remove

76 of C # have a fixed keyword in any case. There are also five identifiers that have fixed in a particular situation. For example, Value can be used as a variable name, but there is a case exception, which is a keyword when it is used as a set statement of the attribute / indexer.

But you can add @ to make it as a variable name in front of the keyword:

INT @INT = 42;

However, do not use this variable name in general.

You can also use @ to generate strings across several lines, which is very useful for generating regular expressions. E.g:

String Pattern = @ "

(# Start the group

Abra (CAD)? # Match Abra And Optional CAD

) "; # One or more ocurrences

If you want to include double quotes in the string, you can do this:

String quote = @ "" "quote" "";

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

New Post(0)