Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/284.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从Main()调用方法_C# - Fatal编程技术网

C# 从Main()调用方法

C# 从Main()调用方法,c#,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(stri

我不知道我不能从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

您的矩形结构拥有该方法。它嵌套在ShapeInfo中。试试这个:

static void Main(string[] args)
{
    ShapeInfo.Rectangle.ValueTypeContainingRefType();
}
您还必须公开矩形。

您的矩形结构拥有该方法。它嵌套在ShapeInfo中。试试这个:

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

您还必须将矩形公开。

这是因为您尝试访问的方法位于另一个类中。您尝试调用的方法位于另一个类中的结构中,并且也是静态的。当您认为编译器正在做一些毫无意义的事情时,你错了99.999999…%的时间。你是说它从不出错?“从来没有?”莫森非常非常罕见。在编译器中遇到实际的错误之前,您会遇到成千上万的人将正确的行为报告为错误。这是因为您尝试访问的方法位于另一个类中。您尝试调用的方法位于另一个类中的结构中,并且也是静态的。当您认为编译器正在执行某些操作时那完全是胡说八道,你错了99.999999…%的时间。你是说它从不出错?“从来没有?”莫森非常非常罕见。在编译器中遇到实际的错误之前,您会遇到成千上万的人将正确的行为报告为错误。您无法访问矩形结构,因为它是private.True。我将把这一点补充到我的回答中。谢谢即使我将其设置为publicTry public struct Rectangle,也无法访问它。我刚刚用上面提到的两个修改编译了你的代码,它可以工作。你不能访问矩形结构,因为它是private.True。我将把这一点补充到我的回答中。谢谢即使我将其设置为publicTry public struct Rectangle,也无法访问它。我刚刚用上面提到的两个更改编译了您的代码,它可以正常工作。