简体   繁体   English

声明C#结构的对象

[英]Declaring an object of C# struct

I'm very new to C#. 我是C#的新手。 Coming from pure C/C++ background. 来自纯C / C ++背景。 Hence please go easy on me, if my question is basic. 因此,如果我的问题很简单,请对我轻松一点。

I'm trying to declare an object of struct Abc which is complaining a compilation error as below which means that, the object obj is NOT recognized. 我正在尝试声明一个结构Abc的对象,该对象抱怨编译错误,如下所示,这意味着该对象obj无法识别。

  string mains1 = obj.s1; 

error CS0120: An object reference is required for the non-static field, method, or property 'csharp1.Program.obj' 错误CS0120:非静态字段,方法或属性'csharp1.Program.obj'需要对象引用

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace csharp1
{
    class Program
    {
        public struct Abc
        {
            public string s1;
        };

        Abc obj = new Abc();;

        static void Main(string[] args)
        {
            System.Console.WriteLine("start main");
            string cwd = Directory.GetCurrentDirectory();
            string fileName = "Myfile.xml";
            string destFile = System.IO.Path.Combine(cwd, fileName);
            string inpFile = "MyfileINP.xml";
            string inpDir = System.IO.Path.Combine(cwd, "SubDir");
            string srcfile = System.IO.Path.Combine(inpDir,inpFile);
            File.Copy(srcfile, destFile, false);
            string mains1 = obj.s1;
        }
    }
}

Not sure what is the error here. 不知道这里是什么错误。

This is complaining because you are trying to access a non static instance "obj" from a static context the Main method so there are multiple ways to resolve it you can make that instance static too like "static Abc obj=new Abc()" or you can shift that declaration inside Main method or you can create an instance of Program class and then use that instance. 这是在抱怨,因为您正尝试从静态上下文Main方法访问非静态实例“ obj”,所以有多种方法可以解决该问题,您可以使该实例也像“ static Abc obj = new Abc()”一样静态或您可以在Main方法中移动该声明,也可以创建Program类的实例,然后使用该实例。

METHOD 1: 方法1:

            static  Abc obj = new Abc();

            static void Main(string[] args)
            {
                System.Console.WriteLine("start main");
               //
                string mains1 = obj.s1;
            }

METHOD 2: 方法2:

 static void Main(string[] args)
        {
            Abc obj = new Abc();
            System.Console.WriteLine("start main");
           //
            string mains1 = obj.s1;
        }

METHOD 3: 方法3:

Abc obj = new Abc();

        static void Main(string[] args)
        {
            System.Console.WriteLine("start main");
            //
            string mains1 = new Program().obj.s1;
        }

Its always not a good practice to make "static" everywhere until there is no other option you have, making static allow this object to be shared across all the instances of this class so its not good thing until we really need that. 在没有其他选择之前,始终到处都是“静态”不是一个好习惯,使static允许在该类的所有实例之间共享该对象,因此在我们真正需要它之前不是一件好事。

您需要先创建包含类Program的实例,然后才能访问它的成员,例如

string mains1 = new Program().obj.s1;

You are trying to access obj (a non-static method) from within Main (a static method). 您正在尝试从Main (静态方法)内部访问obj (非静态方法)。 Because of this, your are getting CS0120 . 因此,您正在获得CS0120 I advise changing Abc obj = new Abc() to static ABC obj = new Abc() . 我建议将Abc obj = new Abc()更改为static ABC obj = new Abc()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM