Showing posts with label CSharp. Show all posts
Showing posts with label CSharp. Show all posts

13 August 2014

Array vs Arraylist Difference - C#

Introduction:


In this article i am going to explain the difference between Array and Arraylist in C#.

Arrays:


Arrays are strongly typed collection of same datatype.Generally in arrays we will store values with index basis that will start with zero. If we want to access values from arrays we need to pass index values.These arrays are of specified length that cannot be change during runtime.

Declaration:


To declare the array use "[]" brackets after data type and then assign the fixed length or fixed size to an array as shown below

static void Main()
{
    string[] myArray = new string[3];
    myArray [0] = "Welcome";
    myArray [1] = "to my";
    myArray [1] = "Blog";
}

Above code shows that we have declared a String type array size of 3 (Means we can store only 3 string values in that array).

ArrayList:


Array lists are not strongly type collection.It stores a collection of values from different data types or same data types. 
Array list size will increase or decrease dynamically it can take any size of values from any data type. These Array lists will be accessible with “System.Collections” namespace. If the values stored in collection are of different data types then type cast is must.

Declaration:


To add values to an array-list you can call "Add" method of Array-List to keep adding values continuously

static void Main()
{
    ArrayList aryList = new ArrayList();
    aryList.Add(123);                                // Added Integer value
    aryList.Add("C-Sharp");                       // Added string value
    aryList.Add(DateTime.Now);               // Added DateTime
}

Note : If the values stored in collection are of different data types then type cast is must.

For better understanding check the below table


                                     !********************* Hope you understand the difference J ***************************!
Reference:
http://www.aspdotnet-suresh.com

28 July 2014

String vs StringBuilder Difference - C#

Introduction :


In this post i am going to explain the difference between String and String Builder in C#. This is a common interview question.

String


String is immutable.Immutable means once we create string object we cannot modify. Any operation like insert, replace or append happened to change string simply it will discard the old value and it will create new instance in memory to hold the new value.

Program

Using System;

public static void Main()
{
  string myString = "Difference";
  myString += " between";
  myString += " String";
  myString += " and";
  myString += " StringBuilder";
}

StringBuilder


String builder is mutable it means once we create string builder object we can perform any operation like insert, replace or append without creating new instance for every time.

Program

Using System.Text;

public static void Main()
{
  StringBuilder myString = new StringBuilder("Difference");
  myString.Append(" between")
  myString.Append(" String")
  myString.Append(" and")
  myString.Append(" StringBuilder")
}

For better understanding see the below table and image


Alvin

                                   !********************* Hope you understand the difference J ***************************!


Reference:

http://www.aspdotnet-suresh.com & Venkat's csharp-video-tuotrials.blogspot.co.uk

15 June 2014

Function to Add Business days - C#

Introduction

      In many cases developers have a demand of calculation and manipulations with business dates.

Scenario:

      We want to add “N” business days/working days to a current date (Or from a specified date). Where “N” is the number of business days.

Solution:

      In this blog I have described how to add “N” business days/working days to a date
The below C# function does it all for you. Simply pass in a date, along with the number of working days you want to add.

public DateTime AddBusinessDays(DateTime DateIn, int Days2Add)
{
    DateTime resultDate;
    // Adds the N (Days2Add) working days to DateIn (Today)
    resultDate = DateIn.AddDays(Days2Add);
         
    // Loops and adds only non-weekend day(working days)
    while (resultDate.DayOfWeek == DayOfWeek.Saturday || resultDate.DayOfWeek == DayOfWeek.Sunday)
    {
        resultDate = resultDate.AddDays((Days2Add < 0 ? -1 : 1));
    }
    return resultDate;
}

And here's how you call it in your application:
private void button1_Click(object sender, EventArgs e)
{
    DateTime newDate = AddBusinessDays(DateTime.Today, 6);
    MessageBox.Show("The 6th Business day from today is {0}", newDate.ToString());
}

The above code is Tested