2
    Info myPath = new Info()
    {
        path = oFile.FileName
    };
    ...
    class Info
    {
        public string path;
        public string Path
        {
            get { return path; }
            set { path = value; }
        }
    }

以上是一些程序的C#代码,它可以正常工作。但我不是很明白。第一个问题是为什么path = oFile.FileName不写成path = oFile.FileName;?为什么分号可以去掉?

第二个问题是为什么我不能这样写:myPath.path = oFile.FileName?Visual Studio 2012 将给出错误消息。

有人可以帮忙吗?谢谢!

4

7 回答 7

3

在 C# 中初始化对象有很多方法。在这里你可以做你写的,这将相当于:

Info myPath = new Info();
myPath.Path = oFile.FileName;

这种语法

Info myPath = new Info()
{
    path = oFile.FileName
};

只是一个快捷方式,并且可以更具可读性,但会做同样的事情。实际上,它似乎是从 VisualBasic(With声明)中获取的。

解释上面的语法:

YourClassName <your_variable> = new YourClassName() 
{ 
    <property_name> = value,
    <anotherProperty> = value, 
    ... 
    <last_property> = value 
};

最后一种方法是有一个将路径作为参数并对其进行初始化的构造函数。这实际上是cpu完成的操作较少的方式(但这并不重要)。

于 2013-09-20T11:19:45.800 回答
3

That construct is an object initializer. It's not a list of arbitrary statements - it's only initialization of fields and properties, and they're comma-separated:

Foo x = new Foo // Implicitly calls the parameterless constructor
{
    Property1 = value1,
    Property2 = value2
};

That's shorthand for:

Foo tmp = new Foo();
tmp.Property1 = value1;
tmp.Property2 = value2;
Foo x = tmp;

Object initializers were introduced in C# 3, along with collection initializers which are effectively syntactic sugar for repeated calls to Add. So:

List<string> names = new List<string>
{
    "Foo", "Bar"
};

is equivalent to:

List<string> tmp = new List<string>();
tmp.Add("Foo");
tmp.Add("Bar");
List<string> names = tmp;
于 2013-09-20T11:21:11.257 回答
2

In C# 3.0, they added a new & helpful feature for initializing objects as a single statement/expression.

Whereas before, you'd have to issue separate statements:

Info myPath = new Info();
myPath.Filename = ...
myPath.AnotherProperty = ...
myPath.AnotherAnotherProperty = ...

You can now perform the same assignments in one step:

Info myPath = new Info
{
    Filename = ...
    AnotherProperty = ...
    AnotherAnotherProperty = ...
};

This is especially useful, for constructing objects in Linq queries (without having to custom-code object constructors).

For example:

someList.Select(x => new SomethingElse{ SomeProperty = x.Something });
于 2013-09-20T11:20:46.393 回答
1
   Info myPath = new Info()
    {
        path = oFile.FileName
    };

意味着:初始化一个新的信息类并将值 oFile.FileName 添加到属性路径,它是以下的简短版本:

Info myPath = new Info();
myPath.path = oFile.FileName;

你不需要 ';' 因为您可以像这样在括号中堆叠更多属性:

Person p = new Person()
{
Name = "John",
Age = 25
};
于 2013-09-20T11:20:33.033 回答
0

这是在 C# 中初始化变量的新方法。您也可以跳过“()”符号。您也可以在括号中初始化列表、数组等,您必须在对象中插入要初始化的元素。

于 2013-09-20T11:17:13.213 回答
0
Info myPath = new Info()
{
    path = oFile.FileName
};

相当于

Info myPath = new Info();
myPath.path = oFile.FileName;

使用对象初始化结构进行初始化时,您将创建一个属性分配列表,如您所记。这样,您可以在一次调用中创建对象并分配变量,而无需显式构造函数。以上内容可以很容易地写在一行中。

更多信息可以在 MSDN 网站上找到。

于 2013-09-20T11:21:40.443 回答
0

C# 允许您在构造对象的同时初始化对象的属性。

这些都是等价的:

var foo = new Foo();
foo.Property = "Bar";
foo.AnotherProperty = 12;


var foo = new Foo()
{
    Property = "Bar",
    AnotherProperty = 12
};


// The parentheses are not really necessary for parameterless constructors
var foo = new Foo
{
    Property = "Bar",
    AnotherProperty = 12
};
于 2013-09-20T11:22:25.343 回答