Array Initilization
Arrays


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

namespace ConsoleApplication21
{
    class Program
    {
        static void Main(string[] args)
        {
            int x, n, sum = 0;
            int[] marks = { 12, 19, 12, 12, 12, 12, 16 };
            n = marks.Length;
            for (x = 0; x < n; x++)
            {
                sum = sum + marks[x];
            }

            Console.WriteLine(sum);
            Console.Read();

        }
    }
}

Array Declaration

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

namespace ConsoleApplication21
{
    class Program
    {
        static void Main(string[] args)
        {
            int x, n, sum = 0;
            int[] marks = new int[5];
            n = marks.Length;
            for (x = 0; x < n; x++)
            {
                Console.WriteLine("Enter subject " +(x+1)+" Marks");
                marks[x] = Convert.ToInt32(Console.ReadLine());
                sum = sum + marks[x];
               
            }

            Console.WriteLine(sum);
            Console.Read();

        }
    }
}


Array Assigment

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

namespace ConsoleApplication21
{
    class Program
    {
        static void Main(string[] args)
        {
            int x, n, sum = 0;
            int[] marks = new int[5];
            marks[0]=12;
            marks[1]=12;
            marks[2]=12;
            marks[3]=12;
            marks[4]=12;
            n = marks.Length;
            for (x = 0; x < n; x++)
            {
               
                marks[x] = Convert.ToInt32(Console.ReadLine());
                sum = sum + marks[x];
               
            }

            Console.WriteLine(sum);
            Console.Read();

        }
    }
}



Sum Of Array Numbers Withot Negetive Values

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

namespace ConsoleApplication21
{
    class Program
    {
        static void Main(string[] args)
        {
            int x, n, sum = 0;
            int[] marks = { -12, 190, 12, 12, 12, 12, 16 };
            n = marks.Length;
            for (x = 0; x < n; x++)
            {
                if(marks[x]>0 && marks[x]<=100)
                {
                sum = sum + marks[x];
                }
            }

            Console.WriteLine(sum);
            Console.Read();

        }
    }
}

Multi Dimensional Array Initialization

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

namespace ConsoleApplication21
{
    class Program
    {
        static void Main(string[] args)
        {
            int x,y, sum = 0;
            int[,] marks = { { 1, 1, 1, 1, 1 }, { 2, 2, 2, 2, 2 }, { 3, 3, 3, 3, 3 } };

            for (x = 0; x < 3; x++)
            {
                for (y = 0; y < 5; y++)
                {
                    sum = sum + marks[x, y];
                }
                Console.WriteLine("Student" + (x + 1) + "Marks are" + sum);
                sum = 0;
            }

            Console.Read();

        }
    }
}

Multi Dimensional Array Declaration


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

namespace ConsoleApplication21
{
    class Program
    {
        static void Main(string[] args)
        {
            int x,y, sum = 0;
            int[,] marks = new int[3, 5];

            for (x = 0; x < 3; x++)
            {
                Console.WriteLine("................Enter Student"+ (x + 1) + "marks...................");
                for (y = 0; y < 5; y++)
                {
                    Console.WriteLine("Enter Subject " + (y + 1) + "Marks are" );
                    marks[x, y] = Convert.ToInt32(Console.ReadLine());

                    sum = sum + marks[x, y];
                }
                Console.WriteLine("Student" + (x + 1) + "Marks are" + sum);
                sum = 0;
            }

            Console.Read();

        }
    }
}

Multi Dimensional Array Assignment


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

namespace ConsoleApplication21
{
    class Program
    {
        static void Main(string[] args)
        {
            int x,y, sum = 0;
            int[,] marks = new int[3, 5];
            marks[0,0] = 1;
            marks[0, 1] = 1;
            marks[0, 2] = 1;
            marks[0, 3] = 1;
            marks[0, 4] = 1;
            marks[1, 0] = 2;
            marks[1, 1] = 2;
            marks[1, 2] = 2;
            marks[1, 3] = 2;
            marks[1, 4] = 2;
            marks[2,0] = 3;
            marks[2, 1] = 3;
            marks[2, 2] = 3;
            marks[2, 3] = 3;
            marks[2, 4] = 3;
            for (x = 0; x < 3; x++)
            {
                
                for (y = 0; y < 5; y++)
                {
            
                    sum = sum + marks[x, y];
                }
                Console.WriteLine("Student" + (x + 1) + "Marks are" + sum);
                sum = 0;
            }

            Console.Read();

        }
    }
}

Date & Time Methods
Date & Time


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

namespace DateTimeDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            //create DateTime obj
            DateTime dt1;
            dt1 = Convert.ToDateTime("7/16/2009 6:23 PM");
            DateTime dt2 = DateTime.Now;
            //display dates
            Console.WriteLine("dt1 is: " + dt1.ToString());
            Console.WriteLine("dt2 is: " + dt2.ToString());
            
            //display individual values
            Console.WriteLine("\nIndividual values of dt2:");
            Console.WriteLine("dd: " + dt2.Day);
            Console.WriteLine("mm: " + dt2.Month);
            Console.WriteLine("yyyy: " + dt2.Year);
            Console.WriteLine("hh: " + dt2.Hour);
            Console.WriteLine("mi: " + dt2.Minute);
            Console.WriteLine("ss: " + dt2.Second);
            Console.WriteLine("ms: " + dt2.Millisecond);

            //implement user-defined date formats
            string IndianFormattedDate = dt2.Day + "-" + dt2.Month + "-" + dt2.Year;
            Console.WriteLine("\nDate in indian format: " + IndianFormattedDate);

            //display the day index
            Console.WriteLine("\nDay of year: " + dt2.DayOfYear);
            Console.WriteLine("\nDay of week: " + dt2.DayOfWeek);

            //long and short formatted date and time
            Console.WriteLine("\nShort date format: " + dt2.ToShortDateString());
            Console.WriteLine("\nLong date format: " + dt2.ToLongDateString());
            Console.WriteLine("\nShort time format: " + dt2.ToShortTimeString());
            Console.WriteLine("\nLong time format: " + dt2.ToLongTimeString());

            //adding values
            DateTime dt3 = dt2.AddDays(5);
            Console.WriteLine("\ndt2 after adding 5 days: " + dt3.ToShortDateString());
            DateTime dt4 = dt2.AddMonths(5);
            Console.WriteLine("\ndt2 after adding 5 months: " + dt4.ToShortDateString());
            DateTime dt5 = dt2.AddYears(5);
            Console.WriteLine("\ndt2 after adding 5 years: " + dt5.ToShortDateString());
            DateTime dt6 = dt2.AddHours(5);
            Console.WriteLine("\ndt2 after adding 5 hours: " + dt6.ToLongTimeString());
            DateTime dt7 = dt2.AddMinutes(5);
            Console.WriteLine("\ndt2 after adding 5 minutes: " + dt7.ToLongTimeString());
            DateTime dt8 = dt2.AddSeconds(5);
            Console.WriteLine("\ndt2 after adding 5 seconds: " + dt8.ToLongTimeString());

            //check whether this is leap year or no
            if (DateTime.IsLeapYear(dt2.Year))
                Console.WriteLine("\nThis is a leap year.");
            else
                Console.WriteLine("\nThis is not a leap year.");

            //get the total no. of days in this month
            Console.WriteLine("\nNo. of days in this month: " + DateTime.DaysInMonth(dt2.Year, dt2.Month));

            //date comparision
            if (dt1 == dt2)
                Console.WriteLine("\ndt1 and dt2 are equal.");
            else if (dt1 > dt2)
                Console.WriteLine("\ndt1 is greater than dt2.");
            else if (dt1 < dt2)
                Console.WriteLine("\ndt1 is less than dt2.");

            //date difference
            TimeSpan ts = dt2 - dt1;
            Console.WriteLine("\nThe difference between dt2 and dt1 is: " + ts.Days + " days.");

            Console.Read();
        }
    }
}

Random Class


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

namespace RandomDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            Random r = new Random();
            for (int i = 0; i < 300; i++)
            {
                Console.Write(r.Next(1, 300) + ", ");
            }
            Console.Read();
        }
    }
}

String
String

iusing System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace StringDemo
{
    class Program

    {
        static void Main(string[] args)
        {
            //create string object
            string s = "I am a .NET Developer";
            Console.WriteLine("The original string is: " + s);
            Console.WriteLine("The original string's length is: " + s.Length);
            
            //copy strings
            string s1;
            s1 = s;
            Console.WriteLine("\nThe copied string is: " + s1);

            //concatenate strings
            string s2 = "I am working in Wipro.";
            string s3;
            s3 = s1 +". " + s2;
            Console.WriteLine("\nThe concatenated string is: " + s3);

            //compare strings
            if (s1 == s2)
                Console.WriteLine("\ns1 and s2 are equal");
            else
                Console.WriteLine("\ns1 and s2 are not equal");
            string s4 = s1;
            if (s1 == s4)
                Console.WriteLine("\ns1 and s4 are equal");
            else
                Console.WriteLine("\ns1 and s4 are not equal");

            //convert the string into lower case
            string s6 = s.ToLower();
            Console.WriteLine("\nLower case is: " + s6);

            //convert the string into upper case
            string s7 = s.ToUpper();
            Console.WriteLine("\nUpper case is: " + s7);

            //string trimming
            string s8 = "   Hello, World   ";

            Console.WriteLine("\nString before trimming: " + s8);
            string s9 = s8.Trim();
            Console.WriteLine("\nString after trimming: " + s9);
            string s10 = s8.TrimStart();
            Console.WriteLine("\nString after left trimming: " + s10);
            string s11 = s8.TrimEnd();
            Console.WriteLine("\nString after right trimming: " + s11);

            //check for the start
            if (s.StartsWith("I"))
                Console.WriteLine("\nString starts with 'I'");
            else
                Console.WriteLine("\nString doesn't starts with 'I'");
            if (s.StartsWith("i"))
                Console.WriteLine("\nString starts with 'i'");
            else
                Console.WriteLine("\nString doesn't starts with 'i'");

            //check for the end
            if (s.EndsWith("Developer"))
                Console.WriteLine("\nString ends with 'Developer'");
            else
                Console.WriteLine("\nString doesn't ends with 'Developer'");
            if (s.EndsWith("am"))
                Console.WriteLine("\nString ends with 'am'");
            else
                Console.WriteLine("\nString doesn't ends with 'am'");

            //insert character(s) into the string
            string s12 = s.Insert(7, "ASP");
            Console.WriteLine("\nString after insetion: " + s12);

            //remove character(s) from the string
            string s13 = s.Remove(7);
            Console.WriteLine("\nString after removal: " + s13);
            string s14 = s.Remove(7, 5);
            Console.WriteLine("\nString after 5 characters removal: " + s14);

            //get the sub string
            string s15 = s.Substring(7);
            Console.WriteLine("\nSub String is: " + s15);

            string s16 = s.Substring(7, 4);
            Console.WriteLine("\nAnother Sub String is: " + s16);

            //replace the character(s)
            string s17 = s.Replace("a", "x");
            Console.WriteLine("\nString after replace: " + s17);

            //left padding
            string s18 = s.PadLeft(26, '*');
            Console.WriteLine("\nString after left padding: " + s18);

            //right padding
            string s19 = s.PadRight(26, '*');
            Console.WriteLine("\nString after right padding: " + s19);

            //split the string
            string s20 = "This is first line.This is second line.This is third line.";
            Console.WriteLine("\nA string for splitting:\n" + s20);
            string[] s21 = s20.Split('.');
            Console.WriteLine("\nThe string array after splitting:");
            foreach (string temp in s21)
                Console.WriteLine(temp);


            //search the string for character(s)
            Console.WriteLine("Index of 'D' is: " + s.IndexOf('D'));
            Console.WriteLine("Index of 'x' is: " + s.IndexOf('x'));
            Console.WriteLine("Index of 'am' is: " + s.IndexOf("am"));
            Console.WriteLine("Index of 'a' is: " + s.IndexOf('a'));
            Console.WriteLine("Index of 'a' after 3rd character is: " + s.IndexOf('a', 3));

            Console.WriteLine("LastIndex of 'D' is: " + s.LastIndexOf('D'));
            Console.WriteLine("LastIndex of 'x' is: " + s.LastIndexOf('x'));
            Console.WriteLine("LastIndex of 'am' is: " + s.LastIndexOf("am"));
            Console.WriteLine("LastIndex of 'a' is: " + s.LastIndexOf('a'));
            Console.WriteLine("LastIndex of 'a' after 3rd character is: " + s.LastIndexOf('a', 3));
            
            Console.Read();
        }
    }
}

Coping, Sorting,Reversing Array
Array Class


namespace ArrayClassDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            //single dimensional array
            Console.WriteLine("Single dimensional array:");
            int[] a = { 643, 32948, 39, 23094, 112};
            foreach (int i in a)
                Console.Write(i + " ");
            Console.WriteLine("\nLength: " + a.Length);
            Console.WriteLine("Rank: " + a.Rank);
            
            //double dimensional array
            Console.WriteLine("\nDouble dimensional array:");
            int[,] b = { {56, 1238, 8812}, {99, 2784, 2892} };
            for (int i = 0; i < 2; i++)
            {
                for (int j = 0; j < 3; j++)
                    Console.Write(b[i,j] + " ");
                Console.WriteLine();
            }
            Console.WriteLine("Length: " + b.Length);
            Console.WriteLine("Rank: " + b.Rank);
            
            //string array
            Console.WriteLine("\nString array:");
            string[] s = { "Java", "Corba", "C++", "Perl", "AJAX", "JavaScript" };
            foreach (string i in s)
                Console.Write(i + " ");
            
            //copying arrays
            Console.WriteLine("\n\nSingle dim array after copy:");
            int[] c = new int[5];
            a.CopyTo(c, 0);
            foreach (int i in c)
                Console.Write(i + " ");

            //sorting arrays
            Array.Sort(c);
            Array.Sort(s);
            Console.WriteLine("\n\nSingle dim array after sorting:");
            foreach (int i in c)
                Console.Write(i + " ");
            Console.WriteLine("\n\nString array after sorting:");
            foreach (string i in s)
                Console.Write(i + " ");

            //reversing arrays
            Array.Reverse(c);
            Array.Reverse(s);
            Console.WriteLine("\n\nSingle dim array after reversing:");
            foreach (int i in c)
                Console.Write(i + " ");
            Console.WriteLine("\n\nString array after reversing:");
            foreach (string i in s)
                Console.Write(i + " ");
            Console.WriteLine("\n");

            //searching an element
            Console.WriteLine("Index of Java is: " + Array.IndexOf(s, "Java"));

            Console.Read();
        }
    }
}