Member '' cannot be accessed with an instance reference

前端 未结 10 1713
我在风中等你
我在风中等你 2020-11-22 10:39

I am getting into C# and I am having this issue:

namespace MyDataLayer
{
    namespace Section1
    {
        public class MyClass
        {
            publ         


        
10条回答
  •  死守一世寂寞
    2020-11-22 11:10

    No need to use static in this case as thoroughly explained. You might as well initialise your property without GetItem() method, example of both below:

    namespace MyNamespace
    {
        using System;
    
        public class MyType
        {
            public string MyProperty { get; set; } = new string();
            public static string MyStatic { get; set; } = "I'm static";
        }
    }
    

    Consuming:

    using MyType;
    
    public class Somewhere 
    {
        public void Consuming(){
    
            // through instance of your type
            var myObject = new MyType(); 
            var alpha = myObject.MyProperty;
    
            // through your type 
            var beta = MyType.MyStatic;
        }
    }       
    

提交回复
热议问题