Marek Marek - 1 month ago 24
C# Question

error CS0236: A field initializer cannot reference the non-static field, method, or property

I have a problem with error CS0236.

public class Converter
{
public string Celsiusz { get; set; }
public string Fahrenheit { get; set; }
public string Kelvin{ get; set; }
public string Rankinen { get; set; }
public string Reaumur { get; set; }
public string Romer { get; set; }
public string Delisle { get; set; }
public string Newton { get; set; }


double CelsiuszDouble;

bool bupa = double.TryParse(Newton, out CelsiuszDouble);
}


Are coming two errors:

First:


error CS0236: A field initializer cannot reference the non-static
field, method, or property Converter.Newton


Second:


error CS0236: A field initializer cannot reference the non-static
field, method, or property Converter.CelsiuszDouble

Answer

I agree with @David You cant write such a code in class definition . You must write that part bool=.... inside either Constructor or a Method.