Category: CSharp Tutorial

  • C# Features

    C# is an Object0oriented programming having many features. Some of the main features are explained briefly below, point by point.

    What are the Features of C++ programming Language?

    • Simple and easy to use
    • Modern and General-purpose
    • Type safe (security)
    • Object-Oriented language
    • C# is Interoperability, Scalable and Updatable
    • Component-Oriented and Structured Programming Language
    • Rich in Library and Fast in Speed

    Details on some main features of C#

    Simple and easy to use

    Being a modern language, C# is simple and easy to use programming language. It inherits many features which makes it more suitable to use such as, it is mention above that it is a structured language that is it breaks the problems into parts making it easy to understand. Not only that but C# is also rich in Library providing various function to use.


    Modern and General-purpose

    C# was created keeping the mind on the current trend, making it powerful to use in every developer platform. The use of C# makes it suitable for building interoperable, scalable, robust applications and yet very powerful.


    Type safe (security)

    Well, there is always a question of safety and C# provides it. It can only access to those elements or memory space on which it has granted the permission to execute. The unsafe casting such as converting double to boolean is restricted, the initialization of data-types and array are checked along with the overflow of types.


    Object-Oriented language

    Like java, C# is also an object-oriented programming language. The use of objects divides the programs into modules making it simpler to use which is not possible in Procedure-Oriented programming language. In POP, code grows as project size increases.

    Unlike java, C# uses Structure that is structs that allow the primitive data-types to be used as objects. OOP supports the concept of Inheritance, Polymorphism, Interfaces, Data Encapsulation.


    C# is Interoperability, Scalable and Updatable

    Interoperability enables you to preserve and take advantage of existing investments in unmanaged code. This process gives the ability to C# to do any actions as the native C++ can do and provide native support for the COM and windows based applications.

    Scalable and updatable are automatic in C#. If we wish to update then we need to replace the old files with the new ones. No need for lining the other library.


    Component-Oriented and Structured Programming Language

    C# supports component-oriented programming through the concepts of properties, methods, events, and attributes (or metadata). It is a process of developing an application by mixing the already exists and new components.

    C# is a structured language that simply refers to the breaking down of the programs into modules or parts using function so that it becomes easy to use, understand and can be easily modified.


    Rich in Library and Fast in Speed

    It is also mentioned above that C# has a rich Library as it is based on the .NET framework that has a huge library. It consists of various inbuilt functions and classes that increase the speed of development.

    The speed of C# to compile and execute is fast that provide fast development of applications.

    Hope this article was helpful for the learner and keep visiting simple2code.com for more article on programming.


  • C# – Introduction

    C# – Introduction

    C# is a modern, general-purpose, object-oriented programming language developed by Microsoft and approved by the European Computer Manufacturers Association (ECMA) and International Standards Organization (ISO).

    C# was developed by Anders Hejlsberg and his team during the development of the .Net Framework.
    It is designed for Common Language Infrastructure (CLI), which consists of the executable code and runtime environment that allows the use of various high-level languages on different computer platforms and architectures.

    This language is also used to develop Web Applications, apps, and also in game development.

    The below points are the reason why C# is widely used in various platform:

    • It is a modern, general-purpose programming language.
    • It is easy to learn and easily understandable.
    • It is an object-oriented language.
    • It is a component-oriented and structured language.
    • It can be compiled on a variety of computer platforms.
    • It is a part of .Net Framework through which memory management becomes easy.
    • C# is used in almost all the developer fields such as developing applications, games and more.

    Following is the list of few important features of C#:

    • Boolean Conditions
    • Automatic Garbage Collection
    • Standard Library
    • Assembly Versioning
    • Delegates and Events Management
    • Easy-to-use Generics
    • Simple Multithreading
    • LINQ and Lambda Expressions
    • Integration with Windows

    Some main features of C# in Details: Click Here


  • C# Variables (initialization, declaration, syntax and example)

    Variables are like a storage unit that stores the values of various data-types present in a C# programming language. The types are different in C# and accordingly, the memory sizes are allocated to those types to store data values.

    Consider an example where you have declared a variable of type int that means that the int variable will be allocated to a memory where it will integer values and some specific operation can be performed.

    And for operation, consider the variable int that you described and since it is an integer the operation like addition and subtraction, multiplication, or modulus is performed on those integer operands.

    C# consists of various types variables provided by its data-types:

    • int: It stores integers values such as 65 or -65.
    • double: It stores floating-point numbers such as 15.34 or -15.34.
    • char: It stores the character in a single-quotes such as ‘s‘.
    • string: It is for storing the string in double quotes such as “simple code
    • bool: It states two values: true and false.

    Like variables in C, C++, java or any other programming language, the variable is declared before use and can be modified at runtime within any section of the code depending upon its types.

    Syntax:

    data-type variable_name = value; 
    or
    data-type variable_name;

    How to declare variable in C#?

    It is simple to declare a variable and it must be declared before the use of that variable. You need to specify the data-type for the variable and also give it a unique name.

    //syntax
    <data-types> <variable_name>;

    As mentioned above data-types can be of any type: int, float, double, char, string or bool, according to the need.

    Let us see an example how you can declare it in a program.

    int number;
    char ch;
    double d;
    float flt;

    How to initialize a variable in C#?

    Initialization means giving the variable an initial value to start with. We can initialize the variable at the declaration part such as: int number = 45;.

    The syntax is simple, first, start with the data-types and unique name the same as a declaration and then assign it a value with equal sign ‘=’.

    //syntax
    <data-types> <variable_name> = <data_value>;

    The data value is always assigned at the right-hand sign of the assignment operator ‘=‘. Let us see some valid examples of how you can initialize a variable.

    int num1 = 45, num2 = 67;
    double pi = 3.14159;
    char ch = 's';

    In the above example as you can see the first line contains two initialization which is possible considering you ant the two variable n you program to be an integer type.


    User Inputs

    Sometimes in a program you need to the value from the user and store it in a variable and use those variable at run time in a programs.

    To accept the inputs from the user, C# provides a particular function named as ReadLine() that take the inputs and store it in a variable. It is present in the console class in the System.

    How to use it in a program:

    int number;
    number = Convert.ToInt32(Console.ReadLine());

    The above example specifies that the variable is declared and it takes the user input and places it in a number variable. Conversion of inputs to integer data-type is done by Convert.ToInt32 and acceptance of input is completed by Console.ReadLine() in the program.


    C# defines two kinds of expressions:

    • lvalue: The expression may appear on either on the right-hand side or the left-hand side of the assignment operator.
    • rvalue: The expression can only appear on the right-hand side but not on the left-hand side of the assignment operator.

    Variables are lvalue expression as it can appear on the right-hand side as well as on the left-hand side of an equal sign’=’ in a program. However numeric values are rvalues as it can only be assigned to the right-hand side of the ‘=’ value.

    This is because the assignment operator(=) always assigns the right-hand side value to the left-hand side and we cannot assign something to a numeric value that is why they are the rvalue.

    Look at the below example that shows which are valid and which are not.

    int num1, num2; //variable declaration
    num1 = 5;       //valid
    num2 = num1;    //valid
    5 = 45;         //invalid

    There are few rules you need to follow which naming the variables such as it must be unique, it has to be letters, digits, or the underscore _. Variables are case sensitive that is Number and number are considered different variables.


  • C# Data Types and Literals

    As we know the variables holds the data and the type of data that it holds refers to the data-types. Theses types are: integer, float, double, boolean, etc. and each of this data-types has a specific size such as integer has the memory size of 4 byte and so on.

    Now, C# has categorized its data-types in three different parts:

    • Value types: int, float, short, char, etc.
    • Reference types: String, Class, Object and Interface
    • Pointer types: simply Pointers
    C# data-types

    C# contains two general categories of built-in data types: value types and reference types.

    The difference between the two types is what a variable contains.

    • For a value type, a variable holds an actual value, such 3.1416 or 212.
    • For a reference type, a variable holds a reference to the value.

    Let us discuss each of them:

    1. Value types

    Value types are derived from the class System.ValueType. They refer to the direct value used in the program such as integers, float, double and more. These data-types are both integer-based and floating-point types. Memory allocates for the memory space whenever some value type is assigned.

    Value types are of two types, Predefined Data Types (Integer, Boolean, etc) and User-defined Data Types (Structure, Enumerations, etc).

    Follow the table below for the value type list in C#:

    Data TypesMemory SizeRange
    int4 byte-2,147,483,648 to -2,147,483,647
    float4 byte1.5 * 10-45 – 3.4 * 1038, 7-digit precision
    short2 byte-32,768 to 32,767
    char1 byte-128 to 127
    long8 byte-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
    double8 byte5.0 * 10-324 – 1.7 * 10308, 15-digit precision
    signed short2 byte-32,768 to 32,767
    unsigned short2 byte0 to 65,535
    signed int4 byte-2,147,483,648 to -2,147,483,647
    unsigned int4 byte0 to 4,294,967,295
    signed long8 byte?9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
    unsigned long8 byte0 – 18,446,744,073,709,551,615
    decimal16 byteat least -7.9 * 10?28 – 7.9 * 1028, with at least 28-digit precision

    Memory size changes according to the Operating System.


    2. Reference Types

    Reference types are a bit different than Value Types, this kind of data-types do not contain the actual data stored in a variable, but they contain a reference to the variables.

    Reference types specify to the memory location and if the data in the memory location is changed by one of the variables, the other variable automatically reflects this change in value.

    They are also of two types, Predefined Data Types (Objects, String and Dynamic) and User-defined Data Types (Classes, Interface).


    3. Pointer Type

    It is also known as the indicator as it points to the address of a value. Pointer type variables store the memory address of another type. The abilities to do the task is same as the pointers that we use in C or C++.

    Syntax(declaration):

    It is declare using a * (asterisk symbol).

    data-type* identifier;
    
    //example
    int* ptr1;
    char* ptr2;

    Literals

    Literals refer to fixed values that are represented in their human-readable form. For the most part, literals and their usage are so intuitive that they have been used in one form or another by all the preceding sample programs.

    For example, the number 100 is a literal.

    C# literals can be of any simple type. The way each literal is represented depends upon its type.

    • To specify a float literal, append an F or f to the constant. For example, 10.19F is of type float.
    • To specify a decimal literal, follow its value with an m or M. For example, 9.95M is a decimal literal.

    Literals can be any of the following types that can be used in a program:

    • Integer Literals
    • Floating-point Literals
    • Character Literals
    • String Literals
    • Null Literals
    • Boolean Literals

    Integer Literals

    An integer literal can be decimal(base 10) or hexadecimal(base 16) constant or Octal(base 8). A prefix specifies the base or radix: 0x or 0X for hexadecimal, and there is no prefix id for decimal.

    For unsigned and signed, the suffix U and L are used. And the only rule is you can represent these suffix either in uppercase or the lower case and in any order you may prefer.

    Example of integer literals:

    20 // simply int
    30u // u for unsigned int
    30L // l for long

    Floating-Point Literals

    Literals is called the floating-point literals if it has the following part:

    • an integer part
    • a decimal point
    • a fractional part
    • an exponent part

    We can constitute them in decimal or exponential form. Representation of decimal form required the decimal point or the exponent or the both while the exponential form required the integer part or the fractional part or both.

    By default, the floating-point literals are of double type and assigning it directly to a float variable is not possible. Instead, we can use the suffix for a float that is I or F.

    Some example to show how can it be used and how it can’t be used in a program:

    4.25671       //valid
    254632E-5F    //valid
    120E          //Invalid: missing exponent
    240f          //Invalid: missing exponent or decimal

    Character Literals:

    Character literals are those literals that are enclosed within the single quote(‘ ‘) such as ‘S’ is stored as a character. we can represent the character literals in several ways:

    • Single quote: These are the plain character enclosed within a single quote. Eg: char c = 'S';
    • Unicode Representation: These are the universal character denoted in Unicode such as char c = '\u0061'; //this Unicode value represents a character.
    • Escape Sequence: This is also a character literal that can be used as a character such as char c = '\t';

    The following table shows some escape sequence that are used in a program with their meaning. Go through them carefully.

    Escape sequenceMeaning
    \\\ character
    \’‘ character
    \”” character
    \?? character
    \aAlert or bell
    \bBackspace
    \fForm feed
    \nNewline
    \rCarriage return
    \tHorizontal tab
    \vVertical tab
    \xhh . . .Hexadecimal number of one or more digits

    String Literals

    This type of literals is enclosed within double-quotes (“”) or with @””. A string also enclosed the same character as the character literal does such as plain character, universal characters and more.

    The below example show how the string literals are represented in a program.

    "simple, code"
    "simple, \code"
    "simple, " "d" "code"
    @"simple code"

    Boolean Literals

    This literal has only two values that show the result and they are true and false. You can assign the value at the beginning while declaring the boo as shown in an example below.

    bool a = true;
    bool b = false