diff --git a/.scripts/convertAndReplaceAll.bat b/.scripts/convertAndReplaceAll.bat index 90512b5..46a457e 100644 --- a/.scripts/convertAndReplaceAll.bat +++ b/.scripts/convertAndReplaceAll.bat @@ -1,3 +1,4 @@ +wsl bash renamePptx.sh call convertPptxToMd.bat REN img imgs diff --git a/.scripts/renamePptx.sh b/.scripts/renamePptx.sh new file mode 100644 index 0000000..51b04c2 --- /dev/null +++ b/.scripts/renamePptx.sh @@ -0,0 +1,3 @@ +for f in *.pptx; do + mv -- "$f" "$(echo "$f" | tr '[:upper:]' '[:lower:]' | tr ' ' '-')" +done \ No newline at end of file diff --git a/1. Introduction to C# and .NET.md b/1-introduction-to-csharp-and-dotnet.md similarity index 100% rename from 1. Introduction to C# and .NET.md rename to 1-introduction-to-csharp-and-dotnet.md diff --git a/1.5 Introduction to Visual Studio.md b/1.5-introduction-to-visual-studio.md similarity index 100% rename from 1.5 Introduction to Visual Studio.md rename to 1.5-introduction-to-visual-studio.md diff --git a/10-static-members-methods-and-classes-slides.html b/10-static-members-methods-and-classes-slides.html new file mode 100644 index 0000000..0862954 --- /dev/null +++ b/10-static-members-methods-and-classes-slides.html @@ -0,0 +1,148 @@ +
+

Static Members, Methods and Classes

+

+
+
+

Static Members

+

So far, we have used non-static fields in our classes

+

This means, that each instance of the class holds its own version of the field, and changing the value of it only affects that instance:

+

class MyAwesomeClass

+

{

+

public int MyProperty { get; set; }

+

}

+

class Program

+

{

+

static void Main(string[] args)

+

{

+

MyAwesomeClass instance1 = new MyAwesomeClass();

+

MyAwesomeClass instance2 = new MyAwesomeClass();

+

instance1.MyProperty = 100;

+

instance2.MyProperty = 200; // instance1.MyProperty is still 100

+

}

+

}

+

Static Members (continued)

+

Likewise, non-static class methods _have to _ be called through an instance:

+

class MyAwesomeClass

+

{

+

public void PrintText(string text) { Console.WriteLine(text); }

+

}

+

class Program

+

{

+

static void Main(string[] args)

+

{

+

MyAwesomeClass instance = new MyAwesomeClass();

+

instance.PrintText("Hello World"); // Outputs "Hello World"

+

MyAwesomeClass.PrintText("Hello World"); // Results in an error

+

}

+

}

+

+

Static fields are shared between all instances of a class

+

Let's declare "MyProperty" property with the __static __ keyword. Now it can be referenced through the class type name, but not through the instance, as shown below:

+

class MyAwesomeClass

+

{

+

public static int MyProperty { get; set; } = 100;

+

}

+

class Program

+

{

+

static void Main(string[] args)

+

{

+

MyAwesomeClass instance = new MyAwesomeClass();

+

Console.WriteLine(MyAwesomeClass.MyProperty); // Outputs "100"

+

Console.WriteLine(instance.MyProperty); // Results in an error

+

}

+

}

+

+

Static Members - Example

+

In this example, a static field is used for keeping count on how many times the class has been instantiated:

+

class Person

+

{

+

public static int totalPersons = 0;

+

private string name;

+

public Person(string personName) // Person Constructor

+

{

+

name = personName;

+

++totalPersons;

+

}

+

public void PrintInfo()

+

{

+

Console.WriteLine("This person is called " + name + ".");

+

Console.WriteLine("There are " + totalPersons + " persons total.");

+

}

+

}

+

Static Members - Example (continued)

+

Now let's instantiate a couple of persons and print their info:

+

class Program

+

{

+

static void Main(string[] args)

+

{

+

Person steve = new Person("Steve");

+

Person wendy = new Person("Wendy");

+

steve.PrintInfo();

+

wendy.PrintInfo();

+

}

+

}

+

+

Static Methods

+

Methods can also be static

+

What happens when you try to call a non-static method from a static method?

+

class Program

+

{

+

void PrintHelloName(string name)

+

{

+

Console.WriteLine("Hello, " + name);

+

}

+

static void Main(string[] args)

+

{

+

PrintHelloName(); // Will throw an error

+

}

+

}

+

+
+
+

This is why until this point all example methods have been inside of the main function

+

Static Classes

+
    +
  • Classes can also be made static +
      +
    • Static classes cannot be instantiated
    • +
    • All members of a static class also have to be static
    • +
    +
  • +
  • static class Styling
  • +
  • {
  • +
  • public static string fontFamily = "Verdana";
  • +
  • public static float fontSize = 12.5f;
  • +
  • }
  • +
  • class Program
  • +
  • {
  • +
  • static void Main(string[] args)
  • +
  • {
  • +
  • Console.WriteLine
  • +
  • ("Using font " + Styling.fontFamily + " " + Styling.fontSize + "px");
  • +
  • // Outputs "Using font Verdana 12.5px"
  • +
  • Styling = new Styling(); // Results in an error
  • +
  • }
  • +
  • }
  • +
+

Exercise 1

+

Create a class Message which has two __static __ properties int TotalMessages and string LastMessage, and a __non-static __ property string MessageText.

+

Add a constructor which takes a string message as a parameter, increases TotalMessages by one and sets the value of LastMessage to message which is the parameter

+

Create a main loop which keeps asking the user for a new message. A new Message instance is then created with the user input message as an argument:

+

Message newMessage = new Message(message);

+

newMessage is then added to a list of messages, allMessages

+

Finally the static values Message.TotalMessages and Message.LastMessage are printed

+
+
\ No newline at end of file diff --git a/10. Static Members, Methods and Classes.md b/10-static-members-methods-and-classes.md similarity index 100% rename from 10. Static Members, Methods and Classes.md rename to 10-static-members-methods-and-classes.md diff --git a/11. Delegates and Events.md b/11-delegates-and-events.md similarity index 100% rename from 11. Delegates and Events.md rename to 11-delegates-and-events.md diff --git a/12. Files and Streams-slides.html b/12-files-and-streams-slides.html similarity index 100% rename from 12. Files and Streams-slides.html rename to 12-files-and-streams-slides.html diff --git a/12. Files and Streams.md b/12-files-and-streams.md similarity index 100% rename from 12. Files and Streams.md rename to 12-files-and-streams.md diff --git a/13. Generics, IEnumerable and LINQ-slides.html b/13-generics-ienumerable-and-linq-slides.html similarity index 100% rename from 13. Generics, IEnumerable and LINQ-slides.html rename to 13-generics-ienumerable-and-linq-slides.html diff --git a/13. Generics, IEnumerable and LINQ.md b/13-generics-ienumerable-and-linq.md similarity index 100% rename from 13. Generics, IEnumerable and LINQ.md rename to 13-generics-ienumerable-and-linq.md diff --git a/14. Exceptions, Threads and Tasks-slides.html b/14-exceptions-threads-and-tasks-slides.html similarity index 100% rename from 14. Exceptions, Threads and Tasks-slides.html rename to 14-exceptions-threads-and-tasks-slides.html diff --git a/14. Exceptions, Threads and Tasks.md b/14-exceptions-threads-and-tasks.md similarity index 100% rename from 14. Exceptions, Threads and Tasks.md rename to 14-exceptions-threads-and-tasks.md diff --git a/15. Design Patterns in C#-slides.html b/15-design-patterns-in-csharp-slides.html similarity index 99% rename from 15. Design Patterns in C#-slides.html rename to 15-design-patterns-in-csharp-slides.html index 5e71d80..b02ee1d 100644 --- a/15. Design Patterns in C#-slides.html +++ b/15-design-patterns-in-csharp-slides.html @@ -32,7 +32,7 @@
  • While a static class could be used for this, there are some problems: