C# Language Fundamentals
Topics
Overview of a C# Program: In addition to the basic elements that comprise a C# program, a developer needs to be aware of other .NET features such as commenting options and recommended naming conventions.
- Primitives: Primitives are the basic data types defined by the FCL to represent numbers, characters, and dates.
- Operators: C# uses traditional operator syntax to perform arithmetic and conditional operations.
- Program Flow Statements: Program flow can be controlled using if and switch statements for selection; and while, do, for, and foreach clauses for iteration.
- String: The string class supports the expected string operations: concatenation, extracting substrings, searching for instances of a character pattern, and both case sensitive and insensitive comparisons.
- Enums: An enumeration is a convenient way to assign descriptions that can be used to reference an underlying set of values.
- Using Arrays: Single- or multi-dimensional arrays of any type can be created in C#. After an array is created, the System.Array class can be used to sort and copy the array.
- Reference and Value Types: All types in .NET are either a value or reference type. It is important to understand the differences and how they can affect a program's performance.
2.1. The Layout of a C# Program
Figure 2-1 illustrates some of the basic features of a C# program.
Figure 2-1. Basic elements of a C# program![]()
The code in Figure 2-1 consists of a class MyApp that contains the program logic and a class Apparel that contains the data. The program creates an instance of Apparel and assigns it to myApparel. This object is then used to print the values of the class members FabType and Price to the console. The important features to note include the following:
Core Note
General C# Programming NotesCase Sensitivity
All variable and keywords are distinguished by case sensitivity. Replace class with Class in Figure 2-1 and the code will not compile.
Naming Conventions
The ECMA standard provides naming convention guidelines to be followed in your C# code. In addition to promoting consistency, following a strict naming policy can minimize errors related to case sensitivity that often result from undisciplined naming schemes. Table 2-1 summarizes some of the more important recommendations.
Note that the case of a name may be based on two capitalization schemes:
The rule of thumb is to use Pascal capitalization everywhere except with parameters and local variables.
Commenting a C# Program
The C# compiler supports three types of embedded comments: an XML version and the two single-line (//) and multi-line (/* */) comments familiar to most programmers:
// for a single line /* for one or more lines */ /// <remarks> XML comment describing a class </remarks>
An XML comment begins with three slashes (///) and usually contains XML tags that document a particular aspect of the code such as a structure, a class, or class member. The C# parser can expand the XML tags to provide additional information and export them to an external file for further processing.
The value of the XML comments lies in the fact that they can be exported to a separate XML file and then processed using standard XML parsing techniques. You must instruct the compiler to generate this file because it is not done by default.
The following line compiles the source code consoleapp.cs and creates an XML file consoleXML:
C:\> csc consoleapp.cs /doc:consoleXML.xml
|