从Main()调用方法

时间:2017-01-24 15:39:40

标签: c#

我不知道我不能从Main()调用方法的错误。 我不知道它是Visual Studio中的错误还是我做错了什么

这是代码:

using System;
using System.Drawing;
using System.Security.Claims;
using Csharp_Projects.Constructs;

namespace Csharp_Projects
{
    static class Program
    {
        static void Main(string[] args)
        {
            ValueTypeContainingRefType();
        }

        class ShapeInfo
        {
            public string infoString;

            public ShapeInfo(string info)
            {
                infoString = info;
            }

            struct Rectangle
            {
                public ShapeInfo rectInfo;

                public int recTop, rectleft, rectBottom, rectRight;

                public Rectangle(string info, int top, int left, int Buttom, int Right)
                {
                    rectInfo = new ShapeInfo(info);
                    recTop = top;
                    rectBottom = Buttom;
                    rectRight = Right;
                    rectleft = left;
                }

                public void Display()
                {
                    Console.WriteLine("string={0},top={1},Bottom={2},"+"left={3},Right={4}",rectInfo,recTop,rectBottom,rectRight,rectleft);
                }
                public static void ValueTypeContainingRefType()
                {
                    Console.WriteLine("Creating r1");
                    Rectangle r1 = new Rectangle("First Rec", 10, 10, 50, 50);
                    Console.WriteLine("Assigning r2 to r1");
                    Rectangle r2 = r1;
                    Console.WriteLine("Change Values of r2");
                    r2.rectInfo.infoString = "This is new info!";
                    r2.rectBottom = 222;
                    r1.Display();
                    r2.Display();
                }
            }
        }
    }
}

错误消息是:

  

名称" ValueTypeContainingRefType"在当前上下文中不存在。

由于方法名称为ValueTypeContainingRefType

,因此绝对无稽之谈

1 个答案:

答案 0 :(得分:3)

您的Rectangle结构拥有该方法。哪个嵌套在ShapeInfo中。试试这个:

static void Main(string[] args)
{
    ShapeInfo.Rectangle.ValueTypeContainingRefType();
}

您还必须公开Rectangle