C # struct BODY

Source: Internet
Author: User

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

Practice:

1.

1 usingSystem;2 usingSystem.Collections.Generic;3 usingSystem.Linq;4 usingSystem.Text;5 6 namespaceConsoleApplication17 {8     structYuangong//a custom data type that describes the information for an employee. 9     {Ten          Public stringNO; One          Public stringName; A          Public intAge ; -          Public stringNation; -          Public BOOLsex; the     } -  -     class Program -     { +         Static voidMain (string[] args) -         { +             //define a variable of a custom type AYuangong Zhangsan =NewYuangong (); at             //assigning values to variables -zhangsan.no ="Y001"; -Zhangsan. Name ="Zhang San"; -Zhangsan. Age = A; -Zhangsan.sex =true; -Zhangsan. Nation ="Han"; in  -Yuangong Lisi =NewYuangong (); tolisi.no ="Y002"; +Lisi. Name ="John Doe"; -Lisi. Age = -; theLisi.sex =false; *Lisi. Nation ="Hui"; $ Panax Notoginseng             //to value a variable -Console.WriteLine ("******** Zhang San's personal information ********"); theConsole.WriteLine (zhangsan.no+"\ t"+zhangsan. name+"\ t"+Zhangsan. Nation); +Console.WriteLine (Zhangsan. Age+"\ t"+ (Zhangsan.sex?)"male":"female")); A         } the     } +}

2.

1 usingSystem;2 usingSystem.Collections.Generic;3 usingSystem.Linq;4 usingSystem.Text;5 6 namespaceConsoleApplication17 {8     structXuesheng9     {Ten          Public intXuehao; One          Public stringxingming; A          Public DoubleYuwen; -          Public DoubleShuxue; -          Public DoubleWaiyu; the          Public DoubleZongfen; -          Public intMINGCI; -     } -     classClass1 +     { -  +         Static voidMain (string[] args) A         { atXuesheng[] A =Newxuesheng[5]; -             //input -              for(inti =0; I < a.length;i++ ) -             { -Console.WriteLine ("is entering section"+ (i+1)+"A student's information"); -A[i].xuehao = i +1; inConsole.Write ("Name:"); -A[i].xingming =console.readline (); toConsole.Write ("Language:"); +A[i].yuwen =convert.todouble (Console.ReadLine ()); -Console.Write ("number of text:"); theA[i].shuxue =convert.todouble (Console.ReadLine ()); *Console.Write ("Foreign Languages:"); $A[i].waiyu =convert.todouble (Console.ReadLine ());Panax Notoginseng                  -A[i].zongfen = A[i].yuwen + A[i].shuxue +A[i].waiyu; theConsole.WriteLine ("Total Score:"+A[i].zongfen); +             } A             //Sort the              for(inti =1; I <= a.length-1; i++ ) +             { -                  for(intj =1; J <= A.length-i; J + +) $                 { $                     if(A[j].zongfen > A[j-1].zongfen) -                     { -Xuesheng temp =A[j]; theA[J] = a[j-1]; -A[j-1] =temp;Wuyi                     } the                 } -             } Wu             //Write a ranking -              for(intI=0; i<a.length;i++) About             { $A[I].MINGCI = i +1; -             } -             //Output -              for(inti =0; i < a.length; i++) A             { +Console.WriteLine (a[i].xuehao+"\ t"+a[i].xingming+"\ t"+a[i].yuwen+"\ t"+a[i].shuxue+"\ t"+a[i].waiyu+"\ t"+a[i].zongfen+"\ t"+A[I].MINGCI); the             } -         } $  the     } the}

C # struct BODY

Related Article

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.