Chapter 2: Speaking C# Flashcards

1
Q

non-generic types

A

System.Collections.Hashtable

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

generic types

A

System.Collections.Generic.Dictionary<TKey, TValue>
List<T></T>

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

Events are built on …..

A

delegates

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

define delegate

A

public int MethodIWantToCall(string input)
{
return input.Length; // it doesn’t matter what the method does
}

delegate int DelegateWithMatchingSignature(string s);

// create a delegate instance that points to the method
DelegateWithMatchingSignature d = new(p1.MethodIWantToCall);
// call the delegate, who then calls the method
int answer2 = d(“Frog”);

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

What statement can you type in a C# file to discover the compiler and language version?

A

error version

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

How to output the SDK version?

A

dotnet –version

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

Enabling a specific language version compiler

A

<LangVersion>7.3</LangVersion>

7, 7.1, 7.2, 7.3, 8, 9,
10, 11
latestmajor
latest
preview

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

How to add LangVersion in .csproj file?

A

<Project>
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<LangVersion>preview</LangVersion>
</PropertyGroup>
</Project>

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

What is the difference between a verbatim string and an interpolated string?

A

interpolated string:
string hello = $”Hello, {name}!”;
verbatim string:
string hello = @”Hello, {name}!”;

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

Raw string literal:

A

Characters enclosed in three or more double-quote characters.

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

Verbatim string:

A

A literal string prefixed with @ to disable escape characters so that a backslash
is a backslash. It also allows the string value to span multiple lines because the whitespace
characters are treated as themselves instead of instructions to the compiler

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

Interpolated string:

A

A literal string prefixed with $ to enable embedded formatted variables.

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

sizeof()

A

returns the number of bytes that a type uses in memory

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

you should only use double when…

A

you should only use double when accuracy, especially when comparing the equality of two numbers, is not important

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

… suffix means a decimal literal value

A

decimal c = 0.1M;
M suffix means a decimal literal value

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

The float and double types have some useful special values:

A

NaN represents not-a-number (for example,
the result of dividing by zero), Epsilon represents the smallest positive number that can be stored in
a float or double, and PositiveInfinity and NegativeInfinity represent infinitely large positive
and negative values. They also have methods for checking for these special values like IsInfinity
and IsNan.

17
Q

dynamic types

A

that can also store any type of data.
but unlike object, the value stored in the variable can have its members invoked without
an explicit cast.

18
Q

write the below variables with var keywords
int population = 67_000_000;
double weight = 1.88;
decimal price = 4.99M;
string fruit = “Apples”;
char letter = ‘Z’;
bool happy = true;

A

var population = 67_000_000;
var weight = 1.88;
var price = 4.99M;
var fruit = “Apples”;
var letter = ‘Z’;
var happy = true;

19
Q

target-typed new to instantiate objects

A

XmlDocument xml3 = new();

20
Q

Some examples of value types in C# are:

A

bool, int, long, float, byte, short, decimal, double, char, enum, struct

21
Q

Some examples of reference types in C# are:

A

object, string, dynamic, class, interface, array, delegate

22
Q

How can you write statements that will compile only for the specified platforms?

A

if NET7_0_ANDROID

// compile statements that only works on Android
#elif NET7_0_IOS
// compile statements that only works on iOS
#else
// compile statements that work everywhere else
#endif

23
Q

How to make a request for Apple’s home page?

A

HttpClient client = new();
HttpResponseMessage response = await client.GetAsync(“http://www.apple.com/”);
WriteLine(“Apple’s home page has {0:N0} bytes.”, response.Content.Headers.ContentLength);

24
Q

What statement can you type in a C# file to discover the compiler and language version?

A

error version

25
Q

How can you determine how many bytes a type like double uses in memory?

A

sizeof()