friends i have problem with using get or set in class in c# when i use get or set in gives error(invalid token { in class) pls, see below code,i have this problem in it
There are two errors in your code.
Try this instead:
namespace ConsoleApplication2
{
class Program
{
class Car
{
private int _speed;
public int Speed // <-- no semicolon here.
{
get
{
return _speed; // <-- here
}
}
}
}
}
I noticed that the code you originally posted was formatted badly. I would suggest that you format your document automatically in Visual Studio to make the braces line up. This should make the error more obvious. When the formatting of the code looks wrong you know that there is an error nearby. You can find this option in the menu: Edit -> Advanced -> Format Document or use the keyboard shortcut (Ctrl-E D for me, but might be different for you, depending on your settings).
I would also suggest that you consider using auto-implemented properties instead of writing the getter out in full:
namespace ConsoleApplication2
{
class Program
{
class Car
{
public int Speed { get; private set; }
}
}
}