C# Section 1 (C# program structure, basic syntax, data types, xml annotations and their relationship with .Net)

1. .Net Framework (.Net Framework)

The following types of applications can be written on the .net platform:

  • Windows application
  • web application
  • web service

    2. The .net framework is a multi-platform application. The framework is designed so that it can be used in the following languages: C#, C++, Visual Basic, Jscript, COBOL. These languages ​​can interact with each other. Of course, the java language can also be used with C#. interact

2. Classes and methods

Classes generally contain multiple methods, which define the behavior of the class .

The following points are worth noting:

  1. C# case sensitive;
  2. All programs and expressions must end with a semicolon (;);
  3. Program execution starts from the Main method;
  4. Unlike java, the file name can be different from the class name (the public-modified class in ps:java must be the same as the file name)

In an object-oriented programming approach, a program consists of various objects that interact with each other . Objects of the same kind usually have the same type, or in other words, are in the same class.
For example, the Rectangle (rectangle) class implements the demo:

public class Rectangle
    {
        double length;
        double width;
        public void acceptDetail()
        {
            length = 4.5;
            width = 3.5;
        }
        public double getArea()
        {
            return length * width;
        }
        public void disPlay()
        {
            Console.WriteLine("length is {0}" , length);
            Console.WriteLine("width is {0}", width);
            Console.WriteLine("Area is {0}", getArea());

        }
class Test
{
    static void Main(string[] args)
        {
            Rectangle rectangle = new Rectangle();
            rectangle.acceptDetail();
            rectangle.disPlay();
            Console.ReadKey();
        }
}

Running result:
write picture description here
Note: placeholders in C# need one-to-one correspondence

            Console.WriteLine("A:{1},B:{0}", 66, 88);
            Console.ReadKey();

Running results:
write picture description here
ps: The instantiation of the class is the new object, in addition to this, there are also factory patterns, etc., which use the create() method to create objects;

(1) Identifier:
used to identify a class, variable, function or any other user-defined item. In C#, the naming rules for classes are:

  • Identifiers must start with a letter, underscore or @, and can be followed by a series of letters, numbers (0-9), underscore (_), @;
  • The first character in an identifier cannot be a number;
  • Identifiers must not contain any embedded spaces or symbols, such as ? -+! #%&…. ;: "'/*;
  • An identifier cannot be a C# keyword unless it has an @ prefix, eg: @if is a valid identifier, but if is not;
  • Identifiers must be case sensitive;
  • Cannot be the same as the C# class library name

    **

3. C# keywords

**
In C#, some keywords have special meaning in the context of the code, such as get and set, these are called contextual keywords.
The following table lists the reserved keywords of C# (Reserved Keywords) and context Keywords (contextual Keywords)
write picture description here
write picture description here

**

Fourth, the difference between // and /// comments in C#

**
C# introduces new XML annotations, namely ///;
{1}, // will not be compiled, and /// will be compiled, so using /// will slow down the compilation (but will not affect Execution speed)
{2}, using /// will provide intellisense when calling code in other classes (usually in Form, Designer.cs, a program that will automatically generate /// comments)
{3}, with /// After adding annotations to classes, properties, and methods in this way, when you use the classes, properties, methods, etc. added in this way in other places, the prompt box will display the annotations you wrote, so that you can understand you better. The role of the function to be used.
C# introduces a new xml comment, that is, start a line before a function, enter ///, and VS.Net will automatically add comments in XML format. Here, let's sort out the XML comments used. XML annotations are divided into primary annotations (Primary Tags) and secondary annotations (Secondary Tags).
first-level annotation

  1. Annotate classes, methods, properties, or fields of common types;
  2. It is mainly used for annotation of attributes, indicating the meaning of attribute control, and can be used together;
  3. Describe the type, the function is similar, it is said that it is recommended to use;
  4. Used to describe the parameters of the method, format: value;
  5. It is used to define the return value of the method. For a method, after entering ///, it will automatically add , , ;
  6. Define the exceptions that may be thrown, in the format:;
  7. Used to give how to use a certain method, property or field usage;
  8. access permissions involving methods;
  9. Used to refer to some other thing, you can also set attributes through cref;
  10. XML comments used to indicate external;
    second-level comments

  11. or 主要用于加入代码段;

  12. works like in HTML

    The marker is the segment;

  13. Used to refer to a parameter;
  14. The effect is similar, and other methods can be indicated;
  15. used to generate a list;
  16. You can also customize XML tags
    2" and let C# smart comments display multiple lines.
    When C# smart instructions are used, it is often hoped that it can be displayed as a newline during development, making the prompt more friendly! It's actually quite simple, just need to add tags to something like , , , etc.

C# data types

In C#, variables are divided into the following types:

  • Value types
  • Reference types
  • Pointer types
    value types
    Value type variables can be directly assigned to a value. They are derived from the class System.ValueType.
    Value types directly contain data, such as int, char, float
    . The following table lists the available value types:
    write picture description here

If you need to get the exact size of a slap type or variable on a specific platform, you can use the sizeof method. The expression sizeof(type) yields the storage size of the object or type in bytes, eg: Get the int type on any machine storage size

 static void Main(string[] args)
        {
            Console.WriteLine("size of int is:{0}", sizeof(int));
            Console.ReadKey();
        }

The running result is:
write picture description here

Reference types Reference types
do not contain the actual data stored in the variable, but contain a reference to the variable; that is, a memory address, a reference type can point to a memory location, if the data of the memory location is changed by a variable , other variables will automatically reflect this value change. The built-in reference types are: object, dynamic and string
1. Object type
The object type is the ultimate base class of all data types in the Common Type System (CTS). Object is an alias for the System.Object class. So the Object type can be assigned a value of any other type (value type, reference type, predefined type or user-defined type). But before assigning the value, a type conversion is required.
When a value type is converted to an object type, it is called boxing ; on the other hand, when an object type is converted to a value type, it is called unboxing . eg:

object obj;
obj = 100;//这是装箱

2, dynamic (Dynamic) type
can store any type of value in the dynamic data type variable. Type checking of these variables happens at runtime.
Syntax for declaring dynamic type:

//dynamic < variable_name >= value;
dynamic d = 20;

Dynamic types are similar to object types, but the type checking of object type variables happens at compile time , while the type checking of dynamically typed variables happens at run time .

3. String type

The String type allows assigning any string value to a variable. The String type is an alias for the System.String class. It is derived from the Object type. Values ​​of type String can be assigned in two forms: quotes and @quotes. eg:

String str = "baidu.com";

A @quote string:

@"baidu.com"

C# String strings can be preceded by @ (called "verbatim strings") to treat escape characters (\) as normal characters, eg

String daStr = @"C:\Windows";

Equivalent to:

String daStr = "C:\\Windows";

@String can be arbitrarily newline, newline characters and indented spaces are counted within the length of the string

String str = @"<script type=""text/javascript"">
<!--
-->
</script >";

User-defined types are: class, interface or delegate.

Pointer types
Pointer type variables store another type of memory address. Pointers in C# have the same functionality as pointers in C or C++.
The syntax for declaring a pointer type:

type* indentifier;

eg:

char* cptr;
int* iptr;

Notes:
1. About packing and unpacking

  • Boxing: Converting a value type to an object type, eg:
int val = 8;
object obj = val;//整型转换为独享类型(装箱)

Unboxing: The object type previously converted from the value type is converted back to the value type, eg:

            int val = 66;
            object obj = val;//装箱
            int nval = (int)obj;//拆箱

Note: Only unpacked data can be unpacked.
Packing requires manpower and man-hours (that is, consuming CPU and memory). Similarly, unpacking also requires manpower and man-hours.
Second, the relationship between obj (reference) and int (value type)

             int val = 55;
            object obj = val;
            obj = 52;
            Console.WriteLine("val的值为:{0}", val);
            Console.WriteLine("obj的值为:{0}",obj);
            Console.ReadKey();

Running result:
write picture description here
It can be seen that the reference type just copies the value of the value type, and then operates on it, without changing the value of the original value type.

3. The difference between string and String in
C# stringshi is a class in C#, String is a class of .NET Framework (it will not display blue in C# IDE) c# string is mapped to String of .NET Framework, if you use string, The compiler will mutate it into String, so using String directly will make the compiler do a little less work.
If you use C#, it is recommended to use string. The comparison is in line with the specification. String always represents System.String(1.x) or ::System.Strig(2.0). String is only preceded by using System; and there is no name in the current namespace. When it is a type of String (class, struct, delegate, enum), it means System.String string is a keyword, string is not, that is to say, string cannot be used as the name of a class, structure, enumeration, field, variable, method or property , while String can.
String is a CLR type name (also a keyword), and string is a C# keyword. When a string is compiled, the C# compiler will convert it to String by default. The colors are represented differently in VS: String is green, string is blue.
write picture description here
Fourth, the value type characteristics:

  1. New types cannot be derived from value types, but structs can implement interfaces.
  2. Value types cannot contain null values;
  3. Every value type has an implicit default constructor that initializes the type's default value;

Each value type has an independent memory area to save its own value. When it is called, its value is called, while the reference type calls the address in the memory. For example, if the reference type a1=10 is defined, then the memory 10 is stored in . When a1 is assigned to a2, they both use the same memory space , and the value of a2 will be saved as the value of a1. When a2 is changed to 20, because a1 and a2 point to The same memory space, so a1 also becomes 20, which is a reference type ;
the value type is: when a1 is assigned to a2, a new memory space will be reallocated for a2, and the value of a1 will be saved (copy a copy to a2), when a2 is modified to 20, 20 will be saved in the space of a2, which has nothing to do with a1.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325535597&siteId=291194637