Structs and Arrays

Structs:

A struct is a simple user-defined type, a lightweight alternative to a class. A structure in C# is simply a composite data type consisting of a number elements of other types. 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. Struts support access modifiers, constructors, indexers, methods, fields, nested types, operators, and properties.

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. The struct keyword is used for creating a structure.

public struct Book  
{  
    public decimal price;  
    public string title;     
    public string author;
}    

Syntax wise, you have to replace the word class with struct.

Struct and class difference:

A struct is a value type. To help understand the struct, it’s helpful to make a comparison with classes. While a struct is a value type, a class is a reference type. Value types hold their value in memory where they are declared, but reference types hold a reference to an object in memory. If you copy a struct, C# creates a new copy of the object and assigns the copy of the object to a separate struct instance. However, if you copy a class, C# creates a new copy of the reference to the object and assigns the copy of the reference to the separate class instance. Structs can’t have destructors, but classes can have destructors.

Arrays:

Array is a data structure to store collection or group of variables of same type.

Ex:

Var name1;

Var name2;

Var name3;

Instead of specifying variables like this, you can specify in a array as:

int[] names = new var[4];

[] is used to indicate that this is an array, and [4] specifies how many objects you need in array.

Above example shows there are 4 objects in the array.

Arrays have fixed size during run time and it cannot be changed. When declaring array, you have to allocate memory for it. So, we have to use the new operator. Array is an object in csharp. When array is declared, compiler creates instance of the class, which is an object. So we have to use new operator and allocate memory.

First element of array starts with zero. We can access elements of array as [0], [1] etc. For above example it would be,

names[0] = “John”;

names[1]=“Dave”;

names[3]= “kumar”;

Or we can also declare as {John, Dave, kumar};

Ex: var words = new string[3] { "one", "two", "three" };

Now call them as words[0], words[1] etc.. This is the easiest way to declare array.

Note: Arrays are initiated to zero by default. Meaning, first value in array would be zero by default. Boolean arrays are initiated to false by default.

Ex:

using System;
 namespace MyTestApp
 {
     class Program
     {
         static void Main(string[] args)
         {
             int[] count = new int[5]; // we can also remove int[] in the begining and 
             //replace with var. compiler knows it because we are already assigning it to a integer on the right hand side.
             count[0] = 222;

             Console.WriteLine(count[0]);
             Console.WriteLine(count[1]);
             Console.WriteLine(count[2]);

             //string[] TorF = new string[10];
             var TorF = new bool[10];
             Console.WriteLine(TorF[0]);
             Console.WriteLine(TorF[1]);
             Console.WriteLine(TorF[2]);
             Console.WriteLine(TorF[3]);

         }
     }
 }

Last updated