给出以下代码:
public static class Helpers
{
private static Char[] myChars = new Char[] {'a', 'b'};
private static Int32 myCharsSize = myChars.Length;
}
Run Code Online (Sandbox Code Playgroud)
是否保证在我使用其长度分配之前myChars将其初始化?myCharsSize
我正在登录一个网站并尝试获取会话和cookie,我的cookie容器在我的方法之外,但是我无法获取cookie并将其发送给我的请求,我尝试使用处理程序的方式与我的cookie容器,但我收到一个错误
字段初始值设定项不能引用非静态字段c#
这就是我想要做的事情
private CookieContainer cookieContainer = new CookieContainer();
private HttpClientHandler clienthandler = new HttpClientHandler { AllowAutoRedirect = true, UseCookies = true, cookieContainer };
private HttpClient client = new HttpClient(clienthandler);
Run Code Online (Sandbox Code Playgroud)
那么我怎样才能使用处理程序,以便我可以设置会话并发送会话?谢谢.
我一直在浏览我正在开发的应用程序,它大量使用不变性.我刚刚发现只有getter的自动属性在C#6.0中,所以我正在重构使用它们.我打了一个可能的问号,这是我通过公共属性公开private IList<T>对象的地方ReadOnlyCollection<T>,以避免它们被转换回原始List<T>对象的可能性,例如
private IList<string> tags = new List<string>();
public IEnumerable<string> Tags => new ReadOnlyCollection<string>(this.tags);
有没有办法在这种类型的自定义getter中使用自动属性?
C# 和 OOP 的新手。我正在尝试使用一些信息初始化创建一个名为“Human”的类。但是我收到以下错误a field initializer cannot reference the nonstatic field method or property。尝试创建变量时,错误消息指向first_name和last_namefull_name
这是“简单”的代码
namespace World
{
public class Human
{
// Personal traits
public string first_name;
public string last_name;
public string full_name = first_name + " " + last_name;
}
}
Run Code Online (Sandbox Code Playgroud)
我到底做错了什么?我不明白。。
我有一个接口 Service,它有一个方法 GoTo
public interface Service
{
bool GoTo(string NextPage , object parameter);
}
Run Code Online (Sandbox Code Playgroud)
我有一个A类如下
public class A
{
private Service serviceA;
//constructor of the class
public A(Service serviceB, ServiceManager serviceManager)
{
this.serviceA = serviceB;
}
public ObservableCollection<SampleObject> Example { get; } = new
ObservableCollection<SampleObject>()
{
new SampleObject() { Action= new DelegateCommand(() => this.serviceA.GoTo("NextPage", null))
},
};
Run Code Online (Sandbox Code Playgroud)
}
我收到一个错误,说关键字“this”在当前上下文中不可用。我查看了字段初始值设定项无法引用非静态字段、方法或属性,我可以看到该错误是由于变量未在构造函数中初始化,但在我的情况下,我这样做了。任何帮助理解这一点更好的帮助将不胜感激?
我正在尝试用天然卫星制造太阳系.目前我正在绘制一个相对于静态"太阳"类对象的行星,但我想让这个类使用另一个行星物体的位置并相对于该行星绘制.要做到这一点,我需要提取该行星对象的x和y位置.
这是我用于绘制行星的类的类构造函数.
Nebesko_Telo merkur = new Nebesko_Telo(Sun.x, Sun.y, 4, 12, 4.090909090909091,
1, 255, 255, 255);
// A field initializer cannot reference the non-static field, method,
// or property 'Form1.merkur'.
Nebesko_Telo venera = new Nebesko_Telo(merkur.x, merkur.y, 1, 23, 1.5, 1, 176, 108, 32);
Run Code Online (Sandbox Code Playgroud)
这是类构造函数.
public Nebesko_Telo(doubl _rel_tel_x, double _rel_tel_y, double _r, double _or,
double _Fi_mult, double _tilt_plant_nat, int _re, int_gr, int _bl) {
r = _r;
or = _or;
Fi_mult = _Fi_mult;
re = _re;
gr = _gr;
bl = _bl; …Run Code Online (Sandbox Code Playgroud)