小编Inf*_*hze的帖子

使用默认参数而不是默认构造函数调用构造函数

我想调用一个结构的构造函数,它具有所有参数的默认值。但是当我调用 MyRectangle 的无参数构造函数时,会调用一个未定义的构造函数。这是为什么?是否可以不调用 not 从我创建的构造函数?

using System;

namespace UebungClasses
{
    class Program
    {
        static void Main(string[] args)
        {
            MyRectangle sixRec = new MyRectangle(3, 2);
            MyRectangle oneRec = new MyRectangle();

            Console.WriteLine("area of six: " + sixRec.Area() + " area of one: " + oneRec.Area());
        }
    }

    public struct MyRectangle
    { 
        public MyRectangle(double w = 1, double l = 1)
        {
            width = w;
            length = l;
            Console.WriteLine("Width: " + width + " Lenght: " + length);
        }

        public double Area()
        { …
Run Code Online (Sandbox Code Playgroud)

c# struct

6
推荐指数
1
解决办法
382
查看次数

标签 统计

c# ×1

struct ×1