Posts

Showing posts with the label C#

static and non-static fields in C#

Introduction MSDN Definition : A static class is basically the same as a non-static class, but there is one difference: a static class cannot be instantiated. In other word, we cannot use the new keyword to create a variable of the class type. Because there is no instance variable, we access the members of a static class by using the class name itself. C# fields must be declared inside a class. However, if we declare a method or a field as static, we can call the method or access the field by using the name of the class. No instance is required. We can also use the static keyword when defining a field. With this feature, we can create a single field that is shared among all objects created from a single class. Non static fields are local to each instance of an object. When you define a static method or field, it does not have access to any instance fields defined for the class; it can use only fields that are marked as static. Furthermore, it can directly invoke only oth

Anonymous Typed Classes in C#

Introduction MSDN Definition : Anonymous types provide a convenient way to encapsulate a set of read-only properties into a single object without having to explicitly define a type first. The name is generated by the compiler and is not available at the source code level or our application cannot access it. The type of each property is inferred by the compiler. Anonymous types are a new feature introduced with C# 2.0. Anonymous class is class that has no name and it can help us to increase the readability and maintainability of applications by keeping the declarations of variables much close to the code that uses it. We create anonymous class by using the new keyword and a pair of braces defining the fields and values that we want the class to contain. For example: myAnonyClassObject = new { Name = "Abhimanyu", Age = 21 }; Above class contains two public fields called Name as I have initialized a string "Abhimanyu" and Age as I have initialize

Static Methods in C#

Introduction The methods in C# may or may not take parameters and they may or may not return a value. Also, a custom method that is also known as user defined methods may be declared non-static (instance level) or static (class level). When a method is static then it can be invoked directly from the class level without creating an object. This is the reason for making main() function/method to be static. The another example is WriteLine() method that is called/used without creating any object. Let’s have some chat in example.      class Program     {         public static void withoutObj()         {             Console .WriteLine( "Hello" );         }          static void Main()         {             Program . withoutObj();             Console .ReadKey();         }     } In above example, I will be calling method using class name itself and no any WriteLine() object is used. Using Static Method Usually we define a set of data

Facts of ref and out type Parameters in C#

Introduction Parameters are initialized with a copy of argument when we pass an argument to a method, even this is true regardless of whether parameter is a value type like int, int? (nullable) or reference type. Look at the example given below, it is impossible to change the parameter to affect the value of the argument passed.         static void Main( string [] args)         {             int num = 33;             myfunction(num);             Console .WriteLine(num);             Console .ReadKey();         }         private static void myfunction( int rcnum)         {             rcnum++;         } In above example, the value to the console is 33 not 34. The myfunction method increments a copy of the argument named rcnum and not the original argument. C# Language provides the ref and out keywords to do this. Ref Type Parameters When we pass a parameter as ref type to a method, the method refers to the same variable and changes made

Facts of Enumerations in C#

Introduction You must be aware about enumerations (or simply enum), but there are many things which we should keep in mind always. Let's begin from basics.   C# has two fundamental types, value types and reference types. A value type variable holds values directly on stack whereas reference type variable holds a reference to a variable or object on the heap.   In value types, C# has enumerations and structures.   Why we use Enumerations?   Okay, suppose we want to work with day's name. We could use the integer 0, 1, 2 and so on to represent Sunday, Monday, Tuesday, etc respectively. This system will work cool but it is not much intuitive or say it is not robust solution because if we use the integer value 0 in code, it would not be obvious that a particular 0 represents Sunday. One more thing, if we declare an integer variable named day, then there is nothing which stops assigning any legal integer value apart from 0, 1, …., 5, 6. Here C# offers much better

Using if Statements in C#

When we want to choose between executing two different blocks of code depending on the result of a Boolean expression, we can use an if statement. Syntax:- The syntax of an if statement is as follows (if and else are C# keywords): if ( booleanExpression )       statement-1;   else       statement-2; If booleanExpression evaluates to true, statement-1 runs; otherwise, statement-2 runs. The else keyword and the subsequent statement-2 are optional. If there is no else clause and the booleanExpression is false, execution continues with whatever code follows the if statement. For example, here’s an if statement that increments a variable representing the second hand  of a stopwatch  (Minutes are ignored for now). If the value of the seconds variable is 59, it is reset to 0; otherwise, it is incremented using the ++ operator: int seconds; ...   if (seconds == 59)       seconds = 0;   else       seconds++; Grouping Statements:- Notice tha

Array Manipulations in C# - Part 1

Image
Introduction Array in C# looks like C/C++ but basically C# array are derived from base class System.Array. An array is an un-ordered sequence of elements. All the elements in an array have the same type unlike the fields in a structure or class which can have different types. The elements of an array live in a contiguous block of memory and are accessed by using an integer index unlike fields in a structure or class which are accessed by name. Normally in C# the array index starts with 0 but it is possible to have an array with arbitrary lower bound using the static method CreateInstance() of System.Array. Arrays can be single or multidimensional and the declaration would be as follows for single dimensional: Declaration of Array int[] x = new int[12]; In above statement I have declared a integer type array named x having 12 cells. Now to assign value in them x[0] = 5; x[1] = 10; :::::::::: X[11] = 7; Please note index values starts from 0 and ends

Popular posts from this blog

Migrating database from ASP.NET Identity to ASP.NET Core Identity

Customize User's Profile in ASP.NET Identity System

Lambda two tables and three tables inner join code samples