c# – .NET中用户创建的结构和框架结构之间的差异

栏目: ASP.NET · 发布时间: 7年前

内容简介:翻译自:https://stackoverflow.com/questions/4268452/differences-between-user-created-structs-and-framework-structs-in-net

为什么C#编译器不允许你编译它:

int a;
Console.WriteLine(a);

但是允许你编译:

MyStruct a;
Console.WriteLine(a);

其中MyStruct定义为:

struct MyStruct
{

}

更新:在第一个案例中错误是:

Error 1 Use of unassigned local  variable ‘a’
C#不允许从未初始化的本地人那里阅读.以下是适用于此背景的 language specification

摘录:

5.3 Definite assignment

A struct-type variable is considered

definitely assigned

if each of its

instance variables is considered

definitely assigned.

显然,由于你的结构没有字段,这不是问题;它被认为是明确分配的.向其添加字段应该会破坏构建.

在一个有点相关的说明:

11.3.8 Constructors
No instance member function can be called until all fields  of the struct being constructed have  been definitely assigned.

翻译自:https://stackoverflow.com/questions/4268452/differences-between-user-created-structs-and-framework-structs-in-net


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

具体数学(英文版第2版)

具体数学(英文版第2版)

[美] Ronald L. Graham、Donald E. Knuth、Oren Patashnik / 机械工业出版社 / 2002-8 / 49.00元

This book introduces the mathematics that supports advanced computer Programming and the analysis of algorithms. The primary aim of its well-known authors is to provide a solid and relevant base of ma......一起来看看 《具体数学(英文版第2版)》 这本书的介绍吧!

Base64 编码/解码
Base64 编码/解码

Base64 编码/解码

MD5 加密
MD5 加密

MD5 加密工具

RGB HSV 转换
RGB HSV 转换

RGB HSV 互转工具