C # (8)--structure

Source: Internet
Author: User

struct: the equivalent of a complex type that we define ourselves.

Common simple types: int ... double float bool Char string

Common complex types: DateTime array types

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 "));

Example 1. Describe Employee information

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 Main (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"//If QQ is not entered, print "No" + ":" + (Zhangsan. Lianxi.weixin = = null? " None ": Zhangsan. lianxi.weixin) + "\ T" + "Phone:" + (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, two-person battle game

struct Ren {public string Name;            public int Blood;            public int Attack;            public int defend;                    public int Quick;            } struct Wugong {public string Name;        public int Attack;            } static void Main (string[] args) {wugong[] WG = new WUGONG[3]; Wg[0].            Name = "Evil Sword"; Wg[0].            Attack = 500; WG[1].            Name = "Dragon 18 Palm"; WG[1].            Attack = 600; WG[2].            Name = "dark ecstasy palm"; WG[2].                Attack = 400;            ren r1 = new Ren ();            ren r2 = new ren ();            Initializes the properties of two combatants.            Console.Write ("Please enter the name of the first Warrior:"); R1.            Name = Console.ReadLine ();            Console.Write ("Please enter the name of the second Warrior:"); R2.            Name = Console.ReadLine ();            Generate blood volume random Rand = new Random (); R1. Blood = Rand.            Next (1000) + 1000; R2. Blood = Rand.            Next (1000) + 1000;         Generate attack and defense   R1. Attack = Rand.            Next (50) + 50; R2. Attack = Rand.            Next (50) + 50; R1. defend = rand.            Next (50) + 50; R2. defend = rand.            Next (50) + 50; Generates SHENFA R1. Quick = Rand.            Next (100); R2. Quick = Rand.            Next (100); Console.WriteLine ("Name:" + R1. Name + "\ t Vitality:" + R1. Blood+ "\ t attack:" +R1. attack+ "\ t defensive force:" +r1. Defend+ "\ t agility:" +R1.            Quick);            Console.WriteLine ("VS"); Console.WriteLine ("Name:" + R2. Name + "\ t Vitality:" + R2. Blood + "\ t attack:" + R2. Attack + "\ t defensive force:" + R2. Defend + "\ t agility:" + R2.            Quick);            Console.WriteLine ("Press any key to start ...");            Console.readkey ();                while (true) {//jumps out of the loop. if (R1. Blood <=0 && R2. blood<=0) {Console.WriteLine (r1. Name+ "and" +R2. Name+ "Perish!"                    ");                Break } if (R1. blood<=0) {Console.WriteLine (R2. Name+ "put" +r1. Name+ "KO ~!      ");              Break } if (R2. Blood <= 0) {Console.WriteLine (r1. Name + "put" + R2. Name + "Ko ~!                    ");                Break }//PVP//Big strokes int dz1 = rand. Next (10); R2 's big strokes if (dz1 >= 8)//grand Strokes {wugong w1 = Wg[rand.                    Next (3)]; int B1 = rand. Next (100); R1 lost the blood int gj1 = rand. Next (100)-50; In order to float the r1 attack int fy1 = rand. Next (100)-50;//for floating r2 Defense B1 = (B1 + (R2. Attack + GJ1+W1. Attack)-(R1. Defend + fy1)) < 0? 0: (B1 + (R2). Attack + GJ1+W1. Attack)-(R1.                    Defend + fy1)); R1.                    Blood-= B1; if (R1. Blood < 0) {R1.                    Blood = 0;                    }//Stay a little longer.                    System.Threading.Thread.Sleep (2000); ConsoLe.                    Foregroundcolor = consolecolor.red; Console.WriteLine (R2. Name + "Use big strokes" +w1. Name+ "Launch attack," + R1. Name + "lost vitality" + B1 + "point!"                    ");                    Console.resetcolor ();                                   Console.WriteLine (); } else//normal moves {int sf1 = rand.                    Next (80); if (R1. QUICK-SF1 >= 0) {Console.WriteLine (r1. Name + "escaped" + R2.                    Name + "attack"); } else {int b1 = rand. Next (100); R1 lost the blood int gj1 = rand. Next (100)-50; In order to float the r1 attack int fy1 = rand. Next (100)-50;//for floating r2 Defense B1 = (B1 + (R2. Attack + gj1)-(R1. Defend + fy1)) < 0? 0: (B1 + (R2). Attack + gj1)-(R1.                        Defend + fy1)); R1.                        Blood-= B1; if (R1.               Blood < 0) {             R1.                        Blood = 0;                        }//Stay a little longer.                        System.Threading.Thread.Sleep (2000);                        Console.foregroundcolor = consolecolor.red; Console.WriteLine (R2. Name + "Launch attack," + R1. Name + "lost vitality" + B1 + "point!"                        ");                        Console.resetcolor ();                    Console.WriteLine ();                }}//For a moment.                System.Threading.Thread.Sleep (2000); int dz2 = rand. Next (10); R2 's big strokes if (dz2 >= 8)//grand Strokes {wugong w1 = Wg[rand.                    Next (3)]; int b2 = rand. Next (100); R1 lost the blood int gj1 = rand. Next (100)-50; In order to float the r1 attack int fy1 = rand. Next (100)-50;//for floating r2 Defense b2 = (B2 + (r1. Attack + gj1 + W1. Attack)-(R2. Defend + fy1)) < 0? 0: (B2 + (R1). Attack + gj1 + W1. Attack)-(R2.                 Defend + fy1));   R2.                    Blood-= B2; if (R2. Blood < 0) {R2.                    Blood = 0;                    }//Stay a little longer.                    System.Threading.Thread.Sleep (2000);                    Console.foregroundcolor = consolecolor.red; Console.WriteLine (R1. Name + "Use big strokes" + W1. Name + "Launch attack," + R2. Name + "lost vitality" + b2 + "Point!"                    ");                    Console.resetcolor ();                Console.WriteLine (); } else {int sf2 = rand.                    Next (80); if (R2. QUICK-SF2 >= 0) {Console.WriteLine (R2. Name + "escaped" + R1.                    Name + "attack"); } else {int b2 = rand. Next (+);//r2 lost the blood int gj2 = rand. Next (100)-50; In order to float the r1 attack int fy2 = rand. Next (100)-50;//for floating r2 Defense b2 = (B2+ (R1. Attack + gj2)-(R2. Defend + fy2)) < 0? 0: (B2 + (R1). Attack + gj2)-(R2.                        Defend + fy2)); R2.                        Blood-= B2; if (R2. Blood < 0) {R2.                        Blood = 0;                        } Console.foregroundcolor = Consolecolor.blue; Console.WriteLine (R1. Name + "Launch attack," + R2. Name + "lost vitality" + b2 + "Point!"                        ");                        Console.resetcolor ();                    Console.WriteLine ();                }} console.foregroundcolor = Consolecolor.yellow; Console.Write ("Name:" + R1. Name + "Vitality:" + R1.                Blood + "\ t"); Console.Write ("Name:" + R2. Name + "Vitality:" + R2.                Blood);                Console.resetcolor ();                Console.WriteLine ();            Console.WriteLine (); }        }

C # (8)-struct

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.