C# code snippet

Initialise min and max value:

int min  = int.MaxValue
int max = int.MinValue

Math:

Math.Round(2.6)
Math.Max(x,y)

String:

  • .ToUpper()
  • .Contains(str[i])
  • Ascending: (String.Compare(str1,str2) < 0)
  • string str = string.Empty;

  • Join:

    int[] arr = { 1, 2, 3, 4, 5 };
    string temp = string.Join(" ",arr);  O/P: 5 4 3 2 1

  • Reverse of string

    Note: Reverse() part of LINQ, so include using System.Linq;

    string str = "Abhishek";
    string temp = new string(str.Reverse().ToArray());

    OR,

    string str = "Abhishek";
    string temp = new string(str.ToCharArray().Reverse().ToArray());

    OR,

    string str = "Abhishek";
    string temp= string.Join("",str.ToCharArray().Reverse().ToArray());

    int[] arr = {1,2,3};
    int[] temp = arr.Reverse().ToArray();

Arrays:

int [] arr = new int [5];

arr.Contains(element); 

Ascending order:
    Array.Sort(arr)

Descending order:
    Array.Sort(
arr)
    Array.Reverse(arr)

Maximum element:
    arr.Max();

OR,
    Array.Sort(arr);
    max = arr[arr.Length-1];

Remove duplicates from the array:

int [] arr = {2,2,5,2,4};
int [] temp = arr.Distinct().ToArray();  O/P: 2,5,4
int countDistinct = arr.Distinct().Count(); O/P: 3

Tupple:

  • used to store a group of related values of different types 
  • used to return multiple values from a method

Practise....

Dictionary:

  • map.ContainsKey(key)

  • map.Values.Max()


LINQ:

  • Count vowels in a string
    • str.Count(c => "aeiouAEIOU".Contains(c));






Popular posts from this blog

Introduction to Docker

SOLID Principles

Nuget package | Pushing it to Azure Artifacts

WiX - Windows Installer XML

Working with Git

C# Memory Tricks: Learn How To Master The Garbage Collector

gRPC - Protobuf / Protocol Buffers

Custom Azure DevOps pipeline task extension

C# Questions Part 1