Developed by Microsoft, C# Programming is one of the most popular programming languages. Its popularity stems from its ease of use and robust features. Garbage collection reclaims memory occupied by unused objects, exception handling provides a structured error recovery, and Lambda expressions support functional programming.
C# is especially useful for developing Windows applications, games using the Unity engine, and mobile apps using cross-platform technology such as Xamarin. The language’s versatility also makes it a great choice for Virtual Reality development.
Types
A type specifies the size and kind of data a variable can hold. It’s important to understand the different types in C#, as they are an essential part of function signatures and can affect how a function functions. There are several predefined data types in C#, including integer, boolean, and float. Other types can be created by the user. These include struct, class, enumeration, and record types.
C# is a strongly-typed language, which means that variables must be declared with their data type. In addition, you must specify the data type of a variable when initializing it. If you don’t, the compiler will generate a compilation error.
Numeric values can be stored in integer types (short, int, long), float, and decimal. A float can represent any finite number between 0.0 and 1.0, a decimal can represent numbers with up to 10 digits, and a double can represent values with up to two decimal points. The float and decimal types also support denormalized floating-point values.
Variables
Variables are a way to store reusable data in computer memory. They are identified by a name and assigned a value. These values are then accessed in the program by using the variable name. This is similar to the concept of a LEGO block, where you can move and change the value of a specific piece of code without changing other pieces.
A variable can be of any built-in data type or a custom one. The identifier of the variable must be a valid character and start with either a letter or an underscore (_). It cannot begin with a digit or end with a slash (
Variables are usually declared in the global scope but can also be local to a function, block, foreach statement, or a’switch statement’. Local variables can be declared with the var keyword, which allows the compiler to infer the type of the variable. However, it is generally best to declare the variable type explicitly to make sure that others who maintain your code will understand its meaning.
Arrays
Arrays are a data structure that holds multiple values of the same type in a single variable. These values are accessed by an integer index. Arrays can be Single-Dimensional or Multi-Dimensional. Arrays are different from Lists and Dictionaries, which store heterogeneous data elements. Unlike lists, which have dynamic memory and must be resized when items are added or removed, arrays have static memory.
Generally, an array’s lower bound is 0, and its upper bound is one less than the array’s length. If you use an index that is greater than the upper bound, you will receive an IndexOutOfRangeException.
The Array class has a number of static methods that help you manage arrays. For example, the Clear method removes all items from an array and sets its length to zero. The Sort method has overloaded forms, but the simplest form takes as its parameters an array object, d, and the number of items to sort. For example, the code listed in Listing 8 sorts two items starting at index 1.
Methods
Methods in C# are a series of statements that work together to perform a task. They are used to organize code, reduce memory usage, and increase the readability of a program. The Main method is a prerequisite in every C# program, and it is the first method that is called when the program begins to run.
A method signature includes a method name and a list of parameters. A method can have multiple input parameters and can return any type of data, including primitive types (int, float, and bool) or custom objects. The return type can also be void, which means the method does not return any value.
The method body is the code logic of a method, and it is enclosed in curly braces. Its name must be unique, and its naming convention should reflect the action that it performs. It should also include a Pascal case letter, a numeric digit, and an underscore character. Adding the m_ prefix is optional, but it is a standard practice to identify member variables in a class.
Objects
Objects and classes are the core concepts of object-oriented programming (OOP) in C#. They enable encapsulation, abstraction, and code reuse, making your software more modular and maintainable. They also promote code organization and scalability by providing a way to model real-world entities.
A class is a blueprint from which individual objects are created. A class can include fields, properties, methods, delegates, and events. These members can have access modifiers like public, private, protected, and internal to control accessibility. In addition, a class can contain static member functions that are not visible outside of the class.
A new class is created using the keyword class followed by a name and curly braces. You can then create an instance of that class using the new keyword. You can then access its member variables and methods. You can even create multiple instances of the same class and assign them to different variables. Each instance will have different values for its fields and properties but will behave the same. This is known as polymorphism. Inheritance is a powerful feature of classes that enables you to add and extend functionality by establishing hierarchical relationships between different classes.