May 11, 2021 C#
C# is an object-oriented programming language. I n object-oriented program design methods, programs consist of a variety of objects that interact with each other. Objects of the same kind usually have the same type, or are in the same class.
For example, take the Rectangle object. I t has length and width properties. By design, it may need to accept these property values, calculate area, and display details.
Let's take a look at the implementation of a Rectangle (rectangular) class and use it to discuss the basic syntax of C#:
using System;
namespace RectangleApplication
{
class Rectangle
{
// 成员变量
double length;
double width;
public void Acceptdetails()
{
length = 4.5;
width = 3.5;
}
public double GetArea()
{
return length * width;
}
public void Display()
{
Console.WriteLine("Length: {0}", length);
Console.WriteLine("Width: {0}", width);
Console.WriteLine("Area: {0}", GetArea());
}
}
class ExecuteRectangle
{
static void Main(string[] args)
{
Rectangle r = new Rectangle();
r.Acceptdetails();
r.Display();
Console.ReadLine();
}
}
}
When the above code is compiled and executed, it produces the following results:
Length: 4.5
Width: 3.5
Area: 15.75
The first statement in any C# program is:
using System;
The using keyword is used to include namespaces in the program. A program can contain more than one using statement.
The class keyword is used to declare a class.
Comments are used to interpret code. T he compiler ignores the comment entries. In the C# program, a multi-line comment starts with /?and terminates with the character s/, as follows:
/* This program demonstrates
The basic syntax of C# programming
Language */
A single-line comment is represented by a '//' symbol. For example:
}//end class Rectangle
Knowledge point supplement:
The difference between // Comments and // Notes in C#:
The introduction of a new XML comment in C# means that we start a new line in front of a function and enter ///, VS.Net automatically adds the comment in the XML format
Will not be compiled, and // will be compiled
So using /// slows down compilation (but does not affect execution speed)
But using /// provides intelligence awareness when someone else calls your code (usually in Form). Designer .cs program that automatically generates /// comments from the window) for example:
/// <summary>
/// 必需的设计器变量。 //定义属性,注意该属性为可读可写属性
/// </summary>
/// <summary>
///
/// </summary>
A variable is a property or data member of a class that stores data. In the program above, the Rectangle class has two member variables, named length and width.
A function is a series of statements that perform a specified task. T he member functions of the class are declared within the class. Our example class Rectangle contains three member functions: AcceptDetails, GetArea, and Display.
In the above program, the class ExecuteRectangle is a class that contains the Main() method and the instantiated Rectangle class.
Instantiation of a class of C#: The process of creating an object for that class from an existing class is called instantiation of a class.
For example: You designed a car model and gave it to the worker's master to process and make a car. The automobile model is equivalent to the class in the program, the car produced is the object, and the process of producing the car is called the instantiation of the class.
MyCar car1= new MyCar(); //类的实例化
An identifier is used to identify a class, variable, function, or any other user-defined item. In C#, classes must be named according to the following basic rules:
The keyword is a predefined reserved word for the C# compiler. These keywords cannot be used as identifiers, but if you want to use them as identifiers, you can prefix them with the character .
In C#, some identifiers have special meaning in the context of the code, such as get and set, which are called contextual keywords.
The following table lists reserved keywords and contextual keywords in C#:
Key keyword | ||||||
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 | in |
in (generic
modifier) |
int |
interface | internal | is | lock | long | namespace | new |
null | object | operator | out |
out
(generic modifier) |
override | params |
private | protected | 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 |
volatile | while | |||||
Context keyword | ||||||
add | alias | ascending | descending | dynamic | from | get |
global | group | into | join | let | orderby |
partial
(type) |
partial
(method) |
remove | select | set |
|