Basic of C#

Introduction

First things first, just as we use a language to communicate with each other, like English, Hindi, etc. we have to use a language to communicate with computers also. By communicating, what I mean is that if we have to tell the computer to do something, we need to use a language, Any computer language has some keywords and predefined syntax which we need to follow to communicate with computers..

Identifiers

In programming languages, An identifier is a name used to identify a class, variable, function, or any other user-defined item. Let’s understand with previous “Hello” example:

using System;

namespace MyFirstApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello");
            Console.ReadLine();
        }
    }
}

In above example we have four identifiers

MyFirstApplication – Namespace in which we defined our class
Program – Class name
Main – Method name
Args – parameter variable name

Rules for defining identifiers in C#

To define identifiers C# has some rules, if we break any rule will get a compile-time error in Visual Studio.

  • The first character in an identifier cannot be a digit.
  • A name must begin with a letter that could be followed by a sequence of letters, digits (0 – 9) or underscore.
  • It must not contain any embedded space or symbol such as? – + ! @ # % ^ & * ( ) [ ] { } . ; : ” ‘ / and . However, we can make use of an underscore ( _ ).
  • It should not be a C# keyword.
  • Identifiers should not contain white spaces.

Keyword

C# contains reserved identifiers, reserved words that have special meaning to the compiler which is used to perform some predefined actions. These words are called keywords.

C# has a total of 104 keywords. All these keywords are in lowercase. Here is a complete list of all C# keywords. Below mentioned is the list of all the keywords that are used in C# in ascending order:

TypeKeywords
Namespace Keywordsusing, operator, extern
Access Keywordsthis, base
Access Modifier Keywordspublic, private, protected, internal
Contextual Keywordsadd, var, dynamic, global, set, value
Type Keywordsbool, byte, char, class, decimal, double, enum, float, int, long, sbyte, short, string, struct, uint, ulong, ushort
Method Parameter Keywordparams, ref, out
Modifier Keywordsabstract, async, const, event, extern, new, override, partial, readonly, sealed, static, unsafe, virtual, volatile
Literal Keywordsnull, false, true, value, void
Operator Keywordsas, await, is, new, sizeof, typeof, stackalloc, checked, unchecked
Query Keywordsfrom, where, select, group, into, orderby, join, let, in, on, equals, by, ascending, descending
Statement Keywordsif, else, switch, case, do, for, foreach, in, while, break, continue, default, goto, return, yield, throw, try, catch, finally, checked, unchecked, fixed, lock

We will see these keywords in action in the following tutorial series, for now just to be aware that there are some restricted words that can be used only in a specific way. Additionally, Visual Studio is smart enough to highlight such words in a particular color, so that we do not have to remember them.


Syntax

In simple terms, if we want to give instructions to a computer, to perform some tasks, we just cannot go ahead and write those instructions like “computer, perform the addition of 8 and 5”. Though this would have been very good, had this been the case, however, based on the language you are using to write the program, we have to follow certain rules to give instructions to the computer to perform the tasks we want it to do. These rules are the syntax of that language.

For example: C# is case sensitive language, so if you want to use any keyword we have to write into small letter


Literals

Any hard coadded, fixed value can be called as Literals
For example, in below code ii is a variable, and 50 is literal.so when we refer ii its value is 50.

int ii = 50;

Types of Literals

  • Boolean Literal
  • Integer Literal
  • Character Literal
  • String Literal
  • Null Literal

Boolean Literal

Boolean literal either be true or false, it just has two values. So if defined Boolean literal that can be either true or false.

Integer Literal

Integer literals are used to write values of types int, uint, long, and ulong. Integer literals can be octal, decimal or hexadecimal constant.

50 //decimal
0x143f //hexadecimal
071 //octal

Character Literal

A character literal represents a single character and usually consists of a character in quotes.

MeaningEscape Sequence
Backslash\\
Backspace\b
Carriage return\r
Double quote\”
Form feed\f
Line feed\n
Null character1\0
Single quote\’
Tab\t
Unicode character\u

String Literal

Literals which are enclosed in double-quotes (“”) are known as String Literal

string str1 = "string, literal"; // string, literal
string str2 = @"string, literal"; // string, literal
string str3 = "string \n literal"; // string
                                    literal

Null Literal

Null Literal is literal that signifies the null type
For example

string str =null;

Variables

Variables play an important role in computer programming because they enable programmers to write flexible programs. Rather than entering data directly into a program, a programmer can use variables to represent the data. Then, when the program is executed, the variables are replaced with real data. This makes it possible for the same program to process different sets of data.
Every variable has a name, called the variable name, and a data type. A variable’s data type indicates what sort of value the variable represents, such as whether it is an integer, a floating-point number, or a character.

So the variable is like a container which stores the data
Let us see our last example: From our “hello” program lets accept the user name and print it on the console. When user enter the value on console we need to store it somewhere in the program so we can define the variable which holds that value and later we can use the same for display purpose

using System;

namespace MyFirstApplication
{
    class Program
    {
        static void Main(string[] args)
        {

            Console.WriteLine("Enter a name");
            string name = Console.ReadLine();
            Console.WriteLine($"Hello {name}");
            Console.ReadLine();
        }
    }
}


In the above example, we defined a variable called “name” which can hold the value entered by the user

string name = Console.ReadLine();

Notice: as we need to store text value hence, we used string type, C# is the strongly typed language. It means we can assign a value of the specified data type. we cannot assign a string value to integer type or vice-versa.


Leave a Reply

Your email address will not be published. Required fields are marked *