Structure in C#

Walden Systems Geeks corner Structure in C# Programming tutorial Rutherford NJ New Jersey NYC New York North Bergen County
C# is an elegant and type-safe object-oriented language that enables developers to build a variety of secure and robust applications that run on the .NET Framework. You can use C# to create Windows client applications, XML Web services, distributed components, client-server applications, database applications, and much, much more. Visual C# provides an advanced code editor, convenient user interface designers, integrated debugger, and many other tools to make it easier to develop applications based on the C# language and the .NET Framework.

Structure is a value type and a collection of variables of different data types under a entity. It is almost similar to a class because both are user-defined data types and both hold a bunch of different data types. C# provide the ability to use pre-defined data types. Sometimes we may need to define its own data type which is also known as User-Defined Data Type. Although it comes under the value type, the user can modify it according to requirements and that’s why it is also termed as the user-defined data type.

Defining

To define a structure, we use the struct statement. The struct statement defines a new data type, with more than one member for your program. A structure can also contain constructors, constants, fields, methods, properties, indexers and events. A struct object can be created with or without the new operator, same as primitive type variables. When we create a struct object using the new operator, an appropriate constructor is called. If a struct object is created without the new operator, the members in the struct must be initialized before being accessed. If the new operator is called, the members are initialized to their default values.

struct Student
{
    public int Age;
    public string FirstName;
    public string LastName;
    public int Level;
}


Features

Structures can have methods, fields, indexers, properties, operator methods, and events. Structures can have defined constructors, but not destructors. W can't define a default constructor for a structure. The default constructor is automatically defined and can't be changed or overridden. 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 can't be specified as abstract, virtual, or protected. 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.

Difference between class and structure

As stated earlier, a structure is similar to a class. There are major differences. Structs are value types whereas classes are reference types. What that means is that structs hold their values in memory while classes holds a reference to an object. If we copy a struct, C# creates a new copy of the object and is given to the new struct instance. If we copy class, a new copy of the reference to the object and assigns a copy of the reference to a separate instance of the class. Unlike classes, structs don't have destructors. Structs can have interface inheritance but can't have implementation inheritance.