Click here to Skip to main content
15,867,453 members
Articles / Programming Languages / C#

Using Find in a Generic List

Rate me:
Please Sign up or sign in to vote.
4.06/5 (7 votes)
27 Oct 2008CPOL3 min read 70.4K   999   33  
This article investigates the use of List.Find() when using value and reference types, and how to use it when working with your own custom classes.
namespace GenericsFind
{
    partial class frmMain
    {
        /// <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.groupBox1 = new System.Windows.Forms.GroupBox();
            this.lblResponseValue = new System.Windows.Forms.Label();
            this.txtValueResponse = new System.Windows.Forms.TextBox();
            this.label1 = new System.Windows.Forms.Label();
            this.btnFindValueType = new System.Windows.Forms.Button();
            this.label2 = new System.Windows.Forms.Label();
            this.txtValueInput1 = new System.Windows.Forms.TextBox();
            this.btnFindValueTypeNullable = new System.Windows.Forms.Button();
            this.groupBox2 = new System.Windows.Forms.GroupBox();
            this.txtRerenceInput1 = new System.Windows.Forms.TextBox();
            this.label3 = new System.Windows.Forms.Label();
            this.label4 = new System.Windows.Forms.Label();
            this.txtReferenceResponse = new System.Windows.Forms.TextBox();
            this.label5 = new System.Windows.Forms.Label();
            this.btnFindPerson = new System.Windows.Forms.Button();
            this.label6 = new System.Windows.Forms.Label();
            this.label7 = new System.Windows.Forms.Label();
            this.label8 = new System.Windows.Forms.Label();
            this.label9 = new System.Windows.Forms.Label();
            this.btnFindArray = new System.Windows.Forms.Button();
            this.groupBox1.SuspendLayout();
            this.groupBox2.SuspendLayout();
            this.SuspendLayout();
            // 
            // groupBox1
            // 
            this.groupBox1.BackColor = System.Drawing.SystemColors.ActiveCaption;
            this.groupBox1.Controls.Add(this.label9);
            this.groupBox1.Controls.Add(this.label8);
            this.groupBox1.Controls.Add(this.btnFindValueTypeNullable);
            this.groupBox1.Controls.Add(this.txtValueInput1);
            this.groupBox1.Controls.Add(this.label2);
            this.groupBox1.Controls.Add(this.lblResponseValue);
            this.groupBox1.Controls.Add(this.txtValueResponse);
            this.groupBox1.Controls.Add(this.label1);
            this.groupBox1.Controls.Add(this.btnFindValueType);
            this.groupBox1.ForeColor = System.Drawing.SystemColors.ControlText;
            this.groupBox1.Location = new System.Drawing.Point(12, 12);
            this.groupBox1.Name = "groupBox1";
            this.groupBox1.Size = new System.Drawing.Size(311, 242);
            this.groupBox1.TabIndex = 3;
            this.groupBox1.TabStop = false;
            this.groupBox1.Text = "VALUE TYPES";
            // 
            // lblResponseValue
            // 
            this.lblResponseValue.AutoSize = true;
            this.lblResponseValue.ForeColor = System.Drawing.SystemColors.ControlLightLight;
            this.lblResponseValue.Location = new System.Drawing.Point(6, 163);
            this.lblResponseValue.Name = "lblResponseValue";
            this.lblResponseValue.Size = new System.Drawing.Size(58, 13);
            this.lblResponseValue.TabIndex = 3;
            this.lblResponseValue.Text = "Response:";
            // 
            // txtValueResponse
            // 
            this.txtValueResponse.Location = new System.Drawing.Point(6, 179);
            this.txtValueResponse.Multiline = true;
            this.txtValueResponse.Name = "txtValueResponse";
            this.txtValueResponse.Size = new System.Drawing.Size(294, 51);
            this.txtValueResponse.TabIndex = 2;
            // 
            // label1
            // 
            this.label1.AutoSize = true;
            this.label1.BackColor = System.Drawing.SystemColors.ActiveCaption;
            this.label1.Font = new System.Drawing.Font("Verdana", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.label1.ForeColor = System.Drawing.SystemColors.ControlLightLight;
            this.label1.Location = new System.Drawing.Point(6, 27);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(294, 26);
            this.label1.TabIndex = 1;
            this.label1.Text = "This will do a Find on a generic list populated with \r\nValue types (int32 for thi" +
                "s example)";
            // 
            // btnFindValueType
            // 
            this.btnFindValueType.ForeColor = System.Drawing.SystemColors.ControlText;
            this.btnFindValueType.Location = new System.Drawing.Point(6, 115);
            this.btnFindValueType.Name = "btnFindValueType";
            this.btnFindValueType.Size = new System.Drawing.Size(144, 33);
            this.btnFindValueType.TabIndex = 0;
            this.btnFindValueType.Text = "Find Value Type";
            this.btnFindValueType.UseVisualStyleBackColor = true;
            this.btnFindValueType.Click += new System.EventHandler(this.btnFindValueType_Click);
            // 
            // label2
            // 
            this.label2.AutoSize = true;
            this.label2.ForeColor = System.Drawing.SystemColors.ControlLightLight;
            this.label2.Location = new System.Drawing.Point(9, 86);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(60, 13);
            this.label2.TabIndex = 4;
            this.label2.Text = "Find Value:";
            // 
            // txtValueInput1
            // 
            this.txtValueInput1.Location = new System.Drawing.Point(76, 86);
            this.txtValueInput1.Name = "txtValueInput1";
            this.txtValueInput1.Size = new System.Drawing.Size(224, 20);
            this.txtValueInput1.TabIndex = 5;
            // 
            // btnFindValueTypeNullable
            // 
            this.btnFindValueTypeNullable.ForeColor = System.Drawing.SystemColors.ControlText;
            this.btnFindValueTypeNullable.Location = new System.Drawing.Point(156, 115);
            this.btnFindValueTypeNullable.Name = "btnFindValueTypeNullable";
            this.btnFindValueTypeNullable.Size = new System.Drawing.Size(144, 33);
            this.btnFindValueTypeNullable.TabIndex = 6;
            this.btnFindValueTypeNullable.Text = "Find Using Nullable Types";
            this.btnFindValueTypeNullable.UseVisualStyleBackColor = true;
            this.btnFindValueTypeNullable.Click += new System.EventHandler(this.btnFindValueTypeNullable_Click);
            // 
            // groupBox2
            // 
            this.groupBox2.BackColor = System.Drawing.SystemColors.ActiveCaption;
            this.groupBox2.Controls.Add(this.btnFindArray);
            this.groupBox2.Controls.Add(this.label7);
            this.groupBox2.Controls.Add(this.label6);
            this.groupBox2.Controls.Add(this.txtRerenceInput1);
            this.groupBox2.Controls.Add(this.label3);
            this.groupBox2.Controls.Add(this.label4);
            this.groupBox2.Controls.Add(this.txtReferenceResponse);
            this.groupBox2.Controls.Add(this.label5);
            this.groupBox2.Controls.Add(this.btnFindPerson);
            this.groupBox2.ForeColor = System.Drawing.SystemColors.ControlText;
            this.groupBox2.Location = new System.Drawing.Point(329, 12);
            this.groupBox2.Name = "groupBox2";
            this.groupBox2.Size = new System.Drawing.Size(311, 242);
            this.groupBox2.TabIndex = 4;
            this.groupBox2.TabStop = false;
            this.groupBox2.Text = "REFERENCE TYPES";
            // 
            // txtRerenceInput1
            // 
            this.txtRerenceInput1.Location = new System.Drawing.Point(129, 89);
            this.txtRerenceInput1.Name = "txtRerenceInput1";
            this.txtRerenceInput1.Size = new System.Drawing.Size(171, 20);
            this.txtRerenceInput1.TabIndex = 5;
            // 
            // label3
            // 
            this.label3.AutoSize = true;
            this.label3.ForeColor = System.Drawing.SystemColors.ControlLightLight;
            this.label3.Location = new System.Drawing.Point(9, 89);
            this.label3.Name = "label3";
            this.label3.Size = new System.Drawing.Size(114, 13);
            this.label3.TabIndex = 4;
            this.label3.Text = "Find Value (Firstname):";
            // 
            // label4
            // 
            this.label4.AutoSize = true;
            this.label4.ForeColor = System.Drawing.SystemColors.ControlLightLight;
            this.label4.Location = new System.Drawing.Point(6, 163);
            this.label4.Name = "label4";
            this.label4.Size = new System.Drawing.Size(58, 13);
            this.label4.TabIndex = 3;
            this.label4.Text = "Response:";
            // 
            // txtReferenceResponse
            // 
            this.txtReferenceResponse.Location = new System.Drawing.Point(6, 179);
            this.txtReferenceResponse.Multiline = true;
            this.txtReferenceResponse.Name = "txtReferenceResponse";
            this.txtReferenceResponse.Size = new System.Drawing.Size(294, 51);
            this.txtReferenceResponse.TabIndex = 2;
            // 
            // label5
            // 
            this.label5.AutoSize = true;
            this.label5.BackColor = System.Drawing.SystemColors.ActiveCaption;
            this.label5.Font = new System.Drawing.Font("Verdana", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.label5.ForeColor = System.Drawing.SystemColors.ControlLightLight;
            this.label5.Location = new System.Drawing.Point(6, 27);
            this.label5.Name = "label5";
            this.label5.Size = new System.Drawing.Size(294, 26);
            this.label5.TabIndex = 1;
            this.label5.Text = "This will do a Find on a generic list populated with \r\nReference types (custom cl" +
                "ass for this example)";
            // 
            // btnFindPerson
            // 
            this.btnFindPerson.ForeColor = System.Drawing.SystemColors.ControlText;
            this.btnFindPerson.Location = new System.Drawing.Point(6, 115);
            this.btnFindPerson.Name = "btnFindPerson";
            this.btnFindPerson.Size = new System.Drawing.Size(144, 33);
            this.btnFindPerson.TabIndex = 0;
            this.btnFindPerson.Text = "Find Person";
            this.btnFindPerson.UseVisualStyleBackColor = true;
            this.btnFindPerson.Click += new System.EventHandler(this.btnFindPerson_Click);
            // 
            // label6
            // 
            this.label6.AutoSize = true;
            this.label6.ForeColor = System.Drawing.SystemColors.ControlLightLight;
            this.label6.Location = new System.Drawing.Point(9, 63);
            this.label6.Name = "label6";
            this.label6.Size = new System.Drawing.Size(62, 13);
            this.label6.TabIndex = 6;
            this.label6.Text = "Person List:";
            // 
            // label7
            // 
            this.label7.AutoSize = true;
            this.label7.ForeColor = System.Drawing.SystemColors.ControlLightLight;
            this.label7.Location = new System.Drawing.Point(126, 63);
            this.label7.Name = "label7";
            this.label7.Size = new System.Drawing.Size(132, 13);
            this.label7.TabIndex = 7;
            this.label7.Text = "John Smith, Victor Matfield";
            // 
            // label8
            // 
            this.label8.AutoSize = true;
            this.label8.ForeColor = System.Drawing.SystemColors.ControlLightLight;
            this.label8.Location = new System.Drawing.Point(9, 63);
            this.label8.Name = "label8";
            this.label8.Size = new System.Drawing.Size(26, 13);
            this.label8.TabIndex = 7;
            this.label8.Text = "List:";
            // 
            // label9
            // 
            this.label9.AutoSize = true;
            this.label9.ForeColor = System.Drawing.SystemColors.ControlLightLight;
            this.label9.Location = new System.Drawing.Point(73, 63);
            this.label9.Name = "label9";
            this.label9.Size = new System.Drawing.Size(49, 13);
            this.label9.TabIndex = 8;
            this.label9.Text = "1, 2, 3, 4";
            // 
            // btnFindArray
            // 
            this.btnFindArray.ForeColor = System.Drawing.SystemColors.ControlText;
            this.btnFindArray.Location = new System.Drawing.Point(156, 115);
            this.btnFindArray.Name = "btnFindArray";
            this.btnFindArray.Size = new System.Drawing.Size(144, 33);
            this.btnFindArray.TabIndex = 8;
            this.btnFindArray.Text = "Helper method";
            this.btnFindArray.UseVisualStyleBackColor = true;
            this.btnFindArray.Click += new System.EventHandler(this.btnFindArray_Click);
            // 
            // frmMain
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.BackColor = System.Drawing.SystemColors.ActiveCaption;
            this.ClientSize = new System.Drawing.Size(656, 278);
            this.Controls.Add(this.groupBox2);
            this.Controls.Add(this.groupBox1);
            this.Name = "frmMain";
            this.Text = "Main Form";
            this.groupBox1.ResumeLayout(false);
            this.groupBox1.PerformLayout();
            this.groupBox2.ResumeLayout(false);
            this.groupBox2.PerformLayout();
            this.ResumeLayout(false);

        }

        #endregion

        private System.Windows.Forms.GroupBox groupBox1;
        private System.Windows.Forms.Label lblResponseValue;
        private System.Windows.Forms.TextBox txtValueResponse;
        private System.Windows.Forms.Label label1;
        private System.Windows.Forms.Button btnFindValueType;
        private System.Windows.Forms.Label label2;
        private System.Windows.Forms.TextBox txtValueInput1;
        private System.Windows.Forms.Button btnFindValueTypeNullable;
        private System.Windows.Forms.GroupBox groupBox2;
        private System.Windows.Forms.TextBox txtRerenceInput1;
        private System.Windows.Forms.Label label3;
        private System.Windows.Forms.Label label4;
        private System.Windows.Forms.TextBox txtReferenceResponse;
        private System.Windows.Forms.Label label5;
        private System.Windows.Forms.Button btnFindPerson;
        private System.Windows.Forms.Label label9;
        private System.Windows.Forms.Label label8;
        private System.Windows.Forms.Label label7;
        private System.Windows.Forms.Label label6;
        private System.Windows.Forms.Button btnFindArray;
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
South Africa South Africa
I'm a senior software developer from South Africa and have a huge passion for .NET and Microsoft related technologies.

When I'm not having fun figuring out an architecture or class design, you'd probably find me rocking on the drums, running around with my badminton racquet or enjoying another bitter coffee or an ice-cold Amstel at the bar.

Comments and Discussions