在c#中声明对象的方法 [英] ways to declare an object in c#
本文介绍了在c#中声明对象的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在c#中声明和初始化对象的可能方法是什么?
what r the possible ways to declare and initialize an object in c#?
推荐答案
这是过于基本的,我建议你阅读一些教程(你很喜欢在正确的位置,codeproject!),并且还打了msdn并开始在那里阅读。
http://msdn.microsoft。 com / en-us / vstudio / hh341490.aspx [ ^ ]
http://msdn.microsoft.com/en-us/library/618ayhy6.aspx [ ^ ]
http://msdn.microsoft.com/en-us/library/aa288436%28v=vs.71%29.aspx [ ^ ]
http:// msdn .microsoft.com / zh-cn / library / vstudio / 67ef8sbd.aspx [ ^ ]
http://www.dotnetperls.com/ [ ^ ]
http:/ /www.codeproject.com/KB/cs/#Beginners [ ^ ]
但是要好好回答你的问题,你去吧。
假设您有课程
This is overly basic, i suggest you read some tutorials (by golly you are at the right spot, codeproject!), and also hit up msdn and start reading there.
http://msdn.microsoft.com/en-us/vstudio/hh341490.aspx[^]
http://msdn.microsoft.com/en-us/library/618ayhy6.aspx[^]
http://msdn.microsoft.com/en-us/library/aa288436%28v=vs.71%29.aspx[^]
http://msdn.microsoft.com/en-us/library/vstudio/67ef8sbd.aspx[^]
http://www.dotnetperls.com/[^]
http://www.codeproject.com/KB/cs/#Beginners[^]
But to be nice and answer your question here you go.
Say you have a class
public class Person
{
public string Name { get; set; }
public int Age { get; set; }
public string Address { get; set; }
public string City { get; set; }
public string State { get; set; }
}
你可以用两种方式初学这门课
You can intialize this class in 2 ways
Person david = new Person();
david.Name = "david";
david.Age = 99999;
david.Addres = "100 main street";
david.City = "Somewhere over the rainbow";
david.State = "FL";
或者你可以像这样做对象初始化方法
http://msdn.microsoft.com/en-us/library/vstudio/bb384062.aspx [ ^ ]
Or you could do the object initializer approach like so
http://msdn.microsoft.com/en-us/library/vstudio/bb384062.aspx[^]
Person david = new Person
{
Name = "david",
Age = 99999;,
Addres = "100 main street",
City = "Somewhere over the rainbow",
State = "FL"
};
哇哇哇,你已经初始化了一个物体,让我的孩子出去并初始化!
And tah dah, you have initialized an object, go forth my child and initialize away!
这篇关于在c#中声明对象的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文