Tips and Traps Flashcards

https://app.pluralsight.com/library/courses/c-sharp-tips-traps/table-of-contents

1
Q

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

A

string.IsNullOrWhitespace(mystring)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

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

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

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

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How to format strings to perfectly fit in a table?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

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

A

$”Hello {user.name, -20}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How to do conditional string formatting?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

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

A

StringBuilder class. performance and readability reasons.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How to write a formatter once and use it everywhere?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

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

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

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

A

by installing the Ref12 extension

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

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

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What happens to the hour depending on the DateTimeStyles?

A

It might apply the time zone difference in the output.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

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

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

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

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

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

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

How Enumerable class can be used?

A

to generate sequence of values.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

What is tuples and when were they introduced?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

How to declare a tuple? why is it used?

A

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

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

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

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

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

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

What is [Flag]?

A

better allow code editor to operate over bit flags.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

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

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

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

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
24
Q

How to force a reference equality?

A

Object.ReferenceEquals(obj1, obj2)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
Q

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

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
26
Q

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

A

By using Path.Combine method. Yes.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
27
Q

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

A

yes..ChangeExtension. GetFileName… WithoutExtension.

28
Q

How to get invalid file name chars?

A

Path.GetInvalidFileNameChars.

29
Q

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

A

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

30
Q

How to create a relative URI?

A

new Uri(“/Index.html”, UriKind.Relative)

31
Q

How to create more complex URIs with more options?

A

UriBuilder class

32
Q

How to deal with zip files?

A

ZipFile class

33
Q

How to compress and decompress bytes (anything can be array of bytes)

A

By using FileProcessor and GZipStream classes.

34
Q

Is calling a virtual method from the constructor considered safe?

A

No… you might get null ptrs depending on the inherited classe’s implementations.

35
Q

what does the params does to an array of string argument?

A

remove the need of defining an instance of array but defining each piece of the array separated by ,

36
Q

How to implement an indexer on c# (myInstance[2])?

A

by adding a public property: public byte this[int i ] get…; set…;

37
Q

What is expression-bodied members and when was it introduced?

A

it is pretty much arrow functions defined in the method declaration. Introduced on C# 7 (2017)

38
Q

What is a partial class and when is it used?

A

means that a class can be implemented in different source code files… used most for auto generated code

39
Q

What is dotPeek?

A

Free tool from jetBrains that allow look into binaries and see what is made of (classes and methods, everything)

40
Q

What is a partial method?

A

partial method that may or not be implemented in another cs file

41
Q

What is a local function? how to implement it?

A

a function that is declared inside another function… implemented just like a regulat function but without any visibility keyword

42
Q

How to create a namespace alias?

A

using =

43
Q

How to use a reserved word as a param name?

A

@byte

44
Q

What is a preprocessor directive? What is the symbol that every directive start with?

A

Is a instruction that may affect the output or behavior of the build processor. starts with #

45
Q

What are the most common preprocesor directives?

A

symbol directive, conditional directive, compilation feedback, organizational and non-nullable reference

46
Q

Is there predefined preprocessor directives for debug, release and target .net version?

A

yes

47
Q

How to create a property group in the csproj that will affect all .net standard versions?

A

….

48
Q

How to emit a preprocessor compiler warning (which is usually used with preprocessor conditionals)?

A

warning This target framework is NOT supported.

49
Q

What does the[Conditional(“)] data annotation des to a method?

A

make the method work only if the directive is true. the method is not going to be called when is false.

50
Q

How to make an internal method visible to a specific.namespace?

A

[assembly: InternalVisibleTo(“specific.namespace”)]

51
Q

how to make the a method obsolete? Where will it appear?

A

[Obsolete(“This will be removed in v2”)]. Throws a compilation warning.

52
Q

What is binary compatibility? When does it work?

A

Is when a dll can be copied to an older version of the code and it still works. When signatures are not changed.

53
Q

How to convert binary or any array of byte to base64?

A

By using the class and method Convert.ToBase64String(bytes)

54
Q

How to convert primitive types to binary format (array of bytes)? When is it useful?

A

By using the method BitConverter.GetBytes(50). For de/serialization and for low level network stuff.

55
Q

How to convert one type to another without typecast?

A

via the use of: Convert.ChangeType(value, typeof(double))

56
Q

How to get a type by using its FQN string representation?

A

Type myobj = Type.GetType(“System.Object”);

57
Q

What is the difference between a cast and as?

A

cast will thrown an exception if the cast fails, as will return null.

58
Q

What happens when an static class throws an exception at the constructor? What if I try catch the exception?

A

it crashes the app. If try catched, every attempt to use that type will result in an exception as well.

59
Q

How to deffer heavy object creation (aka lazy constructor)?

A

By using the Lazy class which wrapps the class and creates it only when it is actually used.

60
Q

What does the await Task.Delay(2000) do?

A

suspend the current thread for 2s

61
Q

What does the Zip method do?

A

join two sequences. must have the same count.

62
Q

what are the concat, union, intersect and except?

A

do the operation into the two sequences.

63
Q

What is the CallerMemberName data attribute?

A

is a decorate method that allows accessing information about the caller method. useful for events.

64
Q

What is the difference between & and &&? What is the order of evaluation of two booleans? Does the same apply for ||?

A

& does not short circuit and && short circuit operations. Evaluated left to right.

Yes, applies to ||

65
Q

How to preserve the stack trace when rethrowing exceptions?

A

by not using the exception var.. e.g:
catch (Exception){
Throw;
}

66
Q

What is the null coalescing? how does the syntax look like? Can it be chained?

A

shorthand for null checks and value attribution.

var asd = mynullvar ?? “this string will be used instead of nullvar”.

yes ?? can ?? be ?? chained

67
Q

Will mynullvar?.mymethod() be called? can it be combined with null coalescing?

A

no… yes very useful both together.