You are trying to write execution code inside of a class.
Please close it in a method, or any other execution code block and it will be ok.
Following this article: https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/class
A class can contain declarations of the following members:
- Constructors
- Constants
- Fields
- Finalizers
- Methods
- Properties
- Indexers
- Operators
- Events
- Delegates
- Classes
- Interfaces
- Structs
I have added below some code to illustrate the usage from constructor, following the comments.
Hope this clarifies. Happy learning c#!
using System;
namespace LanguageFeatures.Models
{
public class Product
{
//field
private string name;
//property
public string Name
{
get { return name; }
set { name = value; }
}
public int Test { get; set; }
}
public class TestProperty
{
//constructor
public TestProperty()
{
Product p = new Product { Name = "asd", Test = 1 };
Console.WriteLine(p.Test.ToString());
}
}
}
2
solved Why i can not use reference variable?