Structures. General concepts. Type of structure. Structural variable. Modifiers of access to a structured variable. Examples of using structures

Structures. General concepts. Type of structure. Structural variable. Modifiers of access to a structured variable. Examples of using structures


Contents


1. What is a structure in C#? The purpose of structures use

Often, when writing programs that contain a variety of data, it becomes necessary to group these data according to a certain criterion. This grouping is necessary to improve the visibility of the program code, which in turn leads to a reduction in errors and increased productivity of the programmer.

Access to grouped data simplifies the use of names in the program. Data are grouped according to the problem area for which the program is developed.

In the programming language C#, for the purpose of convenient grouping of data, so-called structures are used.

Using the structure in the program occurs in 2 stages:

  • declaration of the type of structure;
  • declaration of a structural variable.

 

2. What is the general form of the structure declaration (structural type)? The struct keyword

General form of declaring the structure type:

struct structure_type_name : interfaces
{
    // The declaration of members and methods of structure
    // ...
}

where

  • structure_type_name – The name of the structural type on the basis of which objects (variables, instances of the structure) will be declared;
  • interfaces – a list of interfaces whose methods need to be implemented in the body of the structure.

 

3. An example of a structure declaration that describes the information about student

Let it be necessary to create a database of students of an educational institution. Data about students in the database can be represented by structures. In the simplest case, each structure should combine the following data:

  • surname of the student;
  • name of student;
  • year of entering the institution;
  • year of birth;
  • address;
  • number of the record book;
  • current student rating.

The structure, that describes the information will have approximately the following form:

struct Student
{
    public string name; // surname
    public string surname; // name
    public int year; // year of entering
    public int birth_year; // year of birth
    public string address; // address
    public string book_number;   // number of the record book
    public float rank; // rating
}

 

4. What is a structured variable? How is memory allocated for a structured variable? Example of declaring, initializing, and using a structured variable

Suppose given a declaration of structure variable that describes one entry in the phone book:

struct Telephone
{
    public string number; // phone number
    public string name; // subscriber name
    public string surname; // last name of the subscriber
    public string address; // address
    public int code; // Postal code
}

The declaration and use of a structural variable of type Telephone will be as follows:

// Declaring a structural variable named T1 of type Telephone
Telephone T1;

// filling of fields of the structural variable T1
T1.name = "Johnson";
T1.surname = "John";
T1.number = "77777777";
T1.code = 89300;
T1.address = "Boston";

 

5. What type of access do the fields of a structured variable have by default? Examples

By default, the fields of the structure variable have a ‘private’ access type. To be able to access the fields of a structured variable directly, the public keyword is used.

Example 1. Let the structure declaration that describes the student information is given:

struct Student
{
    string name; // surname
    string surname; // name
    int year; // year of entering the institution
    int birth_year; // year of birth
    string address; // address
    string book_number;   // number of the record book
    float rank; // rating
}

In this structure, all members do not have an explicitly specified access modifier (private, public). By default, the private access modifier is set. This means that when you declare a variable of type Student, you will not be able to access the structure fields directly. That is, the following code will be wrong:

Student St;
St.name = "Name of student"; // error!
St.year = 1990; // error!

Example 2. Let it be given a declaration of the type of the structure containing information about the date (day, month, year):

struct Date
{
    public int day; // date
    public int month; // month
    public int year; // year
}

Then, the use of the structure in some method can be as follows:

// use of a structural variable in the code of some method
Date D; // declaration of a variable with the name D of type "Date structure"

// filling the fields of the structure variable
// fields are declared as public
D.day = 14;
D.month = 1;
D.year = 1972;

 

6. In which parts of the program code can you declare the structure and structural variable for Windows Forms applications?

For applications created using the Windows Forms Application template, you can declare the structure type:

  • within the namespace outside the class;
  • within the class but outside of class methods.

In the class method, you can not declare the type of the structure. A structured variable can be declared in a class both in class methods and outside class methods.

 

7. What is the difference between structure and class in terms of their representation in memory?

The structures are value types. This means that memory for variables of the structure type is allocated immediately when they are declared. Value types include basic data types such as short, int, uint, double, float, bool, string, and others. Access to the structure fields is performed directly. It is not necessary (but possible) to allocate memory for a structured variable using the new operator.

Classes are reference types. This means that for a class object (a variable of type “class”), you must allocate memory using the new operator.

 

8. What are the differences between structures and classes?

When using structures and classes, the following main differences can be distinguished:

  • structures belong to value types, classes belong to reference types;
  • when assigning structures, a complete copy of its object is created, that is, memory for an additional structure is allocated. When assigning classes, a reference to one class is assigned to a reference to another class, and as a result, both references point to the same object;
  • structures do not support inheritance. A structure can not inherit a class or other structure. However, the structure can contain a variable of the structural type;
  • unlike classes, you can not declare (implement) the default constructor in structures;
  • unlike classes, in the structures, data members can not be declared with the keyword protected, because structures do not support inheritance;
  • unlike classes, structures do not support destructors;
  • structures can not be declared with the keywords abstract and virtual.

 

9. In which cases is it more appropriate to use structures than classes?

Although structures have less potential than classes, in some cases the use of structures is more efficient. To such cases it is possible to carry cases:

  • when it is necessary to save a group of data (group data) that do not require inheritance and access by reference;
  • when you need to improve performance (access speed) and save memory that is allocated for data that does not require inheritance. This is due to the fact that the structures belong to the type-values, and, therefore, they can be operated directly;
  • if you want to process data groups in an array for which the number of items is fixed and relatively small.

Structures are more convenient for grouping small amounts of data.

 

10. What elements of the programming language can be implemented in the structure?

In the structure you can implement:

  • fields (members of the structure);
  • methods;
  • interfaces;
  • indexers;
  • properties;
  • operator methods;
  • events;
  • constructors (except for the default constructor).

 

11. Is it possible to create a structure of an object by using a new operator?

Yes it is. In this case, after the new keyword, the default constructor is called.

For example. Using the new operator to create an instance of a Date structure.

// Structure that describes the date
struct Date
{
    public int day;
    public int month;
    public int year;
}

Demonstration the use of structure of Date type:

// creating an instance of a structure using the new operator
Date D = new Date(); // the default constructor is called

// fill the structure fields by data
D.day = 14;
D.month = 01;
D.year = 1972;

 

12. Do structures support inheritance?

Unlike classes, the structures do not support inheritance.

 

13. Which one base class inherits all the structures in C#?

All structures inherit the object class. Other classes or structures can not be inherited by structures.

 

14. Can the fields (members) of the structure be declared with the keyword protected?

They can not, because the structures do not support inheritance.

 

15. Can fields (members) of a structure be declared with the ‘abstract’ and ‘virtual’ keywords?

They can not, because the structures do not support inheritance.

 


Related topics