i want to share one finding that i found today and am pretty surprised.
Although class types are passed by reference, nested classes are not.

Form1.cs :

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace nestedClassRequiresRefKeyword
{
	public partial class Form1 : Form
	{
		public Form1()
		{
			InitializeComponent();
		}

		private void button1_Click(object sender, EventArgs e)
		{
			ContainerClass.NestedClass nestedClass = new ContainerClass.NestedClass("something");
			UseWithRefKeyword(ref nestedClass);
			MessageBox.Show(nestedClass.GetField());
		}

		private void button2_Click(object sender, EventArgs e)
		{
			ContainerClass.NestedClass nestedClass = new ContainerClass.NestedClass("something");
			UseWithoutRefKeyword(nestedClass);
			MessageBox.Show(nestedClass.GetField());
		}
		private void UseWithRefKeyword(ref ContainerClass.NestedClass nestedClass)
		{
			nestedClass = new ContainerClass.NestedClass("new something");
		}
		private void UseWithoutRefKeyword(ContainerClass.NestedClass nestedClass)
		{
			nestedClass = new ContainerClass.NestedClass("new something");
		}
	}
}

Form1.Designer.cs :

namespace nestedClassRequiresRefKeyword
{
	partial class Form1
	{
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.IContainer components = null;

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
		protected override void Dispose(bool disposing)
		{
			if (disposing && (components != null))
			{
				components.Dispose();
			}
			base.Dispose(disposing);
		}

		#region Windows Form Designer generated code

		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.button1 = new System.Windows.Forms.Button();
			this.button2 = new System.Windows.Forms.Button();
			this.SuspendLayout();
			// 
			// button1
			// 
			this.button1.Location = new System.Drawing.Point(41, 55);
			this.button1.Name = "button1";
			this.button1.Size = new System.Drawing.Size(144, 23);
			this.button1.TabIndex = 0;
			this.button1.Text = "Use with ref keyword";
			this.button1.UseVisualStyleBackColor = true;
			this.button1.Click += new System.EventHandler(this.button1_Click);
			// 
			// button2
			// 
			this.button2.Location = new System.Drawing.Point(41, 95);
			this.button2.Name = "button2";
			this.button2.Size = new System.Drawing.Size(144, 23);
			this.button2.TabIndex = 1;
			this.button2.Text = "Use without ref keyword";
			this.button2.UseVisualStyleBackColor = true;
			this.button2.Click += new System.EventHandler(this.button2_Click);
			// 
			// Form1
			// 
			this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
			this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
			this.ClientSize = new System.Drawing.Size(292, 266);
			this.Controls.Add(this.button2);
			this.Controls.Add(this.button1);
			this.Name = "Form1";
			this.Text = "Form1";
			this.ResumeLayout(false);

		}

		#endregion

		private System.Windows.Forms.Button button1;
		private System.Windows.Forms.Button button2;
	}
}

ContainerClass.cs :

using System;
using System.Collections.Generic;
using System.Text;

namespace nestedClassRequiresRefKeyword
{
	public class ContainerClass
	{
		public class NestedClass
		{
			private string field = string.Empty;
			public NestedClass(string field)
			{
				this.field = field;
			}
			public string GetField()
			{
				return field;
			}
		}
	}
}

Scott, ddanbe, Ramy, Adatapost, DdoubleD.. you need to contribute to this knowledge. This is an interesting and important finding. Scott you should have some idea as to why this is so and such..

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.