Fundamentals of Variables and Constants in C
In the realm of software development, grasping the usage of variables and constants is pivotal. This article aims to introduce the fundamental concepts associated with creating and utilizing these elements within the context of the C# language.
Variables: Data Custodians
Variables play a pivotal role in temporarily storing information during program execution. They ensure that data remains accessible while the program is running, within the scope in which they are declared.
In C#, each variable necessitates three crucial elements: an access modifier, a data type, and a unique name.
Rules for Variable Names
When naming variables in C#, it is crucial to adhere to specific rules:
- The variable name should commence with a letter or an underscore (_).
- Numerals are permissible within the name, while special characters are not.
- Reserved words of the language, such as "if," "for," "while," and "string," cannot be employed as variable names.
Here is an example of a variable declaration:
int age;
In the example above, "int" signifies the data type, and "age" is the variable name. If the modifier is not explicitly specified, the default is "private."
Modifiers: Controlling Access
Modifiers determine the accessibility of variables, exerting an influence on whether they can be accessed by classes outside of their scope. In C#, a spectrum of modifiers is at one's disposal, including:
- public: Unrestricted access.
- protected: Restricted access to classes derived from the class that declared the variable.
- internal: Limited access to the current assembly.
- protected internal: Restricted access to the current set and to types derived from the containing class.
- private: Constricted access exclusive to the class itself.
Data Types
C# is a strongly typed language, signifying that all data and variables must possess a designated data type. There are two principal categories of data types: "value types" and "reference types."
"Value Types"
"Value types" hoard data directly, while "reference types" stockpile references to objects. It is of paramount importance that the values allocated to variables harmonize with the declared type. Here are illustrations of "value types":
Data Type | Value Range |
---|---|
Byte | 0 to 255 |
Short | -32,768 to 32,767 |
Int | -2,147,483,648 to 2,147,483,647 |
Float | -3.402823e38 to 3.402823e38 |
Bool | true or false |
Char | Unicode character |
"Reference Types"
Variables of the "reference type" stash references to objects. A selection of familiar "reference types" encompasses:
Data Type | Description |
---|---|
String | Used for character sequences enclosed in double quotes. |
Class | Enables the creation of custom objects. |
Interface | Facilitates the definition of agreements between classes. |
Delegate | Represents references to methods. |
Object | A universal foundational type. |
Array | Customized for collections of data with a fixed size. |
Constants: Immutable Values
In scenarios where a value must remain immutable, such as a standard measurement, constants are a pragmatic choice. They bear a resemblance to variables, but their values remain unalterable post-declaration. Take this instance:
const double PI = 3.14159;
To declare a constant, employ the "const" keyword followed by the type and name of the constant.
This article has laid a robust foundation for apprehending variables and constants in C#. Numerous supplementary operations and facets associated with these concepts will be the focus of forthcoming articles. Proficiency in these rudiments is indispensable for C# software development.
Top comments (0)