Structure 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.
A structure is a value type that can contain constructors, constants, fields, methods, properties, indexers, operators, events and nested types.
A structure in C# is simply a composite data type consisting of a number elements of other types. A C# structure is a value type and the instances or objects of a structure are created in stack. The structure in C# can contain fields, methods, constants, constructors, properties, indexers, operators and even other structure types.
The struct keyword is used for creating a structure. Structures are used to represent a record.
Example of Structure is:
public struct Discounts
{ public int Cloths { get; set; }
public int HomeDecor { get; set; }
public int Grocery { get; set; }
}

A struct can be initialized with or without the new keyword same as primitive variable or an object. You can then assign values to the members of the structure as shown below.

Discounts saleDiscounts = new Discounts();
saleDiscounts.Cloths = 20;
saleDiscounts.HomeDecor = 10;
saleDiscounts.Grocery = 5;
Features of C# Structures:
1. Structure can include constructors, constants, fields, methods, properties, indexers, operators, events & nested types.
2. Structure cannot include default constructor or destructor.
3. Structure can implement interfaces.
4. A structure cannot inherit another structure or class.
5. Structure members cannot be specified as abstract, virtual, or protected.
6. Structures must be initialized with new keyword in order to use it is properties, methods or events.

Class vs Structure
1. Class is reference type where as struct is value type.
2. Struct cannot declare a default constructor or destructor. However, it can have parametrized constructors.
3. Struct can be instasntiated without the new keyword. However, you won t be able to use any of its methods, events or properties if you do so.
4. Struct cannot be used as a base or cannot derive another struct or class.
5. structures do not support inheritance.

Read More

आइये जानते हैं Android 11 के बेस्ट 11 फीचर्स

आपके स्मार्टफोंस को और भी स्मार्ट बनाते हुए तथा मोबाइल तकनीक को और भी एडवांस करते हुए टेक दिग्गज़ Google ने अपना नया ऑपरेटिंग सिस्टम Android 11 दुनिया ...

StringBuilder in C#

A String is immutable, meaning String cannot be changed once created. For example, new string "Hello World!!" will occupy a memory space on the heap. ...

Properties in C#

In C#, properties are nothing but natural extension of data fields. They are usually known as smart fields in C# community. We know that data encaps ...

C# Hashtable

The Hashtable class represents a collection of key-and-value pairs that are organized based on the hash code of the key. It uses the key to access the ...

C# Queue

C# includes a Queue collection class in the System.Collection namespace. Queue stores the elements in FIFO style (First In First Out), exactly opposit ...

C# Stack

C# includes a special type of collection which stores elements in LIFO style(Last In First Out). C# includes a generic and non-generic Stack. Here, y ...

C# SortedList

The SortedList collection stores key-value pairs in the ascending order of key by default. SortedList class implements IDictionary & ICollection inter ...

C# ArrayList

ArrayList is a non-generic type of collection in C#. It can contain elements of any data types. It is similar to an array, except that it grows automa ...

Collection in C#

C# includes specialized classes that hold many values or objects in a specific series, that are called collection. Collection classes are specialized ...

C# File Stream I/O

A file is a collection of data stored in a disk with a specific name and a directory path. C# includes static File class to perform I/ ...

Exception Handling in C#

An exception is a problem that arises during the execution of a program. A C# exception is a response to an exceptional circumstance that arises while ...

Recent Posts






















Like us on Facebook