C#. 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


Search other websites:

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. 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?

Structures refer to value types. This means that they are implicitly inherited from the System.ValueType class. Value types (structural types) include basic (primitive) data types, such as short, int, uint, double, float, bool and others, as well as their aliases System.Int32, System.Double, System.Single and others.

Classes are reference types.

In terms of placement in memory, the following difference exists between structures and classes.

If a variable (instance) of a structural type is declared, then this instance is completely placed on the stack. The lifetime of a structure is determined by the context in which it is defined. As soon as a variable of a structural type goes beyond the context in which it is defined, it is immediately deleted from memory.

If a variable (reference) of the class type is declared, then only the reference to the class instance is placed on the stack. The instance itself is located in a memory area called a heap. To create an instance of a class, you must use the new operator. Removing a class instance from memory is the responsibility of the garbage collector.

 

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 you need to group small amounts of data. In this case, structures are used to optimize productivity;
  • when you need to increase productivity. Structures are stored on the stack, so memory allocation/deallocation is faster;
  • when you need to save memory for data. Since structures belong to value types, using structures uses less memory. In the case of classes, you need to use an additional reference variable.

Classes are more appropriate to use in cases:

  • when the functional of the structures is not enough to organize work with data;
  • when you need to process large amounts of information.

 

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 implicitly inherit the System.ValueType class, which is inherited from System.Object.

 

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