bzdww

Get answers and suggestions for various questions from here

Getting started with C# basic syntax

cms

I. Identifiers and keywords

Second, the basic data type

Third, variables and constants

Fourth, operators and expressions

Five, C# statement


I. Identifiers and keywords

As long as it is a language, there will be the concept of identifiers and keywords. The identifier must meet the following conditions:

  1. Can only contain letters, numbers or underscores.
  2. The initials can be letters, underscores, and @symbols, but they cannot be numbers.
  3. Can't be a C# keyword.
  4. C# is a case-sensitive language. When defining, try to preserve its meaning and make it self-descriptive.

Second, the basic data type

In C#, the two most common data types are value types and reference types. Value types can also be called basic data types, including simple types, enumerated types, and structure types. Reference types include string types, array types, class types, object types, interface types, and proxy types.

2.1 Data type cast

C# is a mandatory type language, the compiler will automatically detect the data type, if it is inconsistent, it will automatically perform data type conversion. The conversion that the compiler can automate is called implicit conversion. If you can't implicitly convert, you need to convert the display. There are three ways:
(1) use the cast operator ()

int ii=50;
short ss=(short)ii;

(2) use the static member methods of the System.Convert class, ToString, ToInt, ToDouble, etc.

int ii=10;
    
string str=System.Convert.ToString(ii);

(3) Using the Parse() method provided by the .Net framework

string ff = "200";float f = float.Parse (ff);Console.WriteLine (f);

2.2 String Type

Stirng is an alias for System.String that represents an immutable sequence of zero or more Unicode characters. You can use [] to access each character.

string welcomeText = "欢迎你";
welcomeText=Hi";

Before reassigning, the memory first reinitializes an area and initializes the value Hi. The original memory area cannot be modified.
The string type is a reference type, the string variable itself is stored on the stack, and
the value pointed to by the variable is stored in the managed heap.


2.3 Enumeration type

The enumerated type is a value type and is used as follows:

enum Gender{Female,male};


2.4 Structure Type

The structure in C# is a value type, usually used to represent lightweight objects such as Point, Rectangle, Color, etc. The structure definition is as follows:

public struct APoint{
public int x;
public int y;
public APoint(int px,int py){x = px;y = py;}}

2.5 array type

An array is a reference type that indicates that the same type of data is linearly arranged in a certain order. The declaration method is as follows:
type[] array name=new type[arrayCount];
the specific use of the array is as follows:

int[] a = new int[] { 1,2,3,4,5} ;int[] b = { 1, 2, 3, 4, 5 };Console.WriteLine (b.Length);
string[] Colors =
{ "Red","Blue","Green","Purple","Orange"} ;for (int i = 0; i < Colors.Length; i++) {Console.WriteLine (Colors[i]);
foreach(var item in Colors){Console.WriteLine (item);

Third, variables and constants

A variable represents a block of memory, and the value it stores can be changed. Because of the variables, we no longer need to remember the complex memory address, but the memory name access operation through the variable name. The value of the constant in the memory cannot be modified. It must be initialized when it is declared. It cannot accept the new assignment. The default is static. It is not allowed to explicitly use the static keyword for declaration.

Fourth, operators and expressions

An operator is a program element that is used to manipulate one or more operands. The conditional operator accepts three operands and is the only ternary operator in C#. The precedence of the operators is shown in the following table. The order in which the operators are combined, only three are combined from right to left, respectively, the unary operator, the conditional operator, the assignment operator, and the others are from left to right.
Operator overloading
C# also has operator overloading, mainly to use an operator for a custom type. For unary operators, overloading requires a class or structure type as a parameter; and binary operator overloading requires two classes or structures of that type. The binary syntax is as follows:
public static type operator overloaded operator (type 1, type 2) It
can be seen that all operators should be static methods, and need to be modified with the public keyword.

public struct Complex{public int real;public int imaginary;public Complex(int r,int c){this.real = r;
this.imaginary = c;}
public static Complex operator +(Complex c1,Complex
c2){Complex result = new Complex ();
result.real = c1.real + c2.real;result.imaginary = c1.imaginary + c2.imaginary;return result;}
{public override string ToString ()return string.Format ("{ 0}+{ 1} i",real,imaginary);}}Complex num1 = new Complex (2, 4);Complex num2 = new Complex (3,6);
Complex sum = num1 + num2;Console.WriteLine ("