Int, float, double, char, string
String.Length is read-only and cannot be assigned a value, string is not becoming
Stringbuilder can modify the data of a variable
Var
Value types and reference types
A value type is a value copy, a variable is copied to another variable, and a memory copy of the original variable is created, so changing the value of one variable does not affect the other variable. When a method is called, the row parameter does not change the value of the argument
The reference type does not store the value, but instead stores the address
Nullable modifier:? Int? A = null;
Data type conversions:
Large type to small type needs to be displayed convert long to in int i= (int) long type
Conversion failure throws an exception
Small type to large type hermit convert long number = int type value
Parse transformation and convert transformation, converting one type to another type TryParse ()
Array string[] s = new string[5];
String[] s = {"2"};
Struct and Class
A Struct cannot contain a constructor with the same name without parameters, supports a constructor with a parameter, must initialize all fields, cannot initialize a default constructor with the same name in the property class
A struct can be a new struct when it is called outside.
Declaring a field that initializes a struct at the same time causes an error. The field value cannot be initialized. Class can
Struct Angle
{
int _hour = 9; --Error Cannot initialize value
}
Class Test
{
Public Test ()
{
}
private String name = "";
public string Name {get {return Name;} set {}}
}
struct TESTSTRUCT
{
Public teststruct (string name) with parameter constructor, initializes all fields
{
THIS.name = name;
}
private string name;
public string Name {get {return Name;} set {}}
public void Test ()
{
}
}
Var must be initialized when defining variables. var i; It is wrong to have var i=0; Value must be initialized
Var i=0;
I= "SSS";
Wrong, string cannot be converted to int
The static member static, this keyword is invalid in this method
Const and ReadOnly
A Const is an immutable value that, in the assembly that uses it, does not necessarily reflect the changed value, which is the value that is determined at compile time
Values that may change in the future should be specified as ReadOnly
A Const can be used in a method.
Readonly can not be used in methods, must be under the class, outside the method
Class Program
{
readonly int t = 0;
static void Main (string[] args)
{
const int i = 0; --can be used in the method of const
}
void Test ()
{
int j = 0;
const int i = 0;
readonly int kk=0; ---wrong, ReadOnly can't be used in a method.
}
}
C#-1 Data types