Welcome to C#

The following are the topics to be covered in this article:

  • Welcome to C#
  • Working with variables, operators and expressions
  • Understanding your first C# program

Welcome to C#

C# is a powerful language that is generally called a Component Oriented Language. Now we have heard of Object Oriented Languages, but what is a Component Oriented Language? Though the difference is unclear and thin, the reason why this new concept has become associated with C# is that a new technique was to be introduced that would develop software combining some pre-existing features (OOP) and welcoming some new components. Some of the components are:

  • Properties
  • Methods
  • Events
  • Attributes (Metadata-Data about Data)
  • Assemblies/Packages

Another major characteristic is the introduction of Separation Of Concerns (SOC) and the Service Oriented Architecture (SOA). SOC is Separation Of Concerns via partial classes in C#.

Versions in C#

The following  are the versions so far. The data is from C# Indepth. Please visit or follow the book to learn more about the various versions.

  • C# version 1

  • C# version 2: As said previously, in this version, Generics, Nullable types(), anonynous types and Iterators(blocks) were introduced.

  • C# version 3: As said previously, implicit typing, object and collection initializers, anonymous types, automatic properties, lambda expressions, extension methods, query expressions were introduced.

  • C# version 4: As said previously, dynamic typing, optional parameters, named arguments and generic variance were introduced.

  • C# version 5: As said previously, asynchronous functions, caller info attributes, a tweak to foreach (one of the examples is Parallel.Foreach (with Lambda expression)) and iteration variable capture were introduced. Caller info attributes is a very new concept and an interesting one too. These attributes track information about the calling methods/properties and many more.

A small incident to share, I always wondered why C Sharp?? Is it a kind of successor to C++ or what? I always wondered and I feel many beginner developers would be wondering. Thanks to Wiki the datawarehouse for every bit of concept in the world for letting me understand why? Without any delay, straight from the WIKI,

The name "C sharp" was inspired by musical notation where a sharp indicates that the written note should be made a semitone higher in pitch. The sharp symbol also resembles a ligature of four "+" symbols that thus implies that it is an increment of C++.

Now let's get along and start learning from the basics.

Camel and Pascal Case Notations

Every time a developer gets into these concepts, but sometimes beginners like me would wonder what these actually are. Camel case is used for naming fields whereas Pascal case is used for naming properties and function names. In Pascal case, the starting letters in the multiword name are capitalized whereas in Camel case except the very first letter is all capitals as in the following:

  • Pascal Case: GetSumFromNumbers.
  • Camel Case: getFirstNumber.

Working with Variables, Operators and Expressions

Before getting into the variables, let's get into what an identifier is. Identifiers are the names that are used to identify elements in the program. In C#, there are the following conventions to be followed:

  • Only letters are allowed (it may be uppercase or lowercase), digits and underscore(_) characters are allowed.

  • An identifier should always start with a letter.

For example, _getSum, GetSum, Get_sum are valid identifiers. Just to remember or keep in mind everytime that C# is case sensitive so getSum and GetSum are different and provide different meanings. Keywords, there are many keywords that are predefined in C#, for more info on the keywords, use the following link: C# keywords Now let's get back to our topic of discussion, variables. A variable is a location with memory or storage location that stores/holds a value. A variable in a program holds a unique name, its like madatory to provide it a unique name. A variable holds temporary information. A variable is used to get the value that it holds after assigning. In the following I specify the naming conventions to be followed when declaring variables specified by the Microsoft .NET team:

  • Underscores are advised not to be used.

  • Variables names that differ only in capitalization should be avoided. Like, abcTest or AbcTest used in the same file and at the same time. This would also mean identifiers with the same name and different case should be avoided.

  • It is advised to start the name with a lowercase. This would be consistent throughout since it would lead to easy mantenance.

  • Camel case notations should be used, for example, abcTest, myVariable, as such.

Declaring Variables is easy and simple. Usually the keyword var is used when the type is to be implicitly decided. If any datatype is already used during the declaration of the vriable, then it is explicitly typed.

  1. var setAge; //implicitly typed  
  2. int setAge; //explicitly typed  
  3. setAge = 45; //values assigned 

As in the preceding peice of snippet, you can see the statement ends with a semicolon ";" that simply marks the end so the compiler knows that the statement ends there. The Equals operator is used to assign the value to the declared variable. C# also uses the same the operators that we as developers have been using in other programming languages (+(plus), -(subtraction), *(asterix/multiplication), /(divide) and also the modulo operator(%)). These are, as we know, called the arithmetic operators. We cannot apply the arithmetic operations on datatypes except int (integer type) in a similar way.

  1. int a = 4 + 3;  
  2. string b = "4"+"3";  
  3. Console.Writeline(a);//Prints 7  
  4. Console.Writeline(b);//Prints 43 not 7 

However using explicit conversion, a string can be converted into integers. For incrementing the values, the increment and decrement operators are also strongly followed in C#.

  1. count= count + 1; //Can be incremented but should not be written like this  
  2. count++; //This is how increment should be done  
  3.   
  4. count = count - 1;  
  5. count = count--; 

Understand your first C# program

As we all know, every program follows a common rule that is Input, Process and Output. Let's have a go at it.

  1. using System;  
  2. namespace ConsoleDemoApplication  
  3. {  
  4.     class Program  
  5.     {  
  6.         static void Main(string[] args)  
  7.         {  
  8.             string firstName; //Variable for storing string value  
  9.             string lastName;  
  10.   
  11.             //Displaying message for entering value  
  12.             Console.WriteLine("Please Enter Your First Name");  
  13.             //Accepting and holding values in name variable  
  14.             firstName = Console.ReadLine();  
  15.   
  16.             Console.WriteLine("Please Enter Your Last Name");  
  17.             lastName = Console.ReadLine();  
  18.   
  19.             //Displaying Output  
  20.             //{0}{1} take the variable names that are assigned during the runtime by the user  
  21.             Console.WriteLine("Welcome {0} {1} in your first C-sharp program", firstName,lastName);  
  22.   
  23.             //Holding console screen  
  24.             Console.ReadLine();   
  25.         }  
  26.     }  

When we run the preceding program, we get the following output. I am showing the output step wise, so that you get to understand the importance of the Console.WriteLine and Console.ReadLine.

console

As we see in the image, the console window opens when the project runs and asks for the first name. As we see in the program, first comes Console.WriteLine(), then when the user enters the name Console.ReadLine(), it plays its role and stores it in the variable memory firstName.

cmd

As we see in the image, the console window opens when the project runs and asks for the last name. As we see in the program, first comes Console.WriteLine(), then when the user enters the name Console.ReadLine(), plays its role and stores it in the variable memory lastName.

run

Now when a final Enter is pressed by the user trying to understand what the program is doing. Yes, it gives the desired output. It concats the variables where the names/values entered by the user are temporarily stored in memory. {0} {1}, this actually contains the values entered by the user when asked and as said previously concats the input and displays the full name as the output. This could also have been done by using the "+" operator like:

  1. string fullName = firstName + " " + lastName; //+ operator concats the strings  
  2. Console.WriteLine("Welcome {0} in your first C-sharp program", fullName); 

There are many libraries that can be used in your program. These libraries need to be specified at the top of your program, in other words where the Namespaces are and they are used using a using keyword. :D Yes, so many use! When on the console window, something needs to be displayed, Console.WriteLine() is used and when we need to take the Input from the user and store it in memory, Console.ReadLine() is used. This is a simple and basic difference. The {0} used in our program, acts as a simple placeholder where the dynamic values are used by specifying the argument at the end. The many the arguments, the many the placeholders to be used.

Conclusion

Thanks guys for having patience and reading through this. This is what I could cover in the first part and will continue some more interesting topics in the second part. Let's learn, let's explore and let's share.

Any suggestions and corrctions are humbly accepted as we all keep learning at every step. Follow the C# 6 New Features for more info on the future version, C# 6.

Refrences