The learning of C# is learned together with unity3D. Next, I will record some experiences and some notes in the learning process.
C# is an object-oriented, concise and safe programming language created by Microsoft. Based on C/C++ language, the development environment is visual studio, and the latest version is C#4.0
C# must run on net.framework, which includes common language runtime, net.framework class library
Here are some basic concepts:
Class: It abstracts objects with the same nature into a class, which is a logical abstraction concept that can contain various member variables, such as: fields, constants and methods
Object: An object is a concrete thing, an instance of a class, through which the instance fields and methods of a class can be called
Method: It is a member of a class and is a block of code containing a series of statements that can change the state of an object
The Main() method, called the main method, is the entry point for all programs to run. The rest of the methods are called with parameters and without parameters. If the function with parameters is called, the parameters must be executed when calling. transfer
Namespace: defines a declaration area, provides a way to distinguish one set of names from another, the declared name of one namespace will not conflict with the same name of another namespace, use using to introduce naming space
C# extension
Inheritance: A class contains multiple subclasses, subclasses are more specific than the parent class, and have more behaviors and properties
Polymorphism: A class can be called in different methods and can render different results
Interface: An interface and a class are similar, but an interface only describes a specification and does not specifically implement these specifications
Classification of C# Types
Value type: It is a variable that directly contains data, stores data, its value is not null, and the type is generally a structure type or an enumeration type
Reference type: its variable only references the data it stores, and stores the access address
Pointer type: only used in unsafe code, generally not commonly used
1. The basis of the value type is System.ValueType
3.3 Reference types
Variables of reference types also become objects, and there are six types:
(1) Object types
(2) String type
(3) Class type
(4) Array type
(5) Interface type
(6) Delegation type
Before using a variable of reference type, it is necessary to determine whether the value of the variable is null. If it is, it cannot be accessed directly
1. Class type: support inheritance mechanism
2. Object type: built-in reference type, namely System.object
3. String type: Represents a sequence of zero or more unicode characters, corresponding to System.string, created with double quotes. Once it is created, the content in the created memory cannot be modified. To change the value, you can only reallocate a new piece of memory
4. Array type: When declaring an array, the element type should be followed by square brackets
5. Interface type: An interface is just a data structure that can declare data members and function members. It can only declare properties, methods and events, but cannot implement these properties, methods and events.
6. Delegate type: it can refer to one or more methods, can pass methods as parameters, and can also define callback methods
3.4 Packing and unpacking
Role: mutual conversion of value types and reference types
Reference types are always allocated on the managed heap, value types are always allocated on the stack
1. Boxing: Convert a value type to a reference type. The specific process is: first allocate an object instance, and then copy the value of the value type into the instance. For reference instances, the same instance is shared before and after boxing
Four common boxing:
(1) value type to object type
(2) Value type to System.ValueType type
(3) The interface type implemented by the value type to the value type
(4) Enumeration type to System.Enum type
2. Unboxing: Convert a reference type to a value type. The specific process is: check whether the modified object instance is a boxed value of a given value type, and copy the value from the instance (more checks than boxing) that step)
Four common types of unboxing:
It is to reverse the previous packing process.
Three methods of System.ValueType:
(1) Equals(): Determine whether the current instance and the specified object are equal
(2) GetHashCode(): Returns the hash code of the current instance
(3) ToString(): Returns the string form of the current variable
2. Integer type
(1) 32 bits of Int integer, the range is -2^32~2^32-1
(2) 64 bits of Long integer, the range is -2^64~2^64-1
(3) Char 16-bit unicode, which can be implicitly converted to an integer, should be enclosed in single quotation marks when defining
3. Floating point type
Two floating-point types: single-precision floating-point (float) and double-precision floating-point (double)
Abbreviated to Nan when it is not a number
When writing the float type directly, add f or F after the value
When writing the double type directly, add d or D after the value
4.decimal type
Represents financial calculations and monetary calculations, and represents 128-bit calculations
There is no implicit conversion between float and decimal because the latter has higher precision but a smaller range than the former
When writing, you need to add m after
Signed 0, infinity and Nan are not supported
5. bool type
Boolean type has only two values: true or false
No standard conversions exist before boolean types and other values
6. Enumeration Types
Put together a set of data of the same type and express a fixed meaning as a set
Usually contains one or more enumeration values, each enumeration value is separated by a comma, the default base type is Int
7. Structure Type
is a single entity composed of a set of related information