Thursday, November 8, 2012

Practice c#


//Declare Array
int[] array = new int[] { 3, 1, 4, 5, 2 };


// Sort Method used Asending Order
Array.Sort( array );


// reverse Method used Decenting Order
Array.Reverse( array );


1. Remove duplicate elements from an array


static void Main(string[] args) { 

    var ints = new[] { 1, 2, 3, 4, 4, 4, 5, 6, 6, 6, 5, 3, 3 }; 

    var no_dupes = ints.Distinct(); //That's it.  Magic. 

 

    foreach (int i in no_dupes) 

        Console.Write(i + ","); //1,2,3,4,5,6,

    Console.ReadKey(); 



2. Suppose you want to eliminate duplicate elements from the array

int[] source = { 7, 4, 1, 3, 9, 8, 6, 7, 2, 1, 8, 15, 8, 23}

and sort the elements in descending order using LINQ in Asp.Net framework 4.0. Which of the following statements can you use?


var result = (from s in source orderby s descending select s).Distinct();



3.  
Boxing is an explicit conversion from the  value type to a type object
Unboxing is an explicit conversion from the type object to a value type


int i = 123;          // A value type
object box = i;       // Boxing
int j = (int)box;     // Unboxing


4.

string[] a = {"a","b","a","c","b","b","c","a"}; 
var b = a.Distinct().ToArray();

Output
string[]b = {"a","b","c"}







No comments: