在C#中,类是一种定义对象属性和行为的

// 定义一个简单的Person类
public class Person
{
    public string Name { get; set; } // 属性
    public int Age { get; set; }    // 属性
 
    // 方法
    public void Greet()
    {
        Console.WriteLine($"Hello, my name is {Name} and I am {Age} years old.");
    }
}
 
// 创建Person类的实例并使用它
Person person = new Person();
person.Name = "Alice";
person.Age = 30;
person.Greet(); // 输出: Hello, my name is Alice and I am 30 years old.

 

按钮用于执行操作。在C#中,可以使用 Button 控件来实现这一功能。// 创建按钮
Button button = new Button();
button.Text = "点击我";
button.Click += new EventHandler(Button_Click); // 添加点击事件
this.Controls.Add(button);
 
private void Button_Click(object sender, EventArgs e)
{
    MessageBox.Show("按钮被点击了!");
}

 

 

点赞(0) 打赏

评论列表 共有 0 条评论

暂无评论

微信小程序

微信扫一扫体验

立即
投稿

微信公众账号

微信扫一扫加关注

发表
评论
返回
顶部