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
The float and double types have some useful special values:
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.
dynamic types
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.
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;
var population = 67_000_000;
var weight = 1.88;
var price = 4.99M;
var fruit = “Apples”;
var letter = ‘Z’;
var happy = true;
target-typed new to instantiate objects
XmlDocument xml3 = new();
Some examples of value types in C# are:
bool, int, long, float, byte, short, decimal, double, char, enum, struct
Some examples of reference types in C# are:
object, string, dynamic, class, interface, array, delegate
How can you write statements that will compile only for the specified platforms?
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
How to make a request for Apple’s home page?
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);
What statement can you type in a C# file to discover the compiler and language version?
error version
How can you determine how many bytes a type like double uses in memory?
sizeof()