108x Filetype PDF File size 0.41 MB Source: uomustansiriyah.edu.iq
C# Programming L3- Structures in C# Dr. Asmaa S., Dr.Bassim J., & Lectrurer Nada Th. Structures in C# In C#, a structure is a value type data type. It helps you to make a single variable hold related data of various data types. The struct keyword is used for creating a structure. Structures are used to represent a record. Suppose you want to keep track of your books in a library. You might want to track the following attributes about each book: - (Title, Author, Subject, and Book ID). Defining a Structure The keyword struct can be used to declare a structure. The general form of a structure declaration in C# is as follows:struct { //Structure members } Where: the modifier can be private, public, internal or public. The struct is the required keyword. For example 1. struct MyStruct { 2. public int x; 3. public int y; 4. } The objects of a strcut can be created by using the new operator as follows. 1. MyStruct ms = new MyStruct(); The individual members of a struct can be accessed by using the dot (.) operator as showing below. 1. ms.x = 10; 2. ms.y = 20; The strcut object can also be created without using the new operator. MyStruct ms; 46 C# Programming L3- Structures in C# Dr. Asmaa S., Dr.Bassim J., & Lectrurer Nada Th. But in this case all fields of the struct will remain unassigned and the object can't be used until all of the fields are initialized. Remember that inside a struct, we can only declare a field. We can't initialize a field inside a struct. Example1: 1. using System; 2. struct MyStruct { 3. public int x; 4. public int y; 5. } 6. class MyClient { 7. public static void Main() { 8. MyStruct ms = new MyStruct(); 9. ms.x = 10; 10. ms.y = 20; 11. int sum = ms.x + ms.y; 12. Console.WriteLine("The sum is {0}", sum); 13. } 14. } Example2: here is the way you can declare the Book structure: struct Books { public string title; public string author; public string subject; public int book_id; }; 46 C# Programming L3- Structures in C# Dr. Asmaa S., Dr.Bassim J., & Lectrurer Nada Th. The following program shows the use of the structure:- using System; struct Books { public string title; public string author; public string subject; public int book_id; }; public class testStructure { public static void Main(string[] args) { Books Book1; /* Declare Book1 of type Book */ Books Book2; /* Declare Book2 of type Book */ /* book 1 specification */ Book1.title = "C Programming"; Book1.author = "Nuha Ali"; Book1.subject = "C Programming Tutorial"; Book1.book_id = 6495407; /* book 2 specification */ Book2.title = "Telecom Billing"; Book2.author = "Zara Ali"; Book2.subject = "Telecom Billing Tutorial"; Book2.book_id = 6495700; /* print Book1 info */ Console.WriteLine( "Book 1 title : {0}", Book1.title); Console.WriteLine("Book 1 author : {0}", Book1.author); 44 C# Programming L3- Structures in C# Dr. Asmaa S., Dr.Bassim J., & Lectrurer Nada Th. Console.WriteLine("Book 1 subject : {0}", Book1.subject); Console.WriteLine("Book 1 book_id :{0}", Book1.book_id); /* print Book2 info */ Console.WriteLine("Book 2 title : {0}", Book2.title); Console.WriteLine("Book 2 author : {0}", Book2.author); Console.WriteLine("Book 2 subject : {0}", Book2.subject); Console.WriteLine("Book 2 book_id : {0}", Book2.book_id); Console.ReadKey(); } } The output of the above code is: Book 1 title : C Programming Book 1 author : Nuha Ali Book 1 subject : C Programming Tutorial Book 1 book_id : 6495407 Book 2 title : Telecom Billing Book 2 author : Zara Ali Book 2 subject : Telecom Billing Tutorial Book 2 book_id : 6495700 Example3: // C# program to illustrate copy the structure using System; namespace ConsoleApplication { // Defining structure public struct Person 46
no reviews yet
Please Login to review.