View Full Version : سوال: سوال درباره ساختار در سی شارپ

دوشنبه 14 فروردین 1391, 02:12 صبح

اگه میشه یک توضیح مختصر درباره ساختار در سی شارپ را توضیح دهید
و چه کاربردهایی دارد


دوشنبه 14 فروردین 1391, 09:39 صبح
What Are Structs?
Structs are programmer-defined data types, very similar to classes. They have data members and
function members. Although similar to classes, there are a number of important differences. The most
important ones are the following:
• Classes are reference types, and structs are value types.
• Structs are implicitly sealed, which means they cannot be derived from.
The syntax for declaring a struct is similar to that of declaring a class:

struct StructName

For example, the following code declares a struct named Point. It has two public fields, named X and
Y. In Main, three variables of struct type Point are declared, and their values are assigned and printed out.

struct Point
public int X;
public int Y;
class Program
static void Main()
Point first, second, third;
first.X = 10; first.Y = 10;
second.X = 20; second.Y = 20;
third.X = first.X + second.X;
third.Y = first.Y + second.Y;
Console.WriteLine("first: {0}, {1}", first.X, first.Y);
Console.WriteLine("second: {0}, {1}", second.X, second.Y);
Console.WriteLine("third: {0}, {1}", third.X, third.Y);