C# struct also known as C# structure is a simple user-defined type, a lightweight alternative to a class. A stuct in C# is simply a composite data type consisting of a number elements of other types. This article and code examples implements C# Structs.
Similar to classes, structures have behaviors and attributes. As a value type, structures directly contain their value so their object or instance is stored on the stack.
C# Struts support access modifiers, constructors, indexers, methods, fields, nested types, operators, and properties.
How to define a struct in C#
- public struct Student {
- int id;
- int zipcode;
- double salary;
- }
Note: Struct members may not be declared protected.
Structs are simple to use and can prove to be useful at times. Just keep in mind that they're created on the stack and that you're not dealing with references to them but dealing directly with them. Whenever you have a need for a type that will be used often and is mostly just a piece of data, structs might be a good option.
How to use a struct in C#
- using System;
- namespace example_struct {
- class Program {
- public struct Student {
- int id;
- int zipcode;
- double salary;
-
- public Student(int id, int zipcode, double salary) {
- this.id = id;
- this.zipcode = zipcode;
- this.salary = salary;
- }
-
- public Student(int id, int zipcode) {
- this.id = id;
- this.zipcode = zipcode;
- this.salary = 3400.00;
- }
-
-
-
-
-
-
-
- public Student(Student x) {
- this.id = x.id;
- this.zipcode = x.zipcode;
- this.salary = x.salary;
- }
- public void DisplayValues() {
- Console.WriteLine("ID: " + this.id.ToString());
- Console.WriteLine("Zipcode : " + this.zipcode.ToString());
- Console.WriteLine("Salary : " + this.salary.ToString());
- }
- }
- static void Main(string[] args) {
- Student stu = new Student(12, 201301, 4560.00);
- Student stu1 = new Student(stu);
- stu.DisplayValues();
- Console.WriteLine("Copy constructor values");
- stu1.DisplayValues();
- Console.ReadLine();
- }
- }
- }
C# structs properties
-
A struct is used to improve the performance and clarity of code.
-
A struct uses fewer resources in memory than a class.
-
When we have small and frequent use of some work use structs over classes.
-
Performance can suffer when using structures in situations where reference types are expected due to boxing and unboxing.
-
You should pass structs to method as ref parameters in order to avoid the performance loss associated with copying data.
-
Structs reside on the stack, so we should keep them small.
-
Structs can't be inherited and we can say they are sealed.
-
Structure implicitly inherits from System.ValueType.
-
The default constructor of a structure initializes each field to a default value. You cannot replace the default constructor of a structure.
-
You can't define destructor for structs.
Practical program showing that struct and inherit from an interface
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace example_struct_using_interface {
- class Program {
- public interface aa {
-
- double Increment();
- void DisplayValues();
- }
- public struct Student: aa {
- int id;
- int zipcode;
- double salary;
- public Student(int id, int zipcode, double salary) {
- this.id = id;
- this.zipcode = zipcode;
- this.salary = salary;
- }
- public void DisplayValues() {
- Console.WriteLine("ID: " + this.id.ToString());
- Console.WriteLine("Zipcode : " + this.zipcode.ToString());
- Console.WriteLine("Salary : " + this.salary.ToString());
- }
- public double Increment() {
- return (this.salary += 1000.00);
- }
- }
- static void Main(string[] args) {
- Student stu = new Student(12, 201301, 4560.00);
- stu.DisplayValues();
- Console.WriteLine("Salary after increment is {0}", stu.Increment());
- Console.ReadLine();
- }
- }
- }
Structs and inheritance
Structs don't provide inheritance. It is not possible to inherit from a struct and a struct can't derive from any class. Similar to other types in .NET, struct is also derived from the class System.Object class and provides its functionality. Inheritance is indirect: Structs derive from System.ValueType, which in turns derive from System.Object.ValueType adds no new method of its own, but provides overrides of some the Object methods that are more appropriate to value types.
Difference between structs and classes
structs |
classes |
structs are value type |
classes are reference type |
structs are stored in stack or a inline |
classes are stored on managed heap |
structs doesn't support inheritance |
classes support inheritance |
But handing of constructor is different in structs. The complier supplies a default no-parameter constructor, which your are not permitted to replace |
Constructors are fully supported in classes |