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 . Child Variables = "XXXX"; Console.WriteLine ( variable name . Child Variables );
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 "));
Example 1:
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;
Public Lianxifangshi Lianxi;
}
struct Lianxifangshi
{
public string QQ;
public string Weixin;
public string Email;
public string Telephone;
public string Address;
public string ZipCode;
}
Class Program
{
static void mai0n (string[] args)
{
Yuangong Zhangsan = new Yuangong ();
zhangsan.no = "Y001";
Zhangsan. Name = "Zhang San";
Zhangsan. age = 22;
Zhangsan. Sex = true;
Zhangsan. Nation = "Han";
Zhangsan. LIANXI.QQ = "434354546";
Zhangsan. Lianxi.weixin = "Zhang 33";
Zhangsan. Lianxi.email = "[Email protected]";
Zhangsan. lianxi.address = "Zhang Jia Hutong, Zhangdian District";
Zhangsan. Lianxi.zipcode = "25000";
Zhangsan. Lianxi.telephone = "";
Yuangong lisi = new Yuangong ();
lisi.no = "Y002";
Lisi. Name = "John Doe";
Lisi. Age = 25;
Lisi. Sex =false;
Lisi. Nation = "Hui";
Console.WriteLine ("********** Zhang San personal Information ***********");
Console.WriteLine (zhangsan.no+ "\ t" +zhangsan. Name+ "\ t" +zhangsan. Age);
Console.WriteLine (Zhangsan. nation+ "\ T" + (Zhangsan. "Sex?" Male ":" female "));
Console.WriteLine ("Contact information:");
Console.WriteLine (
"QQ:" + (Zhangsan. Lianxi.qq==null? " None ": Zhangsan. LIANXI.QQ) + "\ T"
+ ":" + (Zhangsan. Lianxi.weixin = = null? " None ": Zhangsan. lianxi.weixin) + "\ T"
+ "Mobile:" + (Zhangsan. Lianxi.telephone==null? " None ": Zhangsan. Lianxi.telephone) + "\ T"
+ "Mailbox:" + (Zhangsan. Lianxi.email==null? " None ": Zhangsan. Lianxi.email) + "\ T"
+ "Address:" +zhangsan. lianxi.address+ "\ t" +zhangsan. Lianxi.zipcode);
}
}
Example 2:
struct Xuesheng
{
public int Xuehao;
public string xingming;
public double Yuwen;
public double Shuxue;
public double Waiyu;
public double Zongfen;
public int Minci;
}
Class Class1
{
static void mai1n (string[] args)
{
Int[] A = new int[5];
Xuesheng[] s = new xuesheng[5];
Input
Shuru (s);
Sort
Paixu (s);
Fill in the rankings
XIEMINGCI (s);
Output display
Shuchu (s);
}
private static void Shuchu (xuesheng[] s)
{
for (int i = 0; i < s.length; i++)
{
Console.WriteLine (S[i]. Xuehao + "\ T" + s[i]. Xingming + "\ T" + s[i]. Yuwen + "\ T" + s[i]. Shuxue + "\ T"
+ S[i]. Waiyu + "\ T" + s[i]. Zongfen + "\ T" + s[i]. MINCI);
}
}
private static void Xiemingci (xuesheng[] s)
{
for (int i = 0; i < s.length; i++)
{
S[i]. Minci = i + 1;
}
}
private static void Paixu (xuesheng[] s)
{
for (int i = 1; I <= s.length-1; i++)
{
for (int j = 1; J <= S.length-i; j + +)
{
if (S[j]. Zongfen > S[j-1]. Zongfen)
{
Xuesheng temp = s[j];
S[J] = s[j-1];
S[J-1] = temp;
}
}
}
}
private static void Shuru (xuesheng[] s)
{
for (int i = 0; i < s.length; i++)
{
Console.WriteLine ("Entering section" + (i + 1) + "Information of students");
S[i]. Xuehao = i + 1;
Console.Write ("Name:");
S[i]. xingming = Console.ReadLine ();
Console.Write ("Language:");
S[i]. Yuwen = Convert.todouble (Console.ReadLine ());
Console.Write ("Mathematics:");
S[i]. Shuxue = Convert.todouble (Console.ReadLine ());
Console.Write ("Foreign Language:");
S[i]. Waiyu = Convert.todouble (Console.ReadLine ());
Score:
S[i]. Zongfen = S[i]. Yuwen + s[i]. Shuxue + s[i]. Waiyu;
}
}
}
C#9 Structure Employee Information student performance