Learning C # From Scratch -- basic syntax (2 ),
Basic syntax
(Cainiao series tutorial)
C #, also known as Csharp, like C well.
C # is an object-oriented programming language. In the object-oriented programming method, a program consists of various interactive objects. Objects of the same type usually have the same type, or are first communicated to that class.
For example, a Rectangle (Rectangle) object has the length and width attributes. According to the design, it may need to accept these attribute values, calculate the area and detailed details.
Let's take a look at the implementation of a Rectangle class and use this to describe the basic syntax of C #.
1 using System; 2 namespace RectangleApplication 3 {4 class Rectangle 5 {6 // member variable 7 double length; 8 double width; 9 public void Acceptdetails () 10 {11 length = 4.5; 12 width = 3.5; 13 14} 15 public double GetArea () 16 {17 return length * width; 18 19} 20 public void Display () 21 {22 System. console. writeLine ("Length: {0}", length); 23 System. console. writeLine ("Width: {0}", width); 24 System. console. writeLine ("Aree: {0}", GetArea (); 25 26} 27 class ExecuteRecatangle28 {29 static void Main (string [] args) 30 {31 Rectangle r = new Rectangle (); 32 r. acceptdetails (); 33 r. display (); 34 System. console. readKey (); 35} 36} 37} 38}
Compilation result:
Using Keyword
The first statement in any C # program is:
using System:
The using keyword is used to include namespaces in a program. A program can contain multiple using statements.
Class keywords
The class keyword is used to declare a class.
Comments in C #
Annotations are used to interpret code. The compiler ignores comments. In the C # program, multi-line comments start with/* and end with the character,
/* This is the multi-line comment of C # programming language */
A single line comment is represented by the '//' symbol,
// This is a single line comment of C # Language
Member variables
A variable is a type of scholarly or data member used to store data. In the preceding program, the Rectangle class has two member variables named length and width.
Member Functions
A function is a series of statements used to execute tasks. The member functions of a class are declared in the class. In our tears, Rectangle contains three member functions:
AcceptDetails, GetArea, and Display.
Instantiate a class
In the above program, the class ExecuteRectangle is a class that contains the Main () method and instantiates the Rectangle class.
Identifier
An identifier is used to identify a class, variable, function, or any other user-defined project. In C #, the class name must follow the following basic rules:
The identifier must start with a subtitle or underscore (_), followed by a series of subtitles, numbers, or underscores. The first character in the identifier cannot be a number.
The identifier must not contain any embedded spaces or symbols, but can be underlined.
The identifier cannot be a C # keyword.
C # keywords
The keywords are reserved words predefined by the C # compiler. These keywords cannot be used as identifiers. However, if you want to use these keywords as identifiers, you can add the @ character before the keywords as the prefix.
In c #, some identifiers have special meanings in the context of the Code, such as get and set.
The reserved keywords and context keywords in C # are listed below;
Abstaract, as, base, bool, break, byte, case, catch, char, checked, class, const, coutinue, decimal, default, delegate, do, double, else, enum, event, explicit, extem, false, finally, fixed, float, for, foreach, goto, if, implicit, in, int, interface, intemal, is, lock, long, namespace, new, null, object, operator, out, override, params, private, protected, public, readonly, ref, return, sbyte, sealed, short, sizeof, stackalloc, static, string, struct, switch, this, throw, true, try, typeof, uint, ulong, unchedked, unsafe, ushort, using, vitual, void, volatile, while
Context keyword
Add, alias, ascending, descending, dynamic, from, get, global, group, into, join, let, orderby, partial, remove, select, set