C#中goto label等效于循环实现查找

发布时间:2024年01月01日

目录

一、关于goto label

二、示例

三、生成效果

四、关于public bool Contains(String value)


一、关于goto label

????????goto语句是一个非常灵活的语句,可以使用goto语句跳转到定义标签的位置,这里所说的标签包括switch语句中的case标签、default标签和语句声明的标签,也可以使用goto语句跳出多层嵌套循环。?????????

????????标签的作用域为声明它的整个语句块,包括其中的嵌套块。goto语句可以在标签的作用域中将程序执行的位置移动到当前标签的位置。

goto语句的3种格式如下:
goto【标签】
goto case【参数表达式】
goto default

?????????不要过多使用goto语句,由于goto语句过于灵活,或多或少地会干扰程序的执行逻辑。

二、示例

// goto label等效于循环语句
namespace _034
{
    public partial class Form1 : Form
    {
        private ListBox? listBox1;
        private GroupBox? groupBox1;
        private Button? button1;
        private TextBox? textBox1;
        private Label? label1;
        readonly string[] book_array =        
        [
        "C#范例宝典",
        "C#编程宝典",
        "C#视频学",
        "C#项目开发全程实录",
        "C#项目开发实例自学手册",
        "C#编程词典",
        "C#实战宝典",
        "C#经验技巧宝典",
        "C#入门模式",
        ];  //定义集合并初始化

        public Form1()
        {
            InitializeComponent();
            Load += Form1_Load;
        }

        private void Form1_Load(object? sender, EventArgs e)
        {
            // 
            // button1
            // 
            button1 = new Button
            {
                Location = new Point(15, 92),
                Name = "button1",
                Size = new Size(75, 23),
                TabIndex = 2,
                Text = "搜索",
                UseVisualStyleBackColor = true
            };
            button1.Click += Button1_Click;
            // 
            // textBox1
            // 
            textBox1 = new TextBox
            {
                Location = new Point(15, 57),
                Name = "textBox1",
                Size = new Size(100, 23),
                TabIndex = 1
            };
            // 
            // label1
            // 
            label1 = new Label
            {
                AutoSize = true,
                Location = new Point(15, 28),
                Name = "label1",
                Size = new Size(44, 17),
                TabIndex = 0,
                Text = "书名:"
            };
            // 
            // listBox1
            // 
            listBox1 = new ListBox
            {
                FormattingEnabled = true,
                ItemHeight = 17,
                Location = new Point(12, 12),
                Name = "listBox1",
                Size = new Size(175, 160),
                TabIndex = 0
            };
            // 
            // groupBox1
            // 
            groupBox1 = new GroupBox
            {
                Location = new Point(193, 10),
                Name = "groupBox1",
                Size = new Size(174, 158),
                TabIndex = 1,
                TabStop = false,
                Text = "搜索图书"
            };
            groupBox1.Controls.Add(button1);
            groupBox1.Controls.Add(textBox1);
            groupBox1.Controls.Add(label1);
            groupBox1.SuspendLayout();

            // 
            // Form1
            // 
            AutoScaleDimensions = new SizeF(7F, 17F);
            AutoScaleMode = AutoScaleMode.Font;
            ClientSize = new Size(379, 180);
            Controls.Add(groupBox1);
            Controls.Add(listBox1);
            Name = "Form1";
            StartPosition = FormStartPosition.CenterScreen;
            Text = "goto label搜索图书";            
            groupBox1.ResumeLayout(false);
            groupBox1.PerformLayout();
            ResumeLayout(false);

            listBox1.Items.AddRange(book_array);
        }
        /// <summary>
        /// 只要列表框里的书名包含文本框里的文字,就视为查询到;
        /// 多本书名满足搜索条件时,只输出第一个满足条件的记录;
        /// 文本框为空也视为查找到了,可以设置条件规避文本框为空;
        /// </summary>
        private void Button1_Click(object? sender, EventArgs e)
        {
            int i = 0;  //定义计数器
        label1:         //声明的标签
            if (textBox1!.Text != "")
            {
                if (book_array[i].Contains(textBox1.Text))   //只要书名包含查询条件就为真
                {
                    listBox1!.SelectedIndex = i;             //选中查找到的结果
                    MessageBox.Show(textBox1.Text + " 已经找到!", "提示!");
                    MessageBox.Show(" 找到的书是:" + book_array[i], "提示!");
                    return;
                }
                i++;
                if (i < book_array.Length) goto label1;      //倘若i<数组所有元素的数量
                MessageBox.Show(textBox1.Text + " 没有找到!", "提示!");
            }
            else
            {
                MessageBox.Show("查询条件不能为空", "提示!");
            }
        }
    }
}

三、生成效果

?

?

四、关于public bool Contains(String value)

????????实例中出现的Contain()方法:

?public bool Contains(String value);

//
// 摘要:
// ? ? Returns a value indicating whether a specified substring occurs within this string.
//
//
// 参数:
// ? value:
// ? ? The string to seek.
//
// 返回结果:
// ? ? true if the value parameter occurs within this string, or if value is the empty
// ? ? string (""); otherwise, false.
//
// 异常:
// ? T:System.ArgumentNullException:
// ? ? value is null.

文章来源:https://blog.csdn.net/wenchm/article/details/135324158
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。