C# Intermediate – Structures



A structure is a value type, the opposite of a class which is a reference type, and it has its own fields, methods, and constructors like a class.

Maybe you didn’t realize, but we have worked with structures in our previous articles, especially in module 1 C# basics.

	using System;

	namespace CSharpIntermediateConstructsStruct
	{
		class Complex 
		{ 
			public int x; 
			public int y;
		}
		
		struct Real
		{ 
			public int x; 
			public int y;
		}
		
		class StructureAndClassAssignment
		{
			static void Main(string[] args)
			{
				Complex complexNumber1 = new Complex();
				complexNumber1.x = 10;
				complexNumber1.y = 20;
				Complex complexNumber2 = complexNumber1;
				complexNumber1.x = 1000;
				complexNumber1.y = 2000;
				Console.WriteLine("Class: x:{0}, y:{1}", complexNumber2.x, complexNumber2.y);
				
				Real realNumber1 = new Real();
				realNumber1.x = 100;
				realNumber1.y = 200;
				Real realNumber2 = realNumber1;
				realNumber1.x = 10;
				realNumber1.y = 20;
				Console.WriteLine("Struct: x:{0}, y:{1}", realNumber2.x, realNumber2.y);
			}
		}
	}

0 Comment's

Comment Form