Structures or structs are user-defined data types that can contain other variables as its
fields, and methods as its bahavior. You can create your very own customized data type using structures.Let say you want a data that don't just store a name as a string,
but you also want to include his age and his monthly salary.
To define a structure in a program, we use the following syntax.
struct StructName { member1; member2; member3; ... member4; }
We declare a structure using the struct
keword. Names of structures should used Pascal Casing to
indicate that it is a structure but that is just a convention
followed in C# programming. The members can either be variables
or methods. The program below shows an example of a structure.
using System; namespace StructuresDemo { public struct Employee { public string name; public int age; public decimal salary; } public class Program { public static void Main() { Employee employee1; Employee employee2; employee1.name = "Jack"; employee1.age = 21; employee1.salary = 1000; employee2.name = "Mark"; employee2.age = 23; employee2.salary = 800; Console.WriteLine("Employee 1 Details"); Console.WriteLine("Name: {0}", employee1.name); Console.WriteLine("Age: {0}", employee1.age); Console.WriteLine("Salary: {0:C}", employee1.salary); Console.WriteLine(); //Seperator Console.WriteLine("Employee 2 Details"); Console.WriteLine("Name: {0}", employee2.name); Console.WriteLine("Age: {0}", employee2.age); Console.WriteLine("Salary: {0:C}", employee2.salary); } } }
Output
Employee 1 Details Name: Jack Age: 21 Salary: $1000.00 Employee 2 Datails Name: Mike Age: 23 Salary: $800.00
Lets disect the code to better understand the program.
the
declaration of a structure
public struct Employee { public string name; public int age; public decimal salary; }
Note that we used the public keyword. That keyword simply indicates that our Employee
can be accessed or used anywhere in the program (or outside the program). public is one
of the access specifiers. We used the struct keyword followed by name of the structure.
Structure names also follow the Pascal Casing convention.
Inside the body of the structure, we defined three fields. Fields are like the properties of our Employee. For example,an employee has a name, an age and his job, he is given a monthly salary. These members was also declared as public so they can
be called outside the structure as demonstrated later.
declares two instances of Employee
Employee employee1; Employee employee2;
declares two instances of Employee. The declaration of structure instances is similar
to normal variables. You first indicate the structure type, and then the identifier.
assigned values to the respective fields of each employee.
employee1.name = "Jack"; employee1.age = 21; employee1.salary = 1000; employee2.name = "Mark"; employee2.age = 23; employee2.salary = 800;
assigned values to the respective fields of each employee. To access the fields outside the
structure, they must be public first .We first write the name of the variable, followed by a dot (.)
and then the name of the field.When the dot operators is used in this context, it is called the member access
operator as it allows you to call the members of a particular structure.
demonstrates how you can access the values stored on each field of a structure variable
Console.WriteLine("Employee 1 Details"); Console.WriteLine("Name: {0}", employee1.name); Console.WriteLine("Age: {0}", employee1.age); Console.WriteLine("Salary: {0:C}", employee1.salary); Console.WriteLine(); //Seperator Console.WriteLine("Employee 2 Details"); Console.WriteLine("Name: {0}", employee2.name); Console.WriteLine("Age: {0}", employee2.age); Console.WriteLine("Salary: {0:C}", employee2.salary);
demonstrates how you can access the values stored on each field of a structure variable. When accessing or reading
a field, we do the same thing. We indicate the variable name followed by a dot and then the name of the field to be read.
Structures are value types. That means that if we assign employee2 with a value of employee1,employee2 will
copy all the values of the attributes of employee1 instead of getting its reference. A class is similar to a
structure but it is a reference type.
You can also add methods inside a structure. The example below modifies the program in Example 1.
using System; namespace StructuresDemo2 { public struct Employee { public string name; public int age; public decimal salary; public void SayThanks() { Console.WriteLine("{0} thanked you!", name); } } public class Program { public static void Main() { Employee employee1; Employee employee2; employee1.name = "Jack"; employee1.age = 21; employee1.salary = 1000; employee2.name = "Mark"; employee2.age = 23; employee2.salary = 800; Console.WriteLine("Employee 1 Details"); Console.WriteLine("Name: {0}", employee1.name); Console.WriteLine("Age: {0}", employee1.age); Console.WriteLine("Salary: {0:C}", employee1.salary); employee1.SayThanks(); Console.WriteLine(); //Seperator Console.WriteLine("Employee 2 Details"); Console.WriteLine("Name: {0}", employee2.name); Console.WriteLine("Age: {0}", employee2.age); Console.WriteLine("Salary: {0:C}", employee2.salary); employee2.SayThanks(); } } }
Output
Employee 1 Details Name: Jack Age: 21 Salary: $1000.00 Jack thanked you! Employee 2 Details Name: Mike Age: 23 Salary: $800.00 Mike thanked you!
define a new method inside a structure
public void SayThanks() { Console.WriteLine("{0} thanked you!", name); }
This method prints a message and gets the value of the name field to show a unique
message for each instance. To call the method, we used similar syntax except that instead
of the field following the dot, we indicate the name of method to call followed by parentheses—and
a set of arguments if they are required by a function.
No comments:
Post a Comment