• دا عبارة عن Value Type
  • It used to define custom data types.
  • Its values are saved in the stack.
// default access modifier inside Namespace : internal
// internal vs Public
struct TypeA
{
	int X;
	// Default access modifier: Private
	public void Print ()
	{
		Console.WriteLine(${X});
	}
}
 
public struct TypeB
{
	//code
}
  • لو روحت على project تاني وعايز أستخدم ال struct اللي انا عامله دا مش هيرضى اني اعمل using للـ namespace اصلا ؟ لازم الأول تضيفها في ال dependencies add project reference

  • ال datatype زي ال struct برضو ليها access modifier جوا ال namespace فلازم تحدد برضو هو public

// Point is a struct
Point p1; // P1 is Object for the point struct
// Allocation 8 Uninitialized bytes in Stack : Value type
Console.WriteLine(p1);//error: Garbage 
  • مش بيتعمل reference انما لما بنستخدمه بنعمل منه Object على عكس الـ Class كان بيعمل Reference
  • بمعنى إن الـ CLR هيخزن في الـ Stack المساحة بتاعته اللي محتاجها
  • طيب ليه بنستخدم الـ new معاها ودا عشان نحدد هنستخدم أنهي Constructor وتختاره
P1 = new Point()
// new is just only for constructor selection
// That will initialize the attributes of the object

Access Modifier

اتكلمنا عنها بالتفصيل في

Transclude of Cs-Access-Modifiers#inside-cs-class-class-or-cs-struct-struct

Constructor

  • اتكلمنا عنه بالتفصيل في Cs Constructor
  • اقدر اعرف multiple constructors
struct Point
{
    int X, Y;
 
    //User defined ctor
    public Point(int _X, int _Y)
    {
        X = _X;
        Y = _Y;
    }
 
	public Point(int N)
	{
		X = Y = N;
	}
 
	// Parameterless ctor
	// By default, we have this ctor
	public Point()
	{
		X = Y = 0;
	}
    // وارث من system.object
    public override string ToString() // overriding
    {
        return $"({X},{Y})";
    }
}
 
Point P2 = new Point(10, 15);
//P2 allocated in Stack
//use new keyword just for Ctor selection
Point P3 = new Point(15);
 

Array of struct

Define a struct “Person” with properties “Name” and “Age”. Create an array of three “Person” objects and populate it with data. Then, write a C# program to display the details of all the persons in the array

using System;
 
struct Person
{
    public string Name;
    public int Age;
 
    // Constructor to initialize a Person
    public Person(string name, int age)
    {
        Name = name;
        Age = age;
    }
}
 
class Program
{
    static void Main()
    {
        // Create and initialize an array of struct Person
        Person[] people = new Person[3]
        {
            new Person("John Doe", 25),
            new Person("Jane Smith", 30),
            new Person("Alice Johnson", 28)
        };
 
        // Print the details of each person in the array
        foreach (Person person in people)
        {
            Console.WriteLine($"Name: {person.Name}, Age: {person.Age}");
        }
    }
}
 

Struct vs Class

FeatureStructClass
TypeValue TypeReference Type
Inheritanceلا يدعميدعم
Memoryيتم تخزينها في الذاكرة المؤقتة (stack)يتم تخزينها في الذاكرة الديناميكية (heap)
Efficientيمكن أن يكون أكثر كفاءةقد يكون أقل كفاءة في بعض الحالات