struct: the equivalent of a complex type that we define ourselves.
Int... Double float bool Char string DateTime array type
Most of the objects in life are complex objects.
How do I define a struct type?
In general, the definition of the structure should be placed outside the class or inside the class, try not to put in the inside of main.
struct Custom type name
{
Public variable type variable name;
......;
......;
......;
}
For example:
struct Yuangong//custom data type. Information used to describe an employee.
{
public string NO;
public string Name;
public int age;
public string Nation;
public bool Sex;
}
How do I define a variable with a custom type?
Custom type name variable = new custom type name ();
How do I use a variable of a custom type?
Variable. Sub variable = "xxxx";
Console.WriteLine (variable name. sub-variable);
For example:
Define a variable of a custom type
Yuangong Zhangsan = new Yuangong ();
Assigning values to variables
zhangsan.no = "Y001";
Zhangsan. Name = "Zhang San";
Zhangsan. age = 22;
Zhangsan. Sex = true;
Zhangsan. Nation = "Han";
To value a variable
Console.WriteLine (zhangsan.no+ "\ t" +zhangsan. Name+ "\ t" +zhangsan. Age);
Console.WriteLine (Zhangsan. nation+ "\ T" + (Zhangsan. "Sex?" Male ":" female "));
********************************************************************************************************
1> structures are particularly similar to classes, where fields, methods, properties, and constructors can be defined in a structure. (Because the structure is passed down in C, and C is process-oriented)
The 2> struct also needs to create objects to invoke members in the struct, unlike classes where structs can be created by the new keyword or created by the new keyword, declaring a variable of a struct type.
This variable is an object (the structure is just very similar to the class, not the same, or there is a syntactic difference). Called by the object name Point member.
3> declaration Syntax:
[access modifier] struct struct name
{
Structural members;
}
4> the difference between a struct and a class:
A. Class is a class declaration struct is a struct declaration
B. Although you can declare a field in a struct, you cannot assign a value to a field while declaring a field. otherwise error.
C. Although constructors can be defined in a struct, you cannot write 1 parameterless constructors. Because in any case the compiler generates 1 parameterless constructors for a struct.
D. In the constructor of a struct, you must assign a value to each field of the struct object.
E. When creating a struct object. You can use the New keyword to create it. But I can also not use the New keyword. Use the new keyword to create an object: All the fields in the struct already have values. can be used directly. Create an object without using the new key:
A field in a struct has no value, so you must assign a value to the field before using the value of the field. The reason for this is that the constructor is called with the New keyword, and the constructor of the struct requires that all fields be assigned values, even if it generates no
constructor of the parameter (assigning default values to all fields). So creating a struct object with the New keyword all fields of the struct object already have values. If you declare 1 struct variables directly without using the New keyword, the constructor is not called,
So there is no value for the field.
F. Assigning a value to a property in a constructor, the compiler does not think it is assigning a value to a field, because the property is not necessarily in the action field.
G. struct is a 1 value type. The class is 1 reference types.
A 5> struct is a value type. When you create a struct object, the struct object is stored directly in the variable. When a struct variable is assigned to each other, the value of the field is copied individually.
********************************************************************************************************
Battle Games
1 usingSystem;2 usingSystem.Collections.Generic;3 usingSystem.Linq;4 usingSystem.Text;5 6 namespaceConsoleApplication17 {8 structRen9 {Ten Public stringName; One Public intBlood; A Public intAttack; - Public intdefend; - the - } - class Program - { + Static voidMain (string[] args) - { +Ren R1 =NewRen (); ARen r2 =NewRen (); atConsole.Write ("Please enter the first expert:"); -R1. Name =console.readline (); -Console.Write ("Please enter the second master:"); -R2. Name =console.readline (); - //Generate Blood Volume -Random Rand =NewRandom (); inR1. Blood = Rand. Next ( +) + +; -R2. Blood = Rand. Next ( +) + +; to //Offensive +R1. Attack = Rand. Next ( -) + -; -R2. Attack = Rand. Next ( -) + -; theR1. defend = rand. Next ( -) + -; *R2. defend = rand. Next ( -) + -; $Console.WriteLine ("Heroes"+ R1. Name +"\ t, Vitality"+R1. Blood);Panax NotoginsengConsole.WriteLine ("Heroes"+ R2. Name +"\ t, Vitality"+R2. Blood); -Console.WriteLine ("in a month of black and high night!"); the while(true) + { A //Jump out of the loop the if(R1. Blood <=0&& R2. Blood <=0) + { -Console.WriteLine (R1. Name +"with the"+ R2. Name +"all the best to go to the afterlife"); $ Break; $ } - if(R1. Blood <=0) - { theConsole.WriteLine (R2. Name +"Took a monkey to salvage the moon, and ended"+ R1. Name +"of Life"); - Break;Wuyi } the if(R2. Blood <=0) - { WuConsole.WriteLine (R1. Name +"with a black tiger taoxin Potter, it's over."+ R2. Name +"of Life"); - Break; About } $ //PvP - intB1 = rand. Next ( $);//the blood that R1 out. -R1. Blood-=B1; - if(R1. Blood <0) A { +R1. Blood =0; the } -System.Threading.Thread.Sleep ( +); $Console.foregroundcolor =consolecolor.red; theConsole.WriteLine (R2. Name +"a fist hit."+ R1. Name + B1 +"blood."); the Console.resetcolor (); the Console.WriteLine (); the //Hold on a second . -System.Threading.Thread.Sleep ( the); in intb2 = Rand. Next ( $);//the blood that R2 out. the the AboutR2. Blood-=B2; the if(R2. Blood <0) the { theR2. Blood =0; + } -Console.foregroundcolor =Consolecolor.darkcyan; theConsole.WriteLine (R1. Name +"a fist hit."+ R2. Name + b2 +"blood.");Bayi Console.resetcolor (); the the -Console.foregroundcolor =Consolecolor.yellow; -Console.WriteLine ("Heroes"+ R1. Name +"also have"+ R1. Blood +"Vitality!"); theConsole.Write ("Heroes"+ R2. Name +"also have"+ R2. Blood +"Vitality!"); the Console.resetcolor (); the Console.WriteLine (); the Console.WriteLine (); - the } the the }94 } the}View Code
C # organizing 8--structures