在C#中,变量初始化可以通过以下几种方式进行:
int myNumber = 10;
string myName = "John Doe";
bool isAvailable = true;
class MyClass
{
public int Number { get; set; }
public string Name { get; set; }
public bool IsAvailable { get; set; }
public MyClass(int number, string name, bool isAvailable)
{
Number = number;
Name = name;
IsAvailable = isAvailable;
}
}
MyClass obj = new MyClass(10, "John Doe", true);
class MyClass
{
public int Number { get; set; }
public string Name { get; set; }
public bool IsAvailable { get; set; }
public void Initialize(int number, string name, bool isAvailable)
{
Number = number;
Name = name;
IsAvailable = isAvailable;
}
}
MyClass obj = new MyClass();
obj.Initialize(10, "John Doe", true);
var myNumber = 10;
var myName = "John Doe";
var isAvailable = true;
class MyClass
{
public int Number { get; set; }
public string Name { get; set; }
public bool IsAvailable { get; set; }
}
MyClass obj = new MyClass
{
Number = 10,
Name = "John Doe",
IsAvailable = true
};
List<MyClass> myList = new List<MyClass>
{
new MyClass { Number = 10, Name = "John Doe", IsAvailable = true },
new MyClass { Number = 20, Name = "Jane Doe", IsAvailable = false }
};
这些方法可以根据实际需求和编程场景选择使用。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。