C# Basics Explained in Brief
C# is one of the most used programming languages today. It is based on the concepts of C++, and is very similar to Java. If you know either of these languages, you should be able to learn C# basics quite easily. You can then develop C# applications and run them on the currently popular .NET framework.
Classes, Members and Objects
The most basic building block of a C# code are classes and objects. Every class has members that define the data and behavior of the class. Objects are real instances of a class. You can give public or private access to the classes defined in your program. Private classes can have only one object instance, which helps to hide data from the rest of the code. You need to start and end a class definition with left and right curly brackets respectively; this forms the class body.
Some classes do not define objects, but define other classes instead. You can then define objects from the subclasses. This key concept of object-oriented programming is called abstraction. When a class has too many properties and you are only interested in some of them, loosely associate subclasses hide the interdependencies and reduce the complexity of the master class. Though it may sound complicated at first, you need to understand the concept of abstraction if you’re learning C#.
You will find the mention of methods in any C# tutorial. Besides classes, you have methods, and each name is followed by a pair of open and close brackets. These carry out specific functions and enable you to break down the code in a modular fashion for the sake of simplicity. Every C# code starts off with the Main method. This is a static method, and can thus be accessed only by classes, and not by objects.
All data is stored using variables, each having a name and type. The data that you can assign to a variable is limited by its data type. For example, integer value cannot be assigned to a string variable. The value of a variable may change during the execution of the program. If there is no value assigned, the default value is taken for that data type.
The other elements that holds data in a C# program are constants; these have fixed values that are set during initialisation. These are preceded by the keyword “const”. For example:
const int X = 5;
it is a good practice to write the names of constants in upper case for the sake of readability, although the compiler would process it the same even if it were written in lower case.
Like C and C++, C# also includes arrays. The difference here is that the square brackets that indicate the position of the array elements follow the type and not the array name or identifier. For example:
Note that this declaration is not enough to create an actual instance of the array. You can do this as follows:
int samplearray = new int;
If you want to assign values to this array, include them within curly braces right after int in the above statement.
Array indexing begins at zero. The size of an array is not fixed at the time it is defined; the number of elements within it can be changed anytime during the code. Square brackets following the array name allow you to access individual elements.
Top courses in C#
C# students also learn
Empower your team. Lead the industry.
Get a subscription to a library of online courses and digital learning tools for your organization with Udemy for Business.