C# STRUCTURES
In this tutorial, you will learn how to use structure in c#, comparison between c# struct vs class, c# structure example. In this tutorial, you will learn how to use structure in c#, comparison between c# struct vs class, c# structure example.
- Structure is a group of elements having different data types.
- Unlike java, c# supports structures
- In c#, structure is similar to class. Because both class and structures are user defined type
- In c#, it can be created using the reserved word struct
- Like class, it contains variables (instance and static variables) and methods (both instance methods and static methods)
- It also includes constants, methods, properties, indexers and Interfaces, events
- The structure’s members can’t be specified as abstract, sealed, protected, virtual.
ACCESS MODIFIERS IN STRUCTURES
- Like class, it supports only two modifiers like public and internal
- If no modifier is specified, then the internal will be considered the default access modifier
- Like class, the members of the structure are private by default, if no modifier is specified
- It is important to note that protected modifier won’t be allowed for members of the structure.
DIFFERENCE BETWEEN CLASS AND STRUCTURE
S.N | CLASS | STRUCTURE |
1. | It is a reference type in c#. So it will be stored on heap memory | It is a value type in c#. So it will be stored on stack memory |
2. | It creates an object (instance) using new keyword | It creates an instance (object) with or without using new keyword (new keyword is an optional) |
3. | It is created using the reserved word class | It is created using the reserved word struct |
4. | It supports destructors | It does not support destructors |
5. | It supports protected modifier to its members | It does not support protected modifier to its members |
I. EXAMPLE OF STRUCTURES
SOURCE CODE
using y = System.Console;
namespace Structures
{
struct employee
{
string ename;
int id;
double salary;
// instance method
void disp()
{
y.WriteLine(“Emp.Name \t: ” + ename);
y.WriteLine(“Emp.Id \t\t: ” + id);
y.WriteLine(“Emp.Salary \t: ” + salary);
}
// main method
static void Main()
{
y.WriteLine(“—————————-“);
y.WriteLine(“\t C# Structures”);
y.WriteLine(“—————————-“);
employee emp = new employee();
emp.ename = “Sachin”;
emp.id = 99;
emp.salary = 95900.50;
emp.disp();
y.WriteLine(“—————————-“);
}
}
}
OUTPUT
POINTS ABOUT STRUCTURES
- It is a value type in c# and stored on stack memory
- Like class, it supports variables and methods
- Like class, it supports parameterized constructors
- Like class, it is internal by default (if no modifier is specified)
- Like class, its members are private by default (if no modifier is used)
- Like class, it includes main method
- Like class, it supports interfaces
- Its members can’t be specified as virtual, sealed, abstract, protected.
OTHER TUTORIALS
-
How to convert Word to Number in Bash Script
FacebookTweetPinLinkedInEmailShares0SharePrint In this bash tutorial, you will get convert word to number in bash script with examples.This is implemented using for loop, tr command, switch case in shell…
-
How to Count Word Frequency in Bash Script
FacebookTweetPinLinkedInEmailShares0SharePrint In this bash tutorial, you will get the program about how to count word frequency in bash script. This is done by using for loop,…
-
Bash Script – How to convert Number to Words
FacebookTweetPinLinkedInEmailShares0SharePrint In this tutorial, you will see how to convert numbers to words using bash script. This shell script can be done with the help of switch case…