未初始化的属性是否为默认值,因为它们在使用默认构造函数时? [英] Are uninitialised attributes given default values as they are when using the default constructor?

查看:475
本文介绍了未初始化的属性是否为默认值,因为它们在使用默认构造函数时?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果使用默认构造函数或无参参数构造函数创建串口,则端口将被赋予默认值。



当使用带有参数的构造函数时:

当使用带参数的构造函数和使用默认构造函数时,串联端口的构造函数中的参数之外的属性赋予相同的默认值?



这个问题,有趣的答案,评论和随后的聊天讨论让我问这个。

解决方案

您可以查看其源代码代码,并查看构造函数和归档定义。



您将看到所有重要的属性,包括 PortName code> BaudRate , DataBits Parity StopBits 具有默认值,如果它们不存在于构造函数参数中,则将使用默认值。还有一些其他重要属性,如 HandShake 也有默认值,而它们不存在于构造函数中。

  public SerialPort(System.ComponentModel.IContainer container)
{
container.Add(this);
}
public SerialPort()
{
}
//非设计的SerialPort构造函数chain,
//对于未指定的成员使用默认值通过参数
public SerialPort(string portName)
:此(portName,defaultBaudRate,defaultParity,defaultDataBits,defaultStopBits)
{
}
public SerialPort baudRate)
:这个(portName,baudRate,defaultParity,defaultDataBits,defaultStopBits)
{
}
public SerialPort(string portName,int baudRate,Parity parity)
:这个(portName,baudRate,parity,defaultDataBits,defaultStopBits)
{
}
public SerialPort(string portName,int baudRate,Parity parity,int dataBits)
:波特率,奇偶校验,dataBits,defaultStopBits)
{
}
public SerialPort(string portName,int baudRate,Parity parity,
int dataBits,StopBits stopBits)
{
this.PortName = portName;
this.BaudRate = baudRate;
this.Parity = parity;
this.DataBits = dataBits;
this.StopBits = stopBits;
}

以下是这些字段:

  private int baudRate = defaultBaudRate; 
private int dataBits = defaultDataBits;
private Parity parity = defaultParity;
private StopBits stopBits = defaultStopBits;
private string portName = defaultPortName;


If creating a serial port with the default constructor or parameterless constructor, the port is given default values. From the documentation:

// Create a new SerialPort object with default settings.
_serialPort = new SerialPort();

This constructor uses default property values when none are specified. For example, the DataBits property defaults to 8, the Parity property defaults to the None enumeration value, the StopBits property defaults to 1, and a default port name of COM1.

There are several constructors

When using a constructor with parameters:
Are the attributes that are not within the parameters of the constructor of the serial port given the same default values when using a constructor with parameters and when using the default constructor?

The problem in this question, the interesting answers, comments and subsequent chat discussion let me to ask this.

解决方案

You can take a look at its source code and see constructors and filed definitions.

You will see all important properties including PortName, BaudRate, DataBits, Parity and StopBits have default values which if they are not present in constructor arguments, the default value will be used for them. Also some other important properties like HandShake has default values as well, while they are not present in constructors.

public SerialPort(System.ComponentModel.IContainer container)
{
    container.Add(this);
}
public SerialPort()
{
}
// Non-design SerialPort constructors here chain, 
//using default values for members left unspecified by parameters
public SerialPort(string portName) 
    : this (portName, defaultBaudRate, defaultParity, defaultDataBits, defaultStopBits)
{
}
public SerialPort(string portName, int baudRate) 
    : this (portName, baudRate, defaultParity, defaultDataBits, defaultStopBits)
{
}
public SerialPort(string portName, int baudRate, Parity parity)
    : this (portName, baudRate, parity, defaultDataBits, defaultStopBits)
{
}
public SerialPort(string portName, int baudRate, Parity parity, int dataBits)
    : this (portName, baudRate, parity, dataBits, defaultStopBits)
{
}
public SerialPort(string portName, int baudRate, Parity parity, 
     int dataBits, StopBits stopBits)
{
    this.PortName = portName;
    this.BaudRate = baudRate;
    this.Parity = parity;
    this.DataBits = dataBits;
    this.StopBits = stopBits;
}

And here is those fields:

private int baudRate = defaultBaudRate;
private int dataBits = defaultDataBits;
private Parity parity = defaultParity;
private StopBits stopBits = defaultStopBits;
private string portName = defaultPortName;

这篇关于未初始化的属性是否为默认值,因为它们在使用默认构造函数时?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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