C# Flashcards
Statement to convert list of Chars to String like List charLst=new List(){'a','b','c'}; to string str='abc'
https://stackoverflow.com/questions/58524222/c-converting-list-of-chars-to-string using string constructor var myString = new string(charList.ToArray());
Time complexity of String(Char[] arr)
https://docs.microsoft.com/en-us/dotnet/api/system.string.-ctor?view=netcore-3.1
O(n) Initializes the new instance to the value indicated by an array of Unicode characters. This constructor copies Unicode characters
HashSet> hs=new HashSet>(); List lst=new List(){1,2,3}; List lst2=new List(){1,2,3}; hs.Add(lst); hs.Add(lst2); what will be in hs?
hashSet will contain
{1,2,3}
{1,2,3}
because both of them having difference reference
Given a dictionary var dict= new Dictionary< int , int > (); What will dictionary contain at 0 index? dict[0]=1; dict[0]=2;
First statement dict[0]
will add an entry in dictionary so dictionary will contain {0:1}
Second statement dict[0]=2
will override an existing entry in dictionary so dictionary will still contain
{0:2}
C# statement to convert char to Numeric value
Char.GetNumericValue(char)—> returns double
Syntax to declare tuple in C# 7.0 of double and int and initialize them with 4.5 and 3.0?
(double Sum, int Count) t2 = (4.5, 3);
Syntax to deconstruct a tuple instance in separate variable?
var t = ("post office", 3.6); (string destination, double distance) = t; Console.WriteLine($"Distance to {destination} is {distance} kilometers.");
Initialize two variables in one statement?
int a,b;
a=b=10;
What will result statement will contain below? string s="sands"; var results=s.Substring(5);
empty string
Given a url , how we can extract host from it. For example http://google.com/news how to extract google.com from it?
string url="http://google.com/news" string host=new Uri(url).Host; // google.com
How to access element in dictionary using index?
dictionary.ElementAt(0).Value will give us element at index 0 its value
How to calculate the remainder for the negative number?
int remainder=number%K;
if(remainder<0)
remainder=remainder+K;
How to generate random value from 0 to n ?
Math.Random(n+1);
How to sort an array in decreasing order rather than increasing order using Array.Sort?
There are different methods available 1. var sortedArray=arr.OrderByDescending(); 2. Comparer comparer=Comparer.Create((x,y)=> { return y.CompareTo(x); }); 3. Create custom comparer class