Tips and Traps Flashcards Preview

.NET / C# > Tips and Traps > Flashcards

Flashcards in Tips and Traps Deck (67)
Loading flashcards...

How to check if a string is empty or filled with empty chars?



How to check the unicode category? when is it usefull?

char.GetUnicodeCategory(mychar) //also applicable for other primitive types;

useful for checking if the char is indeed unicode char (reading from an iot device or other source) also for finding globalization information such as currency crash and etc.


On which C# version the string interpolation ($"hello {}") was added, how was it done before this feature?

included on C# 6 (2015). via concatenation, string builders and/or formaters.

full list of features here:


How to format strings to perfectly fit in a table?

Using methods like PadRight(max String size). string. Format and interpolation itself also has a way to define a positive or negative offset value.


How to define a 20 char padding to the left in the interpolated string?

$"Hello {, -20}


How to do conditional string formatting?

by creating a string separated by ; defining masks #, -# and n/a so that the formatting wil be applied as myint.ToString(myFormattingString)


What to use to build complex strings? What are the two other reasons to use it?

StringBuilder class. performance and readability reasons.


How to write a formatter once and use it everywhere?

By creating a class implementing an IFormatProvider and ICustomFormatter interfaces. Then use it as a String.Format(new myClassName(args))


How to parse a string to int, how to define a number style?

by int.Parse(myString). By adding a second argument: int.Parse(myString, NumberStyles.Integer). NumberStyles allows bit flag operations.


How to make visual studio's f12 open the docs?

by installing the Ref12 extension


How to format a string to date? What is a safer way to do it? How to assume different locales for conversion?

by using DateTime.Parse(string). DateTime.ParseExact(string, "MM/dd/yyyy", null). By defining the third parm DateTimeStyles.AssumeUniversal, for eg)


What happens to the hour depending on the DateTimeStyles?

It might apply the time zone difference in the output.


How to hold incredible big integers on .net? What should I worry about using it? Waht happens to the math.max method?

By using the BigInteger class. You might get out of memory. you'll need to use BigInteger.Max instead.


How to generate random numbers (and arrays)? What is the default seed?

By making use of the Random class. The default seed is the system clock (better use a single instance of random - not thread safe).


How to create a crypto random value? what it generates?

by using RNGCryptoServiceProvider class. generates array of bytes that can be converted to other types.


How Enumerable class can be used?

to generate sequence of values.


What is tuples and when were they introduced?

A way to combine primitive types and structures into a single structure. Introduced on c# 7 (2017).


How to declare a tuple? why is it used?

by using parenthesis: private (int Length, string Name) ...

used to reduce the amount of non-necessary-structures.


What is a bit flag? How each option's number is defined?

Is a way (usually used with enums) to define a configuration via enum that allows multiple selections.

Each number options is defined as (sum of all previous + 1):
[Flags] public enum DinnerItems {
None = 0,
Entree = 1,
Appetizer = 2,
Side = 4,
Dessert = 8,
Beverage = 16,
BarBeverage = 32,
All = Entree | Appetizer | Side...


How to easily check if an bit flag enum has a specific option enabled?

DinnerItems.HasFlag(Side). Also allows combination values optionX | optionY


What is [Flag]?

better allow code editor to operate over bit flags.


Why equality of complex structures (including strings) are slow? How to improve the performance?

Because it uses reflection. By overriding the Equals method and performing manual comparison (without reflection)


What happens when you compare two Uri instances of a class? Why?

It compares the CONTENT! Because URI class overriden the equality method.


How to force a reference equality?

Object.ReferenceEquals(obj1, obj2)


What does the @ means in the beginning of a string?

Means that the object is a literal, so no special string treatment from the language... it means exactly what it looks.


How to easily create path in C#? Does the drive needs a trailing backslash?

By using Path.Combine method. Yes.


Does the Path class has other useful method? Cite 3 examples.

yes..ChangeExtension. GetFileName... WithoutExtension.


How to get invalid file name chars?



How to create URI in .net? How it behaves for local files?

By using the URI class... local/network files are perfectly translated to proper locations with the file:// prefix.


How to create a relative URI?

new Uri("/Index.html", UriKind.Relative)