Tips and Traps Flashcards Preview

.NET / C# > Tips and Traps > Flashcards

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

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

string.IsNullOrWhitespace(mystring)

2

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.

3

On which C# version the string interpolation ($"hello {user.name}") 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:
https://docs.microsoft.com/pt-br/dotnet/csharp/whats-new/csharp-version-history

4

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.

5

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

$"Hello {user.name, -20}

6

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)

7

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

StringBuilder class. performance and readability reasons.

8

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))

9

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.

10

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

by installing the Ref12 extension

11

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)

12

What happens to the hour depending on the DateTimeStyles?

It might apply the time zone difference in the output.

13

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.

14

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).

15

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.

16

How Enumerable class can be used?

to generate sequence of values.

17

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).

18

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.

19

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...
}

20

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

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

21

What is [Flag]?

better allow code editor to operate over bit flags.

22

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)

23

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

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

24

How to force a reference equality?

Object.ReferenceEquals(obj1, obj2)

25

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.

26

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

By using Path.Combine method. Yes.

27

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

yes..ChangeExtension. GetFileName... WithoutExtension.

28

How to get invalid file name chars?

Path.GetInvalidFileNameChars.

29

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.

30

How to create a relative URI?

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