xxxxxxxxxx
Structs are similar to classes in that they represent data
structures that can contain data members and function members.
However, unlike classes, structs are value types and do not
require heap allocation. A variable of a struct type directly
contains the data of the struct, whereas a variable of a
class type contains a reference to the data, the latter known as an object.
Note: Structs are particularly useful for small data structures that
have value semantics. Complex numbers, points in a coordinate system,
or key-value pairs in a dictionary are all good examples of structs.
y to these data structures is that they have few data members,
that they do not require use of inheritance or reference semantics,
rather they can be conveniently implemented using value semantics
where assignment copies the value instead of the reference. end note
xxxxxxxxxx
// C program to implement
// the above approach
#include <stdio.h>
#include <string.h>
// Declaration of the
// dependent structure
struct Employee
{
int employee_id;
char name[20];
int salary;
};
// Declaration of the
// Outer structure
struct Organisation
{
char organisation_name[20];
char org_number[20];
// Dependent structure is used
// as a member inside the main
// structure for implementing
// nested structure
struct Employee emp;
};
// Driver code
int main()
{
// Structure variable
struct Organisation org;
// Print the size of organisation
// structure
printf("The size of structure organisation : %ld\n",
sizeof(org));
org.emp.employee_id = 101;
strcpy(org.emp.name, "Robert");
org.emp.salary = 400000;
strcpy(org.organisation_name,
"GeeksforGeeks");
strcpy(org.org_number, "GFG123768");
// Printing the details
printf("Organisation Name : %s\n",
org.organisation_name);
printf("Organisation Number : %s\n",
org.org_number);
printf("Employee id : %d\n",
org.emp.employee_id);
printf("Employee name : %s\n",
org.emp.name);
printf("Employee Salary : %d\n",
org.emp.salary);
}
xxxxxxxxxx
public struct S : IDisposable
{
private bool dispose;
public void Dispose()
{
dispose = true;
}
public bool GetDispose()
{
return dispose;
}
}
class Program
{
static void Main(string[] args)
{
var s = new S();
using (s)
{
Console.WriteLine(s.GetDispose());
}
Console.WriteLine(s.GetDispose());
}
/*result
False
False
*/
}