The following code shows the syntax for declaring a variable inside a method.
«const» type«[]» name «= value»;
The following list describes the pieces of this declaration.
const
—If you include this, the variable is a constant and its value cannot be changed later. Use the value to assign the constant a value.[]
—Include empty square brackets [] to make an array.=
value—The value you want the variable to initially have.C# enables you to declare and initialize more than one variable in a single declaration statement, but this can make the code more difficult to read.
The following code shows the syntax for declaring a variable outside of any method (at the class level).
«attributes» «accessibility»
«const | readonly | static | volatile | static volatile»
type«[]» name «= value»
The following list describes the pieces of this declaration.
public
, internal
, protected
, internal protected
, or private
. The default is private
.const
—If you include this, the variable is a constant and its value cannot be changed later. Use the value to assign the constant a value.readonly
—If you include this, the variable is similar to a constant except its value can be set either with a value clause or in the class’s constructor.static
—This keyword indicates the variable is shared by all instances of the class.volatile
—This keyword indicates the variable might be modified by code running in multiple threads running at the same time.[]
—Include empty square brackets [] to make an array.=
value—The value you want the variable to initially have.Initialization expressions assign a value to a new variable. Simple expressions assign a literal value to a simple data type. The following example sets the value of a new string variable.
string txt = "Test";
The assignment expression can also initialize a variable to the result of a method or constructor, as in the following example.
Person person = new Person("Rod", "Stephens"); // Constructor.
int numTools = CountTools(); // Method.
An initialization expression for an object can specify values for the object’s public properties as in the following example, which sets the object’s FirstName
and LastName
properties.
Person rod = new Person() { FirstName = "Rod", LastName = "Stephens" };
To create an array of a certain size, set it equal to a new array of the required type with the number of items in its dimensions inside brackets. For example, the following code creates an array that holds 10 entries with indices 0 through 9.
decimal[] salaries = new decimal[10];
To initialize a one-dimensional array, put the array’s values inside braces separated by commas, as in the following code.
int[] fibonacci = { 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89 };
To initialize higher-dimensional arrays, place lower-dimensional array values inside braces and separate them with commas, as in the following example, which initializes a two-dimensional array.
int[,] values =
{
{1, 2, 3},
{4, 5, 6}
};
To initialize a collection class that provides an Add
method, create a new instance of the class followed by initial values enclosed in braces, as in the following code.
List<string> pies = new List<string>()
{
"Apple", "Banana", "Cherry", "Coconut Cream"
};
If the collection’s Add
method takes more than one parameter, group parameters in brackets, as in the following example.
Dictionary<string, string> directory = new Dictionary<string, string>()
{
{"Alice Artz", "940-283-1298"},
{"Bill Bland", "940-237-3827"},
{"Carla Careful", "940-237-1983"}
};
To make it easy to call an object’s Dispose
method, you can declare the object inside a using
statement. When the code reaches the end of the using
block, the program automatically calls the object’s Dispose
method.
For example, the following code creates a Graphics
variable named gr
that is automatically disposed when the using
block ends.
using (Graphics gr = Graphics.FromImage(bm))
{
...
}
The syntax for declaring an enumerated type is as follows.
«attributes0» «accessibility» enum name
«: type»
{
«attributes1» name1 «= value1»,
«attributes2» name2 «= value2»,
...
}
The pieces of this declaration are as follows.
:
type—All enumerations are stored internally as integer values. By default, an enumeration’s type is int
. You can use this part of the declaration to change the underlying type to byte
, sbyte
, short
, ushort
, int
, uint
, long
, or ulong
(any integer type except char
).3.145.7.208