Tuesday, June 23, 2015

C# syntax, system library functions should be memorized


Try to use them if the interviewer allows to do so.

string

Property
  • Length
Method

  • ToCharArray()
  • Remove(index, length)
  • IndexOf(c)

Array

Initialization
  • // Single-dimensional array (numbers).
    int[] n1 = new int[4] {2, 4, 6, 8};
    int[] n2 = new int[] {2, 4, 6, 8};
    int[] n3 = {2, 4, 6, 8};
    // Single-dimensional array (strings).
    string[] s1 = new string[3] {"John", "Paul", "Mary"};
    string[] s2 = new string[] {"John", "Paul", "Mary"};
    string[] s3 = {"John", "Paul", "Mary"};
    
    // Multidimensional array.
    int[,] n4 = new int[3, 2] { {1, 2}, {3, 4}, {5, 6} };
    int[,] n5 = new int[,] { {1, 2}, {3, 4}, {5, 6} };
    int[,] n6 = { {1, 2}, {3, 4}, {5, 6} };
    
    // Jagged array.
    int[][] n7 = new int[2][] { new int[] {2,4,6}, new int[] {1,3,5,7,9} };
    int[][] n8 = new int[][] { new int[] {2,4,6}, new int[] {1,3,5,7,9} };
    int[][] n9 = { new int[] {2,4,6}, new int[] {1,3,5,7,9} };

Method
  • Array.Reverse(arr)
  • Array.Sort(arr)

List

Method

  • Add()
  • AddRange()


HashTable / Dictionary

Property
  • Count
  • Keys
  • Values
Method
  • Contains(K)
  • ContainsKey(K) (same as Contains)
  • ContainsValue(V)

HastSet

Method
  • ToArray()

Stack

Property
  • Count
Method

  • Push()
  • Pop()
  • Peek()

Queue

Property
  • Count
Method
  • Enqueue()
  • Dequeue()
  • Peek()

File Operation

Read file in steam mode

            using (FileStream contents = File.OpenRead(fileName))
            using (StreamReader reader = new StreamReader(contents))
            {
                while (!reader.EndOfStream)
                {
                    Console.WriteLine(reader.ReadLine());
                }
            }

No comments:

Post a Comment