Duhok Polytechnic University Amedi Technical Institute/ IT Dept. By Halkawt Rajab Hussain 2016-04-02
: Structure declaration and initialization. Access to fields of structure. Array of Structs. 11/10/2016 1
In C# and.net Framework there are two implementations of the concept of "class" from the object-oriented programming: classes and structures. Classes are defined through the keyword class. Structures are defined through the keyword struct. The main difference between a structure and a class is that: 1. Classes are reference types (references to some address in the heap which holds their members). 2. Structures (structs) are value types (they directly hold their members in the program execution stack). 11/10/2016 2
Structure declaration and initialization. To define a structure, you must use the struct statement. The struct statement defines a new data type, with more than one member for your program. For example, 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; ; 11/10/2016 3
Initialization: StudentInfo st = new StudentInfo() Id = 123, Name = "Ali", Phone = 4577678 ; StudentInfo st = new StudentInfo() st.id=123; st.name= Ali ; st.phone=4577678; Direct set values set values after create new object 11/10/2016 4
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); 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(); 11/10/2016 5
Features of C# Structures: Structures can have methods, fields, indexers, properties, operator methods, and events. Structures can have defined constructors, but not destructors. However, you cannot define a default constructor for a structure. The default constructor is automatically defined and cannot be changed. Unlike classes, structures cannot inherit other structures or classes. Structures cannot be used as a base for other structures or classes. A structure can implement one or more interfaces. Structure members cannot be specified as abstract, virtual, or protected. 11/10/2016 6
When you create a struct object using the New operator, it gets created and the appropriate constructor is called. Unlike classes, structs can be instantiated without using the New operator. If the New operator is not used, the fields remain unassigned and the object cannot be used until all the fields are initialized. HomeWork: Rewrite the previous example, creates two methods, getvalue to get values of book properties and display method to display book information. 11/10/2016 7
Example The following program, if the access specifier for struct properties n1,n2 and res are privates, how to set values to n1,n2 and get result of res? using system; namespace Exam class test struct Calc public int n1, n2, res; ; static void Main(string[] args) Clac c; c.n1=5; c.n2=10; c.res=c.n1+c.n2; Console.WriteLine(c.res); 11/10/2016 8
Defining (Creating a Custom struct Type) The following example demonstrates how to define a custom struct. In this case,the struct is a StudentInfo with Id, Name and Phone properties. Example Create custom structure for student called StudentInfo? struct StudentInfo private int m_id; public int Id get return m_id; set m_id = value; private string m_name; public string Name get return m_name; set m_name = value; private int m_phone; public int Phone get return m_phone; set m_phone = value; 11/10/2016 9
Array of structs: Structures Write a console application that receives the following information for a set of students: studentid, studentname, coursename, date-of-birth.. The application should also be able to display the information being entered.. Implement this using structs? struct student public int s_id; public String s_name, c_name, dob; class Program static void Main(string[] args) student[] arr = new student[4]; for(int i = 0; i < 4; i++) Console.WriteLine("Please enter StudentId, StudentName, CourseName, Date-Of-Birth"); arr[i].s_id = Int32.Parse(Console.ReadLine()); arr[i].s_name = Console.ReadLine(); arr[i].c_name = Console.ReadLine(); arr[i].s_dob = Console.ReadLine(); 11/10/2016 10
Example This example declares a struct with three members: a property, a method, and a private field. It creates an instance of the struct and puts it to use: using System; struct SimpleStruct private int xval; public int X get return xval; set if (value < 100) xval = value; public void DisplayX() Console.WriteLine("The stored value is: 0", xval); class TestClass public static void Main() SimpleStruct ss = new SimpleStruct(); ss.x = 5; ss.displayx(); 11/10/2016 11
Example Write complite program in C# console application that demonstrates struct initialization using both default and parameterized constructors. using system; Namespace exam public struct CoOrds public int x, y; public CoOrds(int p1, int p2) x = p1; y = p2; class TestCoOrds static void Main() CoOrds coords1 = new CoOrds(); CoOrds coords2 = new CoOrds(10, 10); // Display results: Console.Write("CoOrds 1: "); Console.WriteLine("x = 0, y = 1", coords1.x, coords1.y); Console.Write("CoOrds 2: "); Console.WriteLine("x = 0, y = 1", coords2.x, coords2.y); // Keep the console window open in debug mode. Console.WriteLine("Press any key to exit."); Console.ReadKey(); 11/10/2016 12
Thanks Any Questions 11/10/2016 13