C# class field

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{//w w w  . j  av  a 2s  . c o  m

  fieldAccessor  fieldType fieldName;

}
  • fieldAccessor specifies the access,
  • fieldType specifies the type of variable, and
  • fieldName is the variable's name.

Example

A class with fields


class Person //from   www . j  ava 2 s.com
{ 
   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;

}




















Home »
  C# Tutorial »
    Custom Types »




C# Class
C# Struct
C# Interface
C# Inheritance
C# Namespace
C# Object
C# Delegate
C# Lambda
C# Event
C# Enum
C# Attribute
C# Generics
C# Preprocessor