Chapter 2: Speaking C# Flashcards
non-generic types
System.Collections.Hashtable
generic types
System.Collections.Generic.Dictionary<TKey, TValue>
List<T></T>
Events are built on …..
delegates
define delegate
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”);
What statement can you type in a C# file to discover the compiler and language version?
error version
How to output the SDK version?
dotnet –version
Enabling a specific language version compiler
<LangVersion>7.3</LangVersion>
7, 7.1, 7.2, 7.3, 8, 9,
10, 11
latestmajor
latest
preview
How to add LangVersion in .csproj file?
<Project>
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<LangVersion>preview</LangVersion>
</PropertyGroup>
</Project>
What is the difference between a verbatim string and an interpolated string?
interpolated string:
string hello = $”Hello, {name}!”;
verbatim string:
string hello = @”Hello, {name}!”;
Raw string literal:
Characters enclosed in three or more double-quote characters.
Verbatim string:
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
Interpolated string:
A literal string prefixed with $ to enable embedded formatted variables.
sizeof()
returns the number of bytes that a type uses in memory
you should only use double when…
you should only use double when accuracy, especially when comparing the equality of two numbers, is not important
… suffix means a decimal literal value
decimal c = 0.1M;
M suffix means a decimal literal value