C# class field
In this chapter you will learn:
- What is a class field
- Syntax to create a new field
- A class with fields
- How to declare more than one fields
Description
A field is a variable that is a member of a class or struct.
Syntax
The field definition has the following syntax.
class className{/*from w ww. j av a2 s . c om*/
fieldAccessor fieldType fieldName;
}
fieldAccessor
specifies the access,fieldType
specifies the type of variable, andfieldName
is the variable's name.
Example
A class with fields
class Person //from w ww . j a v a 2 s . co m
{
string name;
public int Age = 10;
}
Example 2
For the fields with the same type we can declare them together.
class Rectangle{
int Width, Height;
}
They can share the same modifiers as well.
class Rectangle{
public int Width, Height;
}
Next chapter...
What you will learn in the next chapter: