Snippets

Alex Darby Compare Struct Instances (note: if you need to do this you should really be using a class)

You are viewing an old version of this snippet. View the current version.
Revised by Alex Darby 14e0298
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace structRefChecker
{
	public struct TestStruct
	{
		static uint s_structIdGenerator = 0;

		private uint	m_structId;
		public int		m_someValue;

		public TestStruct( int value )
		{
			m_someValue	= value;
			m_structId	= s_structIdGenerator++;
		}

		public static bool IsSameInstance( TestStruct one,  TestStruct two )
		{
			return ( one.m_structId == two.m_structId );
		}
	}

	

	class Program
	{
		static void ProcessTestStructs( ref TestStruct one, ref TestStruct two )
		{
			if( TestStruct.IsSameInstance( one, two ) )
			{
				Console.WriteLine( "one and two are the same instance" );
				return;
			}
				Console.WriteLine( "one and two are different instances" );
		}

		static void Main( string[] args )
		{
			var testOne = new TestStruct( 1 );
			var testTwo = new TestStruct( 2 );

			ProcessTestStructs( ref testOne, ref testTwo ); 
			ProcessTestStructs( ref testOne, ref testOne ); 
			ProcessTestStructs( ref testTwo, ref testTwo ); 

			Console.ReadKey();
		}
	}
}
HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.