English 中文(简体)
为什么我们使用新的关键词来启动结构,尽管它具有价值类型
原标题:Why we use new keyword for initializing structure though it s a value type
  • 时间:2011-11-21 07:53:39
  •  标签:
  • c#
  • structure

我们为什么使用<代码>新 尽管结构在C#中具有价值类型,但还是初步形成结构的关键词?

问题回答

由于这是你所说的话的关键词:use/em>,以开始形成一种价值类型(通过一种习惯构造或简单地使记忆零化(代号为new(<>>,>在价值类型上的使用),尽管即便没有必要,也存在一种优势。 添加“<>不同>的同一概念的关键词似乎有困难。 如果你认为是通用的:

public static T Create<T>() where T : new() // not terribly helpful, but....
{
    return new T();
}

如果在参考型号和价值型号方面有不同的关键词,这将非常混淆,例如,var0 = 创造与设计;int>:。 再说一遍,这个例子令人sil笑皆是,但这里有点。

当你使用新运营商制造一个构件时,便会制造,并打电话给适当的建筑商。

但有一个例外。 可以在不使用新运营商的情况下即刻进行施工。 在这种情况下,没有建筑商呼吁,这提高了分配的效率。 然而,这些田地仍将是没有签名的,只有在所有田地都开标之前,才能使用。

例如:

public struct CoOrds
{
    public int x, y;

    public CoOrds(int p1, int p2)
    {
        x = p1;
        y = p2;
    }
}

 // Declare an 
 CoOrds coords1;

 // Initialize:
 coords1.x = 10;
 coords1.y = 20;




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签