Showing posts with label MVC. Show all posts
Showing posts with label MVC. Show all posts

Operators in c#

    An operator is a symbol that tells the compiler to perform specific mathematical or logical manipulations

    Type of operators:
    1.     Arithmetic Operators
    2.     Relational Operators
    3.     Bitwise Operators
    4.     Logical Operators
    5.     Unary Operators
    6.     Ternary Operator
    7.     Bitwise and Bit Shift Operators
    8.     Compound Assignment Operators  
       
    1-Arithmetic Operators       



                            

      2-Compound Assignment Operators







3-Bitwise Operators








    4 -Logical Operators





    5-Relational Operators





    6-Unary  Operators










    Difference between N-tier vs N-layer architecture


    N-tier and n-layer are entirely different concepts. N-tier refers to the actual and system components of your application. For example: Suppose you create a web application, its components include your application server where it is being hosted, the database being used with it, on another server and the user machine who access the application so These are referred to as the 3 tiers of your application. They may be n in number and so the term n-tier application. So tiers are the physically separate components of the same system.

     On the other hand, layers refer to the internal architecture of your component. For example: in your application code, you divide the code into different layers like the Data access layer (DAL), Business logic layer (BAL), etc. So they are internal to the component and these layers interact with each other internally to form the entire component.


    Reflection in C#

    Reflection is the ability of a managed code to read its own metadata for the purpose of finding assemblies, modules and type information at runtime. it allows code to inspect other code within the same system. The main class for reflection is the System. Type class, which is an abstract class representing a type in the Common Type System (CTS). When you use this class, you can find the types used in a module and namespace and also determine if a given type is a reference or value type.


    Benefits of Reflection C#


    1.      Use Module to get all global and non-global methods defined in the module.
    2.      Use Assembly to load modules listed in the assembly manifest.
    3.      Use PropertyInfo to get the declaring type, reflected type, data type, name and writable status of a property or to get and set property values.
    4.      Use MethodInfo to look at information such as parameters, name, return type, access modifiers, and implementation details.
    5.      Use EventInfo to find out the event-handler data type, the name, declaring type and custom attributes.
    6.      Use ConstructorInfo to get data on the parameters, access modifiers, and implementation details of a constructor.




    Constructors in C#


    The constructor is special methods which get called automatically when an object of a class is created. Constructors are specially used to initialize data members.

    Types of constructors:-
    1.     Default Constructor
    2.     Parameterized Constructor
    3.     Copy Constructor
    4.     Static Constructor


       public class SampleClassNitin

        {

            public SampleClassNitin()

            {

                Console.WriteLine("SampleClassNitin  Test Method");

            }
        }

    Default Constructor
    A constructor without having any parameters called the default constructor.

    Parameterized Constructors
    A constructor with at least one parameter is called as parameterized constructor.

    Constructor Overloading
    When we create another constructor with the same method name with different parameters called Constructor Overloading.

    Copy Constructor
    A parameterized constructor that contains a parameter of the same class type is called as copy constructor.


    Static Constructor

    If we declared constructor as static and it will be invoked only once that type of constructor is called Static Constructor.


    Try-Catch-Finally in c#

    Exceptions allow an application to transfer control from one part of the code to another. When an exception is thrown, the current flow of the code is interrupted and handed back to a parent try-catch block. C# exception handling is done with the following keywords: try, catch, finally, and throw

    Syntax



    try
            {
                //statements that may raise exceptions
            }
            catch
            {
                //Catch the exception
            }
            finally
            {
                //This block of code will always get executed whether an exception occurs or not as long                as there are no exceptions within this block itself.
            }




    Example 1

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;

    namespace ConsoleApplication1
    {
        class ExceptionHandlingExampleProgram
        {
            static void Main(string[] args)
            {
                object o2 = null;
                try
                {
                    int i2 = (int)o2;   // Error 
                }
                catch (Exception exceptionObj)
                {
                    // log exception from exceptionObj
                }
                finally
                {

                    // do cleanup work here
                }

            }
        }
    }



    C# Hello World Program

    // A Hello World! program in C#.
    using System;
    namespace HelloWorldExampleNamespace
    {
        class HelloWorldClass
        {
            static void Main()
            {
                Console.WriteLine("Hello World!");

                // Keep the console window open in debug mode.
                Console.WriteLine("Press any key to exit.");
                Console.ReadKey();
            }
        }
    }

    Static Class in #

    A static class is the same as a non-static class, but a static class cannot be instantiated. In other words, you cannot use the new keyword to create an object of the class type. The static keyword in the C# programming language allows you to define static classes and static members. A static class is similar to a class that is both abstract and sealed. To declare a class as static, you should mark it with the static keyword in the class declaration.



    Syntax of Static Class


    static class classname
    {
        //static data members
       //static methods
    }



    Example of Static Class

    using System;
    namespace StaticClassExampleNameSpace
    {
        public static class StaticClassExamleClass
        {
            public static double CelsiusToFahrenheit(string temperatureCelsius)
            {
                // Convert argument to double for calculations.
                double celsius = Double.Parse(temperatureCelsius);
                // Convert Celsius to Fahrenheit.
                double fahrenheit = (celsius * 9 / 5) + 32;
                return fahrenheit;
            }

            public static double FahrenheitToCelsius(string temperatureFahrenheit)
            {
                // Convert argument to double for calculations.
                double fahrenheit = Double.Parse(temperatureFahrenheit);
                // Convert Fahrenheit to Celsius.
                double celsius = (fahrenheit - 32) * 5 / 9;
                return celsius;
            }
        }
    }


    Variable in c#

    The variable in C# is nothing but a name given to a data value. Each variable in C# has a specific type, which determines the size and layout of the variable's memory the range of values that can be stored within that memory.

    Example

    using System;
    namespace VariableExampleNameSpace
    {
        public class StudentClass
        {
            private int RollNo; ///private variable declaration
            public int EnrolNo; ///public variable declaration
            public void Percentage()
            {
                ///local variable declaration,
                int totalMarks = 360;
            }
        }
    }


    Array in c#


    We can store multiple variables of the same type in an array data structure, so An array stores a fixed-size sequential collection of elements of the same type. A fixed-length array can store a predefined number of items. A dynamic array does not have a predefined size. The size of a dynamic array increases as you add new items to the array. You can declare an array of fixed length or dynamic.

    Example of Array in c#:-

    int[] array = new int[4];
    array[0] = 10;
    array[1] = 20;
    array[2] = 30;
    array[3] = 40;

    More Examples of the array in c#

    int[] intArray;  // can store int values
    bool[] boolArray; // can store boolean values
    string[] stringArray; // can store string values

    Array Types in c#
    1.      Single-dimensional arrays
    2.      Multidimensional arrays or rectangular arrays
    3.      Jagged arrays
    4.      Mixed arrays.

    Single Dimension Arrays
    int[] intArray;
    intArray = new int[3];

    Multi-Dimensional Arrays
    Array with more than one dimension is called multi- Dimensional. The form of a multi-dimensional array is a matrix.

    Example:-
    int[,] numbers = new int[3, 2] { { 1, 2 }, { 3, 4 }, { 5, 6 } };

    Jagged Arrays

    Jagged arrays are arrays of arrays.
    int[][] intJaggedArray = new int[3][];
    intJaggedArray[0] = new int[2]; 
    intJaggedArray[1] = new int[4]; 
    intJaggedArray[2] = new int[6]; 

    Mixed Arrays
    Mixed arrays are a combination of multi-dimension arrays and jagged arrays.



    Data Types in c#

    There are two kinds of types in C#: value types and reference types. Variables of value types directly contain their data whereas variables of reference types store references to their data. There are two kinds of types in C#: value types and reference types. Variables of value types directly contain their data whereas variables of reference types store references to their data.

    Value types of data types in c#

    1) sbyte
    2) short
    3) int
    4) long
    5) byte
    6) ushort
    7) uint
    8) ulong
    9) char
    10) float
    11) double
    12) decimal ,bool
    13) Enum types
    14) Struct types
    15) Nullable value types

    Reference types of data types in c#
    1.     Class types
    2.      Interface types
    3.      Array types
    4.      Delegate types