35
asp.net c# examples ebook by saiful alam free copy http://asp-net-example.blogspot.com 1

ASP.net Examples

  • Upload
    prem09

  • View
    7

  • Download
    0

Embed Size (px)

DESCRIPTION

Asp.net Examples

Citation preview

  • asp.netc#examplesebookbysaifulalam

    freecopyhttp://aspnetexample.blogspot.com

    1

  • 001.splitastringonnewlines

    protected void Button1_Click(object sender, System.EventArgs e) { string txt = "this \r\n is a \n sample text."; //string[] lines = txt.Split(new string[] { Environment.NewLine },StringSplitOptions.None); string[] lines = txt.Split(new string[] { "\r\n","\n" },StringSplitOptions.None); Label1.Text = "splitted by new line............"; foreach (string s in lines) { Label1.Text += s + " "; } }

    002.comparestringarrays

    protected void Button1_Click(object sender, System.EventArgs e) { string[] colors = { "red", "blue", "green", "yellow" }; string[] colors2 = { "crimson", "pink", "hotpink", "darkred" }; string[] colors3 = { "red", "blue", "green", "yellow" };

    Label1.Text = "Colors: "; foreach(string s in colors) { Label1.Text += s + " | "; } Label1.Text += "Colors2: "; foreach(string s in colors2) { Label1.Text += s + " | "; } Label1.Text += "Colors3: "; foreach(string s in colors3) { Label1.Text += s + " | "; } Boolean result = colors.SequenceEqual(colors2); Label1.Text += "Colors = Colors2? " + result.ToString();

    2

  • Boolean result2 = colors.SequenceEqual(colors3); Label1.Text += "Colors = Colors3? " + result2.ToString(); }

    003.arraymergewithoutduplicates

    protected void Button1_Click(object sender, System.EventArgs e) { string[] colors = { "red", "blue", "green" }; string[] colors2 = { "crimson", "green", "hotpink"}; var mergedcolors = colors.Union(colors2);

    Label1.Text = "Colors: "; foreach(string s in colors) { Label1.Text += s + " | "; } Label1.Text += "Colors2: "; foreach(string s in colors2) { Label1.Text += s + " | "; } Label1.Text += "Merged Colors: "; foreach (string s in mergedcolors) { Label1.Text += s + " | "; } }

    004.mergetwoarrays

    protected void Button1_Click(object sender, System.EventArgs e) { string[] colors = { "red", "blue", "green" }; string[] colors2 = { "crimson", "green", "hotpink"};

    List colorslist = new List(); colorslist.AddRange(colors);

    3

  • colorslist.AddRange(colors2);

    string[] mergedcolors = colorslist.ToArray();

    Label1.Text = "Colors: "; foreach(string s in colors) { Label1.Text += s + " | "; } Label1.Text += "Colors2: "; foreach(string s in colors2) { Label1.Text += s + " | "; } Label1.Text += "Merged Colors: "; foreach (string s in mergedcolors) { Label1.Text += s + " | "; } }

    005.createchararray

    protected void Button1_Click(object sender, System.EventArgs e) { char[] chararray = { 'c','f','s','u','m','a','n'};

    Label1.Text = "char array output: ";

    foreach (char c in chararray) { Label1.Text += c; }

    Label1.Text += "char array separated: ";

    foreach (char c in chararray) { Label1.Text += c + " | "; } }

    4

  • 006.createstringfromchararray

    protected void Button1_Click(object sender, System.EventArgs e) { char[] chararray = { 'b','a','n','g','l','a','d','e','s','h'};

    Label1.Text = "char array....."; foreach (char c in chararray) { Label1.Text += c + " | "; }

    string txt = new string(chararray); Label1.Text += "char array to string: " + txt; }

    007.initializestringarray

    protected void Button1_Click(object sender, System.EventArgs e) { string[] birds = new string[] { "Snow Goose", "Tundra Swan", "Orinoco Goose","Muscovy Duck" };

    //another way to initialize string array string[] morebirds = new string[3]; morebirds[0] = "Andean Condor"; morebirds[1] = "Greater Kestrel"; morebirds[2] = "Peregrine Falcon";

    Label1.Text = "birds array....."; foreach (string s in birds) { Label1.Text += s + " | "; }

    Label1.Text += "more birds array....."; foreach (string s in morebirds) { Label1.Text += s + " | "; } }

    5

  • 008.initializetwodimensionalstringarray

    protected void Button1_Click(object sender, System.EventArgs e) { string[,] birds = new string[3, 2] { {"Prairie","Falcon"}, {"Mississippi","Kite"}, {"Bald","Eagle"} };

    int rows = birds.GetUpperBound(0); int columns = birds.GetUpperBound(1);

    Label1.Text = "";

    for (int currentRow = 0; currentRow

  • 010.convertstringarraytolist

    protected void Button1_Click(object sender, System.EventArgs e) { string[] birds = new string[] { "Snail Kite", "Black Kite", "Red Kite", "PiedHarrier" };

    List birdsbirdslist = birds.OfType().ToList();

    Label1.Text = "birds array: "; foreach(string s in birds) { Label1.Text += s + " | "; }

    Label1.Text += "birds list: "; foreach (string s in birdslist) { Label1.Text += s + " | "; } }

    011.convertstringtobytearray

    protected void Button1_Click(object sender, System.EventArgs e) { const string txt = "array examples"; byte[] bytearray = Encoding.ASCII.GetBytes(txt);

    Label1.Text = ""; foreach(byte b in bytearray) { Label1.Text += b + " = " + Convert.ToChar(b).ToString()+ ""; } }

    7

  • 012.convertbytearraytostring

    protected void Button1_Click(object sender, System.EventArgs e) { byte[] bytearray = new byte[]{97,114,114,97,121,32,101,120,97,109,112,108,101,115 }; string txt = System.Text.ASCIIEncoding.ASCII.GetString(bytearray);

    //another way to convert byte array to string //string txt = System.Text.Encoding.Default.GetString(bytearray);

    Label1.Text = "byte array"; foreach(byte b in bytearray) { Label1.Text += b + " = " + Convert.ToChar(b).ToString()+ ""; }

    Label1.Text += "Converted String: " + txt; }

    013.getlengthofarray

    protected void Button1_Click(object sender, System.EventArgs e) { string[] birds = new string[] { "Indian Vulture", "Northern Harrier", "Spotted Harrier", "Lizard Buzzard", "Bateleur" };

    Label1.Text = "birds array........."; foreach(string s in birds) { Label1.Text += s + ""; }

    //this line return array length int birdsbirdscount = birds.Length;

    8

  • Label1.Text += "Number of birds[array length]: " + birdscount; }

    014.arraysortalphabetically

    protected void Button1_Click(object sender, System.EventArgs e) { string[] birds = new string[] { "Spotted Sandpiper", "Jack Snipe", "Whimbrel", "Dunlin", "Antarctic Tern" };

    Label1.Text = "birds array........."; foreach(string s in birds) { Label1.Text += s + ""; }

    //this line sort array elements Array.Sort(birds);

    Label1.Text += "sorted birds array........."; foreach (string s in birds) { Label1.Text += s + ""; } }

    015.arraysortdescending

    protected void Button1_Click(object sender, System.EventArgs e) { string[] birds = new string[] {

    9

  • "Crimson Rosella", "Caspian Tern", "Herring Gull", "Black Skimmer", "Varied Lorikeet" };

    Label1.Text = "birds array........."; foreach(string s in birds) { Label1.Text += s + ""; }

    //this query sort array in descending order var result = from element in birds orderby element descending select element;

    Label1.Text += "descending sorted birds array........."; foreach (string s in result) { Label1.Text += s + ""; } }

    016.arraycopy

    protected void Button1_Click(object sender, System.EventArgs e) { string[] birds = new string[] { "Great Jacamar", "Noisy Pitta", "Velvet Asity" };

    string[] birds2 = new string[birds.Length]; birds.CopyTo(birds2, 0);

    Label1.Text = "birds array........."; foreach (string s in birds) { Label1.Text += s + ""; }

    10

  • Label1.Text += "birds2 array[after copy]........."; foreach (string s in birds2) { Label1.Text += s + ""; } }

    017.checkstringarraycontainsavalue

    protected void Button1_Click(object sender, System.EventArgs e) { string[] birds = new string[] { "Eurasian Bittern", "Little Bittern", "Striated Heron", "Goliath Heron", "Australian Pelican" };

    Label1.Text = "birds array........."; foreach (string s in birds) { Label1.Text += s + ""; }

    Boolean result = birds.Contains("Australian Pelican"); Label1.Text += "'Australian Pelican' bird contains in birds array? " +result;

    Boolean result2 = birds.Contains("Pygmy Cormorant"); Label1.Text += "'Pygmy Cormorant' bird contains in birds array? " +result2; }

    018.intarraycontains

    11

  • protected void Button1_Click(object sender, System.EventArgs e) { int[] marks = new int[] { 82, 75, 58, 42, 65 };

    Label1.Text = "marks array........."; foreach (int i in marks) { Label1.Text += i.ToString() + ""; }

    Boolean result = marks.Contains(2); Label1.Text += "'2' mark contains in marks array? " + result;

    Boolean result2 = marks.Contains(82); Label1.Text += "'82' mark contains in marks array? " + result2; }

    019.arraycontainscaseinsensitive

    protected void Button1_Click(object sender, System.EventArgs e) { string[] birds = new string[] { "Brown Pelican", "Great White Pelican", "Peruvian Booby", "European Shag", "Guanay Cormorant" };

    Label1.Text = "birds array........."; foreach (string s in birds) { Label1.Text += s + ""; }

    Boolean result = birds.Contains("Guanay Cormorant",

    12

  • StringComparer.CurrentCultureIgnoreCase); Label1.Text += "'Guanay Cormorant' bird contains in birds array? " +result;

    // choose any one way //Boolean result2 = birds.Contains("gUaNay Cormorant",StringComparer.InvariantCultureIgnoreCase); //Boolean result2 = birds.Contains("gUaNay Cormorant",StringComparer.CurrentCultureIgnoreCase); Boolean result2 = birds.Contains("gUaNay Cormorant",StringComparer.OrdinalIgnoreCase); Label1.Text += "'gUaNay Cormorant' bird contains in birds array? " +result2;

    Boolean result3 = birds.Contains("Flightless Cormorant",StringComparer.CurrentCultureIgnoreCase); Label1.Text += "'Flightless Cormorant' bird contains in birds array?" + result3; }

    020.checkstringcontainsastringinstringarray

    protected void Button1_Click(object sender, System.EventArgs e) { string[] birds = new string[] { "Pygmy Falcon", "American Kestrel", "Lanner Falcon", "Mississippi Kite", "Griffon Vulture" };

    Label1.Text = "birds array........."; foreach (string s in birds) { Label1.Text += s + ""; }

    string stringtocheck = "Kestrel"; foreach (string s in birds) { if (s.Contains(stringtocheck))

    13

  • { Label1.Text += "[ " +stringtocheck +" ] contains in array's thiselement [" + s + "] "; } } }

    021.checkchararraycontainsachar

    protected void Button1_Click(object sender, System.EventArgs e) { char[] chararray = { 'a','b','c','d','e','f','g'};

    Label1.Text = "char array output.........";

    foreach (char c in chararray) { Label1.Text += c + ""; }

    Boolean cExists = chararray.Contains('c'); Boolean pExists = chararray.Contains('p');

    Label1.Text += "[c] char contains in char array? " + cExists; Label1.Text += "[p] char contains in char array? " + pExists; }

    022.addnewiteminexistingarray

    protected void Button1_Click(object sender, System.EventArgs e) { string[] birds = new string[] { "Pied Monarch", "Crested Jay", "Blue Jay", "European Magpie" };

    14

  • Label1.Text = "birds array[" + birds.Length.ToString()+ "]........."; foreach(string s in birds) { Label1.Text += s + ""; }

    Array.Resize(ref birds, birds.Length + 1); birds[birds.Length - 1] = "House Crow";

    Label1.Text += "after added new item birds array["+birds.Length.ToString()+"]........."; foreach (string s in birds) { Label1.Text += s + ""; } }

    023.addnewiteminendofexistingarray

    protected void Button1_Click(object sender, System.EventArgs e) { string[] birds = new string[] { "Northern Raven", "Barn Swallow", "Horned Lark" };

    Label1.Text = "birds array[" + birds.Length.ToString()+ "]........."; foreach(string s in birds) { Label1.Text += s + ""; }

    Array.Resize(ref birds, birds.Length + 1); birds[birds.Length - 1] = "Cape Grassbird";

    Label1.Text += "after added new item birds array[" +birds.Length.ToString() + "]........."; foreach (string s in birds) { Label1.Text += s + ""; }

    15

  • Array.Resize(ref birds, birds.Length + 1); birds[birds.Length - 1] = "Common Chiffchaff";

    Label1.Text += "after added new item birds array["+birds.Length.ToString()+"]........."; foreach (string s in birds) { Label1.Text += s + ""; } }

    024.arrayinitializationsyntaxes

    protected void Button1_Click(object sender, System.EventArgs e) { string[] birds = new string[] { "Brown Songlark", "Cape Grassbird", "Sedge Warbler" };

    string[] birds2 = new string[3]; birds2[0] = "Olivaceous Warbler"; birds2[1] = "Icterine Warbler"; birds2[2] = "Wood Warbler";

    string[] birds3 = { "Wrentit", "Striated Yuhina", "Bearded Reedling", };

    Label1.Text = "birds array........."; foreach (string s in birds) { Label1.Text += s + ""; }

    Label1.Text += "birds2 array........."; foreach (string s in birds2) { Label1.Text += s + ""; }

    Label1.Text += "birds3 array........."; foreach (string s in birds3)

    16

  • { Label1.Text += s + ""; } }

    025.intarrayaverage

    protected void Button1_Click(object sender, System.EventArgs e) { int[] exammarks = new int[] { 99,55,74,66,69};

    Label1.Text = "marks array......."; foreach (int i in exammarks) { Label1.Text += i.ToString() + ""; }

    double average = exammarks.Average(); Label1.Text += "Average Marks: " + average.ToString(); }

    026.sumanintarrayitems

    protected void Button1_Click(object sender, System.EventArgs e) { int[] score = new int[] { 125,100,255,500,85};

    Label1.Text = "score array......."; foreach (int i in score) { Label1.Text += i.ToString() + ""; }

    int total = score.Sum(); Label1.Text += "Total Score [Sum]: " + total.ToString(); }

    17

  • 027.arrayany

    protected void Button1_Click(object sender, System.EventArgs e) { int[] numbers = new int[] { 5,10,15,20,25};

    Label1.Text = "numbers array......."; foreach (int i in numbers) { Label1.Text += i.ToString() + ""; }

    Boolean result = numbers.Any(x => x * 10 == 100); Label1.Text += "any (item * 10) == 100? " + result.ToString(); }

    028.anyoperatorwithanstringarray

    protected void Button1_Click(object sender, System.EventArgs e) { string[] birds = new string[] { "Sooty Albatross", "Southern Giant Petrel", "Brown Pelican", "Fairy Prion" };

    Label1.Text = "birds array........."; foreach(string s in birds) { Label1.Text += s + ""; }

    Boolean resultA = birds.Any(x => x.StartsWith("A")); Label1.Text += "any bird name starts with ['A']? " + resultA;

    Boolean resultB = birds.Any(x => x.StartsWith("B")); Label1.Text += "any bird name starts with ['B']? " + resultB; }

    18

  • 029.passarrayasparameter

    static void changeArrayItems(int[] arr) { for (int i = 0; i < arr.Length;i++ ) { arr[i] += 1; } } protected void Button1_Click(object sender, System.EventArgs e) { int[] numbers = new int[] { 5, 10, 15, 20 };

    Label1.Text = "numbers array........."; foreach(int i in numbers) { Label1.Text += i.ToString() + ""; }

    changeArrayItems(numbers); Label1.Text += "after calling custom function. numbersarray........."; foreach (int i in numbers) { Label1.Text += i.ToString() + ""; } }

    030.doublearrayinitialization

    protected void Button1_Click(object sender, System.EventArgs e) { double[] doublearray = new double[] { 5.25,

    19

  • 10.33, 15.70, 20.99 };

    Label1.Text = "double array........."; foreach (double d in doublearray) { Label1.Text += d.ToString() + ""; }

    double sum = doublearray.Sum(); Label1.Text += "sum of double array items: " + sum.ToString(); }

    031.convertdoublearraytostring

    protected void Button1_Click(object sender, System.EventArgs e) { double[] doublearray = new double[] { 15.05, 10.23, 55.70, 29.89 };

    Label1.Text = "double array........."; foreach (double d in doublearray) { Label1.Text += d.ToString() + ""; }

    string convertedstring = String.Join(" ", doublearray); //another way tto convert double array items to string //string convertedstring = String.Join(" ",doublearray.Select(x=>x.ToString()).ToArray());

    Label1.Text += "converted string: " + convertedstring; }

    20

  • 032.convertstringarraytodoublearray

    protected void Button1_Click(object sender, System.EventArgs e) { string[] stringarray = new string[] { "15.56", "16.23", "55.70", "28.89" };

    Label1.Text = "string array........."; foreach (string s in stringarray) { Label1.Text += s + ""; }

    double[] darray = new double[stringarray.Length]; for (int i = 0; i < stringarray.Length;i++ ) { darray[i] = Convert.ToDouble(stringarray[i]); }

    Label1.Text += "double array........."; foreach (double d in darray) { Label1.Text += d.ToString() + ""; } }

    033.convertstringarraytointarray

    protected void Button1_Click(object sender, System.EventArgs e) { string[] stringarray = new string[] { "100", "99", "125", "122", "25"

    21

  • };

    Label1.Text = "string array........."; foreach (string s in stringarray) { Label1.Text += s + ""; }

    int[] intarray = new int[stringarray.Length]; for (int i = 0; i < stringarray.Length;i++ ) { intarray[i] = Convert.ToInt32(stringarray[i]); }

    Label1.Text += "int array........."; foreach (double d in intarray) { Label1.Text += d.ToString() + ""; } }

    034.convertdecimalarraytostring

    protected void Button1_Click(object sender, System.EventArgs e) { decimal[] decimalarray = new decimal[] { 1.23M, 2.99m, 10.0M, 2.88M, 4.5M };

    Label1.Text = "decimal array........."; foreach (decimal d in decimalarray) { Label1.Text += d.ToString() + ""; }

    string convertedstring = String.Join(", ", decimalarray); Label1.Text += "decimal array to converted string: " + convertedstring; }

    22

  • 035.decimalarraytostringarray

    protected void Button1_Click(object sender, System.EventArgs e) { decimal[] decimalarray = new decimal[] { 5.28M, 2.93M, 11.5M, 2.82M, 9.5M };

    Label1.Text = "decimal array........."; foreach (decimal d in decimalarray) { Label1.Text += d.ToString() + ""; }

    string[] stringarray = new string[decimalarray.Length]; for (int i = 0; i < decimalarray.Length;i++ ) { stringarray[i] = decimalarray[i].ToString(); //another way for conversion //stringarray[i] = Convert.ToString(decimalarray[i]); }

    Label1.Text += "converted string array........."; foreach (string s in stringarray) { Label1.Text += s + ""; } }

    036.convertdecimalarraytodoublearray

    protected void Button1_Click(object sender, System.EventArgs e) { decimal[] decimalarray = new decimal[] { 51.28M, 12.93M,

    23

  • 13.50M, 29.84M, 8.0M };

    Label1.Text = "decimal array........."; foreach (decimal d in decimalarray) { Label1.Text += d.ToString() + ""; }

    double[] darray = new double[decimalarray.Length]; for (int i = 0; i < decimalarray.Length;i++ ) { darray[i] = Convert.ToDouble(decimalarray[i]); }

    Label1.Text += "converted double array........."; foreach (double d in darray) { Label1.Text += d.ToString() + ""; } }

    037.stringarraytostring

    protected void Button1_Click(object sender, System.EventArgs e) { string[] stringarray = new string[] { "how", "are", "you" };

    Label1.Text = "string array........."; foreach (string s in stringarray) { Label1.Text += s + ""; }

    string convertedstring = String.Join("", stringarray); Label1.Text += "string array to converted string: " + convertedstring; }

    24

  • 038.stringarraytocommaseparatedstring

    protected void Button1_Click(object sender, System.EventArgs e) { string[] birds = new string[] { "Horned Screamer", "Cackling Goose", "Greylag Goose", "Andean Flamingo" };

    Label1.Text = "birds array........."; foreach (string s in birds) { Label1.Text += s + ""; }

    string commaseparatedstring = String.Join(",", birds); Label1.Text += "string array to comma separated string........"; Label1.Text += commaseparatedstring; }

    039.stringarraytocsv

    protected void Button1_Click(object sender, System.EventArgs e) { string[] colors = new string[] { "red", "green", "yellow", "blue", "pink", };

    Label1.Text = "colors array.........";

    25

  • foreach (string s in colors) { Label1.Text += s + ""; }

    string commaseparatedstring = String.Join(",", colors); string filename = Server.MapPath("csv.txt");

    using (StreamWriter file = new StreamWriter(filename)) { file.Write(commaseparatedstring); } }

    040.arraybinarysearch

    protected void Button1_Click(object sender, System.EventArgs e) { string[] colors = new string[] { "white", "green", "yellow", "blue", "pink", };

    Label1.Text = "colors array........."; foreach (string s in colors) { Label1.Text += s + ""; }

    //binary search work correctly only on a pre sorted array. Array.Sort(colors);

    Label1.Text += "sorted colors array........."; foreach (string s in colors) { Label1.Text += s + ""; }

    int index = Array.BinarySearch(colors, "pink"); Label1.Text += "index of color [pink] by binary search = " +

    26

  • index.ToString(); }

    041.arrayofobjects

    protected void Button1_Click(object sender, System.EventArgs e) { object[] objectarray = new object[6]; objectarray[0] = "first element"; objectarray[1] = null; objectarray[2] = true; objectarray[3] = 2.99; objectarray[4] = DateTime.Now; objectarray[5] = 15;

    Label1.Text = "object array and data type........."; foreach (object o in objectarray) { if(o != null) { Label1.Text += o.ToString() + " = "; Label1.Text += o.GetType() + ""; } } }

    042.arrayconcat

    protected void Button1_Click(object sender, System.EventArgs e) { string[] birds = new string[] { "Manx Shearwater", "Andean Flamingo", "Chilean Flamingo" };

    string[] morebirds = new string[]

    27

  • { "Roseate Spoonbill", "Little Bittern" };

    Label1.Text = "birds array........."; foreach (string s in birds) { Label1.Text += s + ""; }

    Label1.Text += "more birds array........."; foreach (string s in morebirds) { Label1.Text += s + ""; }

    string[] totalbirds =new string[birds.Length + morebirds.Length]; birds.CopyTo(totalbirds, 0); morebirds.CopyTo(totalbirds,birds.Length);

    //another way by linq to concatenate two array //var totalbirds = birds.Concat(morebirds);

    Label1.Text += "total birds array [after concatenate] twoarrays........."; foreach (string s in totalbirds) { Label1.Text += s + ""; } }

    043.arrayclear

    protected void Button1_Click(object sender, System.EventArgs e) { int[] numbers = new int[] { 100, 99, 98, 92 };

    28

  • string[] colors = { "red","green","pink",};

    Label1.Text = "numbers array........."; foreach (int i in numbers) { Label1.Text += i.ToString() + ""; }

    Label1.Text += "colors array........."; foreach (string s in colors) { Label1.Text += s + ""; }

    //sets a range of elements in the Array to zero Array.Clear(numbers,0,numbers.Length);

    Label1.Text += "numbers array [after clear]........."; foreach (int i in numbers) { Label1.Text += i.ToString() + ""; }

    //sets array elements null Array.Clear(colors, 0, colors.Length);

    Label1.Text += "colors array [after clear]........."; foreach (string s in colors) { Label1.Text += s + ""; } }

    044.arrayclone

    protected void Button1_Click(object sender, System.EventArgs e) { string[] birds = new string[] { "Green Heron", "Grey Heron", "Goliath Heron" };

    29

  • string[] clonedbirds = birds.Clone() as string[];

    Label1.Text = "birds array........."; foreach (string s in birds) { Label1.Text += s + ""; } Label1.Text += "cloned birds array........."; foreach (string s in clonedbirds) { Label1.Text += s + ""; }

    clonedbirds[1] = "Great Blue Heron"; birds[2] = "Striated Heron";

    Label1.Text += "birds array [after update one element from eacharray]...."; foreach (string s in birds) { Label1.Text += s + ""; } Label1.Text += "cloned birds array [after update one element from eacharray]..."; foreach (string s in clonedbirds) { Label1.Text += s + ""; } }

    045.createarraywithnondefaultvalue

    protected void Button1_Click(object sender, System.EventArgs e) { string[] stringarray = Enumerable.Repeat("c# examples",3).ToArray(); int[] intarray = Enumerable.Repeat(100, 4).ToArray(); Boolean[] booleanarray = Enumerable.Repeat(true,2).ToArray();

    Label1.Text = "string array........."; foreach (string s in stringarray) { Label1.Text += s + ""; }

    30

  • Label1.Text += "int array........."; foreach (int i in intarray) { Label1.Text += i.ToString() + ""; }

    Label1.Text += "boolean array........."; foreach (Boolean b in booleanarray) { Label1.Text += b.ToString() + ""; } }

    046.arraydeleteelement

    protected void Button1_Click(object sender, System.EventArgs e) { string[] birds = new string[] { "Crimson Sunbird", "Italian Sparrow", "Russet Sparrow", "Swahili Sparrow", "Painted Finch" };

    Label1.Text = "birds array........."; foreach (string s in birds) { Label1.Text += s + ""; }

    List birdsbirdslist = birds.ToList(); birdslist.RemoveAt(2);

    Array.Resize(ref birds,birds.Length -1);

    for(int i=0;i

  • foreach (string s in birds) { Label1.Text += s + ""; } }

    047.arraydeleteduplicateelements

    protected void Button1_Click(object sender, System.EventArgs e) { string[] colors = new string[] { "red", "green", "yellow", "red", "violet", "yellow" };

    Label1.Text = "colors array........."; foreach (string s in colors) { Label1.Text += s + ""; }

    var distinctcolors = colors.Distinct().ToArray();

    Array.Resize(ref colors, distinctcolors.Length);

    for (int i = 0; i < colors.Length; i++) { colors[i] = distinctcolors[i]; }

    Label1.Text += "distinct colors array........."; foreach (string s in colors) { Label1.Text += s + ""; } }

    32

  • 048.arraydistinct

    protected void Button1_Click(object sender, System.EventArgs e) { string[] birds = new string[] { "Chestnut Munia", "Java Sparrow", "Western Bluebill", "Chestnut Munia", "Water Pipit", "Water Pipit" };

    Label1.Text = "birds array........."; foreach (string s in birds) { Label1.Text += s + ""; }

    string[] distinctbirds = birds.Distinct().ToArray();

    Label1.Text += "distinct birds array........."; foreach (string s in distinctbirds) { Label1.Text += s + ""; } }

    049.arraydimensionlength

    protected void Button1_Click(object sender, System.EventArgs e) { string[,] birds = { {"Fairy","Penguin"}, {"African","Penguin"}, {"Magellanic","Penguin"}, {"Sooty","Albatross"}, {"Laysan","Albatross"} };

    33

  • int rows = birds.GetUpperBound(0); int columns = birds.GetUpperBound(1);

    Label1.Text = "birds array.....";

    for (int currentRow = 0; currentRow

  • foreach (string s in colors) { Label1.Text += s + ""; if (!morecolors.Contains(s)) { morecolorsarraynotexists.AppendFormat("{0} ", s); } else { commoncolors.AppendFormat("{0} ", s); } }

    StringBuilder colorsarraynotexists = new StringBuilder();

    Label1.Text += "more colors array........."; foreach (string s in morecolors) { Label1.Text += s + ""; if (!colors.Contains(s)) { colorsarraynotexists.AppendFormat("{0} ", s); } }

    Label1.Text += "" + morecolorsarraynotexists + "is not exists in morecolorsarray"; Label1.Text += colorsarraynotexists + "is not exists in colors array"; Label1.Text += commoncolors + "is common colors in both array"; }

    *******************************thanks for reading our asp.net c# examples ebook free copy. to buy full copy of this

    ebook please visit http://asp-net-example.blogspot.com

    35