fix lecture 12-15

main
borb 2 weeks ago
parent 88c8c953c6
commit 76b49a6af8

File diff suppressed because one or more lines are too long

@ -217,15 +217,15 @@ The following reads the first 20 characters of a file and prints them to the con
```csharp ```csharp
string path = @"C:\Users\Public\TestFolder\TestFile.txt"; string path = @"C:\Users\Public\TestFolder\TestFile.txt";
int nOfBytesToPrint = 20; int numberOfBytesToPrint = 20;
byte[] byteArray = new byte[nOfBytesToPrint]; byte[] byteArray = new byte[numberOfBytesToPrint];
if (!File.Exists(path)) if (!File.Exists(path))
Console.WriteLine($"The file {path} does not exist."); Console.WriteLine($"The file {path} does not exist.");
else else
using (FileStream fs = File.OpenRead(path)) using (FileStream fs = File.OpenRead(path))
{ {
fs.Read(byteArray, 0, nOfBytesToPrint); fs.Read(byteArray, 0, numberOfBytesToPrint);
}; };
// No need to close the file since it was handled inside of a using block // No need to close the file since it was handled inside of a using block

File diff suppressed because one or more lines are too long

@ -47,32 +47,30 @@ class Program
### Multiple type parameters ### Multiple type parameters
Generic classes can receive multiple types as parameters: * Generic classes can receive multiple types as parameters:
```csharp
```csharp class CustomContainer<T1, T2, T3>
class CustomContainer<T1, T2, T3>
{
public T1 First { get; set; }
public T2 Second { get; set; }
public T3 Third { get; set; }
}
class Program
{
static void Main(string[] args)
{ {
CustomContainer<int, string, DateTime> container public T1 First { get; set; }
= new CustomContainer<int, string, DateTime>(); public T2 Second { get; set; }
container.First = 10; public T3 Third { get; set; }
container.Second = "Testing.";
container.Third = DateTime.Now;
} }
} class Program
``` {
static void Main(string[] args)
{
CustomContainer<int, string, DateTime> container
= new CustomContainer<int, string, DateTime>();
container.First = 10;
container.Second = "Testing.";
container.Third = DateTime.Now;
}
}
```
### Creating a generic Method ### Creating a generic Method
```csharp ```csharp
void GenericMethodExample<T>(T value) void GenericMethodExample<T>(T value)
{ {
Console.WriteLine Console.WriteLine
@ -83,7 +81,7 @@ GenericMethodExample<string>("ABC");
![](imgs/13%20Generics%2C%20IEnumerable%20and%20LINQ_2.png) ![](imgs/13%20Generics%2C%20IEnumerable%20and%20LINQ_2.png)
* ***Note:*** You could name the generic type as anything, e.g. `<GenericType>`. It is named `<T>` by convention. * ***Note:*** You could name the generic type as anything, e.g. `<GenericType>`. It is named `<T>` here by convention.
## Exercise 1: Initializing a populated list ## Exercise 1: Initializing a populated list
<!-- _class: exercise --> <!-- _class: exercise -->

File diff suppressed because one or more lines are too long

@ -250,7 +250,7 @@ class Program
{ {
int waitTime = 5000; int waitTime = 5000;
await Task.Delay(waitTime); await Task.Delay(waitTime);
// Replace this with your time consuming async functionality // Replace above with some time-consuming async function
return new string( return new string(
$"The task took {waitTime / 1000} seconds to finish." $"The task took {waitTime / 1000} seconds to finish."
); );
@ -260,7 +260,7 @@ class Program
Console.WriteLine("Starting operation..."); Console.WriteLine("Starting operation...");
Task<string> resultTask = GetResultAsync(); Task<string> resultTask = GetResultAsync();
Console.WriteLine("Operation has been started."); Console.WriteLine("Operation has been started.");
Console.WriteLine("In the meantime, tell me something nice: "); Console.WriteLine("In the meantime, say something nice:");
string somethingNice = Console.ReadLine(); string somethingNice = Console.ReadLine();
resultTask.Wait(); // Wait for the task to complete resultTask.Wait(); // Wait for the task to complete
Console.WriteLine(resultTask.Result); Console.WriteLine(resultTask.Result);
@ -280,5 +280,5 @@ class Program
## Exercise 3: Asynchronous Loading ## Exercise 3: Asynchronous Loading
<!-- _class: exercise --> <!-- _class: exercise -->
Re-create the exercise 2, but instead of using separate threads for "loading the data", use an asynchronous method `LoadDataAsync()` Redo exercise 2, but instead of using separate threads for "loading the data", use an asynchronous method `LoadDataAsync()`.

File diff suppressed because one or more lines are too long

@ -69,7 +69,6 @@ A singleton implementation could look something like this:
<div markdown='1'> <div markdown='1'>
```csharp ```csharp
class Program class Program
{ {
static void Main(string[] args) static void Main(string[] args)
@ -123,11 +122,9 @@ public class HomeController : Controller
## Design Patterns ## Design Patterns
* If the concepts of a singleton and dependency injection flew over your head, don't worry about it * If the concepts of a singleton and dependency injection flew over your head, don't worry about it
* The important thing is to know they exist so that when they come up again in ASP.NET, you have already familiarized yourself with the terms * The important thing is to know they exist so that when they come up again in ASP.NET, you have already familiarized yourself with the terms
* Thus, understanding the logic behind ASP.NET becomes less overwhelming * Thus, understanding the logic behind ASP.NET becomes less overwhelming
* There are many more design patterns, see the material [here](https://www.c-sharpcorner.com/UploadFile/bd5be5/design-patterns-in-net/) * There are many more design patterns, see the material [here](https://www.c-sharpcorner.com/UploadFile/bd5be5/design-patterns-in-net/)
```csharp ```csharp
public void ConfigureServices(IServiceCollection services) public void ConfigureServices(IServiceCollection services)
{ {

Loading…
Cancel
Save