字段初始化程序无法引用非静态字段方法或属性Error [英] a field initializer cannot reference the nonstatic field method or property Error

查看:128
本文介绍了字段初始化程序无法引用非静态字段方法或属性Error的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用.Net C#和Azure blob存储

I am trying to work with .Net C# and Azure blob storage

我遵循Microsoft的文档以访问Blob表.

I follow Microsoft's documentation in order to access a blob table.

using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Table;
using System.Threading.Tasks;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace WebApplication1.Controllers
{
    public class EmailAdress
    {
        CloudStorageAccount storageAccount = new CloudStorageAccount(
            new Microsoft.WindowsAzure.Storage.Auth.StorageCredentials(
                "experimentstables", "token"), true);

        // Create the table client.
        CloudTableClient tableClient = storageAccount.CreateCloudTableClient();

        // Get a reference to a table named "peopleTable"
        CloudTable pexperimentsEmailAddresses = tableClient.GetTableReference("experimentsEmailAddresses");
    }
}

在此行

CloudTableClient tableClient = storageAccount.CreateCloudTableClient();

storageAccount被标记为红色,并显示以下错误:

storageAccount is marked red with the following error:

字段初始化器无法引用非静态字段方法或属性

a field initializer cannot reference the nonstatic field method or property

我应该如何解决?

推荐答案

实例字段的变量初始化器无法引用正在创建的实例.因此,在变量初始值设定项中引用它是编译时错误,因为对于变量初始值设定项通过simple_name引用任何实例成员都是编译时错误.

A variable initializer for an instance field cannot reference the instance being created. Thus, it is a compile-time error to reference this in a variable initializer, as it is a compile-time error for a variable initializer to reference any instance member through a simple_name.

您只能在构造函数中相对于另一个字段初始化一个字段.

You can initialize a field with respect to another field only in a constructor.

将不会编译:

class A
{
    int x = 1;
    int y = x + 1;        // Error, reference to instance member of this
}

将编译:

class A
{
    public A() 
    {
        int x = 1;
        int y = x + 1;        // Works just fine
    }
}

这篇关于字段初始化程序无法引用非静态字段方法或属性Error的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆