在构造函数C#中的默认值相同,两个构造进行序列化 [英] C# default value in constructor same as two constructors for serialization

查看:247
本文介绍了在构造函数C#中的默认值相同,两个构造进行序列化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我提供一个默认值的构造

When I provide a constructor with a default value

  public MyClass(string description = null) { .... }

这是等同于

  public MyClass() { .... }
  public MyClass(string description) { .... }

连载。换句话说,是一个默认的构造函数可用?实际上它是,但是当我使用的序列化,我会面对一些问题?

in terms of Serialization. In other words, is a default constructor available? Practically it is, but will I face some issues when I use serialization?

推荐答案

没有。它不幸的是,没有一个默认的构造函数

No. It unfortunately is not a default constructor.

当你写的:

public MyClass(string description = null) { .... }

您实际上是在制作构造函数接受一个字符串参数,却标志着该属性的默认值的属性。这比上一类的默认构造函数不同。

You're actually making a constructor that accepts a string parameter, but has an attribute marking the default value for that attribute. This is different than having a default constructor on the class.

这篇关于在构造函数C#中的默认值相同,两个构造进行序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆