public string name{get;set;}=new();
时间: 2023-07-30 09:09:14 浏览: 69
这个代码是一个自动属性,表示一个字符串类型的属性名为name,包含get和set访问器,同时使用了C# 9.0的新特性,使用“= new()”为属性提供了默认值,即使用了字符串的默认构造函数创建了一个空字符串。这样,在创建类的实例时,属性name的值即为一个空字符串。
在使用这个自动属性时,可以通过赋值操作符“=”来设置属性的值,例如:
```
myObject.name = "Alice";
```
也可以使用属性访问器来获取或设置属性的值,例如:
```
myObject.name = "Bob";
string myName = myObject.name;
```
相关问题
解释一下这行代码public string Name { get => _name; set => _name = value; }\n
这行是 C# 的属性,其中 _name 是一个私有字段,通过 get 和 set 访问器(accessor)公开了该字段。当调用 Name 属性的 get 访问器时,_name 的值将被返回;当调用 set 访问器时,_name 的值将被设置为传入的 value。
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace WinFormsApp16 { public class student { private int stuno; private string stuname; private int stuage; private string stusex; private string stuspeciality; public student(int myno ,string myname, int myage ,string mysex,string myspecility) { this.Stuno = myno; this.Stuname = myname; this.Stuage = myage; this.Stusex = mysex; this.Stuspeciality = myspecility; } public int Stuno { get => stuno; } public string Stuname { get => stuname; } public string Stusex { get => stusex; } public string Stuspeciality { get => stuspeciality; set => stuspeciality = value; } public int Stuage { get => stuage; set => stuage = value; } } }把这段代码优化成输入信息后再只读
这段代码是 C# 语言的代码,可以通过在 Windows 系统上安装 Visual Studio 编辑器进行修改和运行。
以下是将该代码优化为输入信息后再只读的代码示例:
```csharp
using System;
using System.Windows.Forms;
namespace WinFormsApp
{
public partial class Form1 : Form
{
private int stuno;
private string stuname;
private int stuage;
private string stusex;
private string stuspeciality;
public Form1()
{
InitializeComponent();
}
private void btnSubmit_Click(object sender, EventArgs e)
{
stuno = int.Parse(txtStuno.Text);
stuname = txtStuname.Text;
stuage = int.Parse(txtStuage.Text);
stusex = txtStusex.Text;
stuspeciality = txtStuspeciality.Text;
// 显示信息
txtInfo.Text = $"学号:{stuno}\r\n姓名:{stuname}\r\n年龄:{stuage}\r\n性别:{stusex}\r\n专业:{stuspeciality}";
// 设置文本框只读
txtStuno.ReadOnly = true;
txtStuname.ReadOnly = true;
txtStuage.ReadOnly = true;
txtStusex.ReadOnly = true;
txtStuspeciality.ReadOnly = true;
}
}
}
```
这段代码实现了一个简单的 Windows 窗体应用程序,包含了一个输入学生信息的表单,用户在填写完信息后,点击提交按钮,会将学生信息显示在一个文本框中,并将输入框设置为只读状态,不允许再次编辑。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)