Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Converting C# Basics to use .NET Fiddle #498

Merged
merged 40 commits into from
Sep 20, 2024
Merged
Show file tree
Hide file tree
Changes from 36 commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
887f3f9
Fixing statement on C#
webreidi Sep 16, 2024
ef261fb
Update write-to-console.md
webreidi Sep 16, 2024
acfcca7
fix date
webreidi Sep 16, 2024
4e8f859
Changing graphic to remove top level statements
webreidi Sep 16, 2024
3dfdd4c
Update comments.md
webreidi Sep 16, 2024
7c0aa44
fixing date
webreidi Sep 16, 2024
8f9e2b6
Fixing grammar and examples
webreidi Sep 16, 2024
d43689d
Add heading
webreidi Sep 16, 2024
34f23c6
Add Replit link
webreidi Sep 16, 2024
74bbb8b
Minor updates to operators lesson
webreidi Sep 16, 2024
f25dbec
Adding Mac to where C# can run
webreidi Sep 16, 2024
ebb7202
just types in control-structures
webreidi Sep 17, 2024
f7cd502
fixing date
webreidi Sep 17, 2024
fd22f7e
clarity updates for methods
webreidi Sep 17, 2024
72b0415
cleaning up objects and classes
webreidi Sep 17, 2024
b0f3f8f
Fixing typo
webreidi Sep 17, 2024
9bbfcb5
Fixing grammar
webreidi Sep 17, 2024
0b7d4bf
Removing extra spac
webreidi Sep 17, 2024
ba5692e
fixing C# to csharp
webreidi Sep 17, 2024
f020914
moving c# to csharp for code blocks
webreidi Sep 17, 2024
ba29a60
formatting if, for, and while in intro paragraph
webreidi Sep 17, 2024
d29edac
Update and rename using-replit.md to using-netfiddle.md
webreidi Sep 17, 2024
7857327
Adding new graphic for .NET Fiddle
webreidi Sep 17, 2024
1da6580
Finishing the conversion for the Using page
webreidi Sep 17, 2024
3609627
Adding .NET Fiddle screen captures
webreidi Sep 17, 2024
50e2957
Fixing a couple of misses
webreidi Sep 18, 2024
fc1abec
Replacing replit on Print Statements page
webreidi Sep 18, 2024
e91279c
replacing replit on Comments page
webreidi Sep 18, 2024
43247ed
Replacing replit in variables page
webreidi Sep 18, 2024
e6334af
Replacing replit on Operators page
webreidi Sep 18, 2024
af0e118
fixing an embedded fiddle in variables
webreidi Sep 18, 2024
ae5e00c
replacing replit on control structures page
webreidi Sep 18, 2024
98f8f59
replacing replit ion Methods page
webreidi Sep 18, 2024
1598c00
replacing replit on Classes page
webreidi Sep 18, 2024
4045c10
fixing typo
webreidi Sep 18, 2024
b61191c
Updating fiddle instructions
webreidi Sep 18, 2024
8a9dbb2
Updating to fix the intro to Fiddle
webreidi Sep 19, 2024
2af112b
Merge branch 'master' into webreidi-csharpBasicsConversionToFiddle
webreidi Sep 19, 2024
d9f4736
[Github Actions] Add AI translations
webreidi Sep 19, 2024
c818301
Revert "[Github Actions] Add AI translations"
Sep 20, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions content/english/csharp-basics/_index.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
title: "C#: Basics"
description: "An introduction to the basics of the C# programming language."
date: 2023-09-12T00:00:00Z
date: 2024-09-16T00:00:00Z
prereq: "none"
difficulty: "Beginner"
draft: false
Expand All @@ -18,7 +18,7 @@ The C# programming language was designed by Anders Hejlsberg from Microsoft in 2
A high-level programming language is called high-level because it is quite a few steps away from the original code run on a computer's Central Processing Unit (CPU). High-level code is meant to be used by humans and is much easier to understand than low-level code. The high-level code is later translated into a low-level language, which can only be perceived and run by a specific CPU. For the code to work, every single line of code written in a high-level language must be translated into machine language before being implemented by the computer.
{{% /notice %}}

C# is one of the most popular programming languages in the world, along with Java, JavaScript, Python and others. C# runs on Windows and various Linux operating systems.
C# is one of the most popular programming languages in the world, along with Java, JavaScript, Python and others. C# can run on any computer, including on Windows, Mac, and any Linux Operating systems.

<details open>
<summary>Topics</summary>
Expand Down
68 changes: 40 additions & 28 deletions content/english/csharp-basics/classes-and-objects.md
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
---
title: "Classes and Objects"
description: "Introduce classes and objects in C#."
date: 2023-09-13T00:00:00Z
date: 2024-09-16T00:00:00Z
weight: 8
---

## What is an Object and what is a Class?

An object is an thing (noun) that has certain characteristics and can perform certain tasks. A Class is the blueprint or definition for that object when it is created.
An Object is a thing (noun) that has certain characteristics and can perform certain tasks. A Class is the blueprint or definition for that object when it is created.

An example is a Person. A Person is a kind of Class. A person can have certain characteristics that distinguish it from another person. It can have blue eyes, it can be 15 years old, etc. A person can also perform certain tasks. It can walk, it can talk, etc.

A C# Object is an instance of a C# Class. In this instance "Bea" is a "Person". "Bea" has brown eyes. This is a property (or data field) of "Bea". "Bea" can speak in English. "Bea" can speak in Spanish. These are methods that "Bea" can perform.
A C# Object is an instance of a C# Class. In this instance "Bea" is a "Person". "Bea" has brown eyes. This is a property (or data field) of "Bea". "Bea" can speak in English. "Bea" can speak in Spanish. These are methods that "Bea" can perform.

## Creating a Class!

C# is an **object-oriented** programming language, which means everything in C# is associated with an **object** and a **class** (the blueprint for the object).

For starters, the line of code that we used to print statements from activity 1 is actually a method call from a predefined class called `System`!

```c#
```csharp
Console.WriteLine("Hello World");
```

Expand All @@ -28,9 +28,9 @@ Console.WriteLine("Hello World");

Another built-in class we have interacted with in the previous exercises is `string`. The `string` class defines a set of rules on how a list of characters should behave.

With following line of code, we created a `string` object called `name` using the rules defined in the `string` class:
With the following line of code, we created a `string` object called `name` using the rules defined in the `string` class:

```c#
```csharp
string name = "Patrick";
```

Expand All @@ -42,10 +42,6 @@ Let's learn about the different parts in a class below:
### Class
A class is a blueprint or prototype of a new type of object. In general, a class contains three important parts:

* data fields/instance variables
* constructor
* methods

<img src="../images/class.png" height="400" alt="A Class blueprint for an object contains instance varialbes/data fields which are data/attributes in the object, constructor which are methods that creates the obejct of the class, and methods, which are behaviors possible for the object."/>
webreidi marked this conversation as resolved.
Show resolved Hide resolved

**Element** | **Description** | **Example**
Expand All @@ -56,17 +52,34 @@ A class is a blueprint or prototype of a new type of object. In general, a class

For example:

```c#
public class SampleClass{
```csharp
public class Person{
// (1) data fields/instance variables
private String name; // example
private int age;
private int height;

// (2) constructor - constructors have the same name as the class
public SampleClass(){
name = "example";
public Person()
{
name = "Bea";
age = 29;
height = 167;
}

// (2) constructor - you can have more than one
public Person( String nameInput, int ageInput, int heightInput)
{
name = nameInput;
age = ageInput;
height = heightInput;
}

// (3) methods
public sampleMethod(){}
public void talk()
{
Console.WriteLine($"Hello from {name}");
}
}
```

Expand All @@ -75,23 +88,22 @@ public class SampleClass{
## Try it Out 🐥!

Let's make a `Bird` class to represent Patrick 🐥 and all of his Bird friends by following the steps below!
**Note:** For this exercise, you will be working in Bird.cs instead of Main.cs.

<a class="my-2 mx-4 btn btn-info" href="https://replit.com/@nuevofoundation/JavaBasicsBird" target="_blank">Launch Replit</a>
<iframe width="100%" height="475" src="https://dotnetfiddle.net/Widget/OH5XQO" frameborder="0"></iframe>

### First, we start by defining the class name in the format `public` `class` `name`.

```c#
```csharp
public class Bird{}
```

<br />

### Second, let's declare all the fields of the Bird class: species, name, hobby, age, loveMusic.

Each field is declared in the format: `private` `data type` `name` `;`.
Each field is declared in the format: `access specifier` `data type` `name` `;`.

1. We declare all 5 fields as `private` in the class `Bird`. This ensures these data can only be accessed within this class.
webreidi marked this conversation as resolved.
Show resolved Hide resolved
1. We declare all 5 fields as `private` in the class `Bird`. This ensures these fields can only be accessed within this class.

2. Let's identify the appropriate type for each field:

Expand All @@ -107,17 +119,17 @@ For example, to declare `species` as a private field of class `Bird`, you would

### Third, let's create the constructor for the class `Bird`.

Usually, constructor is the method that initialializes value to all the fields in a class. It has the format `public` `class name` `(parameter)`. Since we have 5 fields in this class, the constructor will take in 5 parameter/inputs.
Usually, constructor is the method that initializes value to all the fields in a class. It has the format `public` `class name` `(parameter)`. Since we have 5 fields in this class, the constructor will take in 5 parameter/inputs.

```c#
```csharp
public Bird(string speciesInput, string nameInput, string hobbyInput, int ageInput, bool loveMusicInput){
// constructor body
}
```

In the constructor body, we need to initialize all the instance variables, by assign each variables to its initial values:

```c#
```csharp
species = speciesInput;
name = nameInput;
hobby = hobbyInput;
Expand Down Expand Up @@ -145,7 +157,7 @@ After, let's write the `ToString()` method together!

We will return a String that include all fields of the `Bird`: name, age, species, hobby, loveMusic by calling the 5 methods you just implemented!

```c#
```csharp
public string ToString(){
// initialize variable info to have empty String
string info = "";
Expand Down Expand Up @@ -191,13 +203,13 @@ To create a new object of a particular class, we call the constructor of that cl

Recall that the constructor of `Bird` class is the following:

```C#
```csharp
public Bird(string speciesInput, string nameInput, string hobbyInput, int ageInput, bool loveMusicInput);
```

Hence, we can create a Bird with these attributes (species - duck; name - Patrick; hobby - hangout with friends; age - 15; loveMusic - true) with this line of code.

```c#
```csharp
Bird patrick = new Bird("duck", "Patrick", "hangout with friends", 15, true);
```

Expand All @@ -207,8 +219,8 @@ Try it out yourself and create multiple Birds of different species 🐦🐤🐔
Next, let's call the `ToString()` method on these `Bird` objects we created to print the information of our friends. You can do either of the following:

1. Print the return value of `ToString()` method. (i.e. `Console.WriteLine(patrick.ToString());`)
2. Directly print the `Bird` object, which prompt `ToString()` to be called in the background. (i.e. `Console.WriteLine(patrick);`).
2. Directly print the `Bird` object, which prompts `ToString()` to be called in the background. (i.e. `Console.WriteLine(patrick);`).

Try it out and print out all the information of the bird friends you created 🐦🐤🐔🐧!

<a class="my-2 mx-4 btn btn-info" href="https://replit.com/@nuevofoundation/CSharpBasicsClasses" target="_blank">Launch Replit</a>
<iframe width="100%" height="475" src="https://dotnetfiddle.net/Widget/fMXXuT" frameborder="0"></iframe>
9 changes: 8 additions & 1 deletion content/english/csharp-basics/comments.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
title: "Comments"
description: "Introduce comments in C#."
date: 2023-09-12T00:00:00Z
date: 2024-09-16T00:00:00Z
weight: 3
---

Expand All @@ -18,6 +18,13 @@ Also, adding comments will not affect the program whatsoever. So, you can add co
* multi-line
* comment */
```

## Play with Comments

In the .NET Fiddle frame below, add a single line comment and a multi-line comment to your program.

<iframe width="100%" height="475" src="https://dotnetfiddle.net/Widget/TTAhVm" frameborder="0"></iframe>

{{% notice tip %}}

You can comment you code so the computer won't run it.
Expand Down
16 changes: 8 additions & 8 deletions content/english/csharp-basics/control-structures.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
title: "Control Structures"
description: "Introduce if-else, for, and while loops in C#."
date: 2022-09-12T00:00:00Z
date: 2024-09-16T00:00:00Z
weight: 6
---

Expand Down Expand Up @@ -65,15 +65,15 @@ An **if-else statement** follows the structure shown above. If the boolean expre
The code above will print the statement `I like Music` because the boolean expression, `likeMusic == true`, is true.
{{% /notice %}}

Let's revisit the greating example above, and make decision for Patrick on whether to say "Good Morning," "Good Afternoon," "Good Night".
Let's revisit the greeting example above, and make decision for Patrick on whether to say "Good Morning," "Good Afternoon," "Good Night".

Patrick should say "Good Morning" between 0:00 - 11:59, "Good Afternoon" between 12:00 - 19:59, and "Good Night" between 20:00 - 23:59.

webreidi marked this conversation as resolved.
Show resolved Hide resolved
1. We have a variable `currentHour` that stores the current hour.
2. Fill out the correct `boolean expression A` and `boolean expression B` to complete the program.
3. Test your result by assigning the variable `currentHour` to numbers from 0 to 23:

<a class="my-2 mx-4 btn btn-info" href="https://replit.com/@nuevofoundation/CSharpBasicsControlStructures" target="_blank">Launch Replit</a>
<iframe width="100%" height="475" src="https://dotnetfiddle.net/Widget/T6AUdh" frameborder="0"></iframe>

## Repeat It Again: For-Loop, While-Loop

Expand Down Expand Up @@ -138,15 +138,15 @@ for(int number = 1; number <= 10; number = number + 1){

{{% /notice %}}

After learning about `for` loops and `while` loops, let's see how do we help Patrick 🐥 and calculate **1 + 2 + 3 + 4 ... + 99 + 100** easily?
After learning about `for` loops and `while` loops, let's see how do we help Patrick 🐥 calculate **1 + 2 + 3 + 4 ... + 99 + 100** easily?

Let's try to do it in a while loop:
Let's try to do it in a `while` loop:

1. We have a variable called `total` that tracks the total addition sum we get so far.
2. We have a variable called `num` that tracks the next number to be added to `total`.
3. Write out the appropriate `boolean expression` and write out the while loop.

By following those step we will get a while loop like this:
By following those step we will get a `while` loop like this:

```C#
int total = 0;
Expand All @@ -158,6 +158,6 @@ while(num <= 100){
Console.WriteLine("Answer calculated in a while loop: " + total);
```

Test this out below by clicking `Run` and write a for loop version that calculates the same thing! You should get the same answer!
Test this out below by clicking `Run` and write a `for` loop version that calculates the same thing! You should get the same answer!

<a class="my-2 mx-4 btn btn-info" href="https://replit.com/@nuevofoundation/CSharpBasicsLoops" target="_blank">Launch Replit</a>
<iframe width="100%" height="475" src="https://dotnetfiddle.net/Widget/ySFwK0" frameborder="0"></iframe>
Binary file modified content/english/csharp-basics/images/Step1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
16 changes: 10 additions & 6 deletions content/english/csharp-basics/methods.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
---
title: "Methods"
description: "Introduce methods in C#."
date: 2023-09-13T00:00:00Z
date: 2024-09-16T00:00:00Z
weight: 7
---

## What is a Method?

So far, we've learned about how to store data in variables, print data and sentences, and make decisions with if-statements, for-loops, and while-loops.
So far, we've learned about how to store data in variables, print data and sentences, and make decisions with `if`-statements, `for`-loops, and `while`-loops.

From the last exercise, we know the following block of code calculates the sum from 1 to 100:

Expand Down Expand Up @@ -35,11 +35,15 @@ There are 2 parts in a method: signature and body:

To define a method, we need to first write out its <b>method signature</b>. A signature header has four main parts:

```
access_specifier return_type method_name(list_of_parameters)
```

**Part Name** | **Description** | **Examples or possible options**
----|----|----
**access specifier** | provides the level of access to the method | `public` tells the computer that anyone can use this method. `private` tells the computer it can only be called within a class. (We'll learn about classes on the next page!) `protected` tells the computer it can be called by objects of the same class.
**return type** | data type that is returned to the calling function, technically the return type is not a part of the signaure in C# | `string` or `int`, use `void` if the method doesn't return anything.
**method name** | name of the method used to call it | a descriptive name you choose based on what the method does.
**method name** | name of the method, used to call it | a descriptive name you choose based on what the method does.
**list of parameters** | list of inputs that must be provided when the method is used | can have zero or more parameters in the form of (`type` `input name`, `type` `input name`, ... ). Use () for no parameters.

```
Expand Down Expand Up @@ -101,7 +105,7 @@ One of the reasons methods are powerful is we can call them more than once:
Console.WrlineLine(sum); // prints 10
```

For the curious we could have written the above to just print the return numbers without storing in a variable:
We could have written the above to just print the return numbers without storing them in a variable:
```c#
Console.WriteLine(sumNum(1,3)); // prints 6
Console.WriteLine(sumNum(1,4)); // prints 10
Expand All @@ -114,7 +118,7 @@ Here is an image of square-number pyramid where each level is a perfect square o

<img src="../images/pyramid.png" height="250" alt="pyramid with layers of colorful beads. Bottom layer is yellow, next blue, next brown, next white, next pink, next light blue, next orange, next coral, next green, last red" />

That is the top level has `1 * 1` bead, the 2nd level has `2 * 2` beads, and so on.
The top level has `1 * 1` bead, the 2nd level has `2 * 2` beads, and so on.

Let's write a method that takes in the number of total levels and output the total number of beads in the pyramid!

Expand All @@ -124,4 +128,4 @@ Once you solved the challenge, you will see the following message:
Congratulations! Challenge Solved!
```

<a class="my-2 mx-4 btn btn-info" href="https://replit.com/@nuevofoundation/CSharpBasicsMethods" target="_blank">Launch Replit</a>
<iframe width="100%" height="475" src="https://dotnetfiddle.net/Widget/ireaAA" frameborder="0"></iframe>
17 changes: 9 additions & 8 deletions content/english/csharp-basics/operators.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
title: "Operators"
description: "Introduce operators and perform operations on variables in C#."
date: 2023-09-12T00:00:00Z
date: 2024-09-16T00:00:00Z
weight: 5
---

Expand Down Expand Up @@ -37,19 +37,20 @@ The following are the arithmetic operators in C#:

**Note**: If both of the operands of division are integers, the result will also be an integer. For example, `10 / 4` returns 2, not 2.5 since we throw away the remainder. If either of the operands is a double, the result will be a double.

**Note**: Modulus operation get the remainder of the division operation.
**Note**: Use the modulus operation (`%`) get the remainder of the division operation.

{{% /notice %}}

{{% notice tip %}}
Add (`+`) is used on String as concatenation operator. For example:
The add (`+`) operator is used on Strings as a concatenation operator. For example:

`String name = "Patric" + "k";` is same as `String name = "Patrick";`
{{% /notice %}}

### Instructions
1. Use the program below to practice using the arithmetic operators. Change the numbers to see the answers.

<a class="my-2 mx-4 btn btn-info" href="https://replit.com/@nuevofoundation/CSharpBasicsOperators" target="_blank">Launch Replit</a>
<iframe width="100%" height="475" src="https://dotnetfiddle.net/Widget/dUSTOt" frameborder="0"></iframe>

## Comparisons: Relational Operator

Expand All @@ -70,7 +71,7 @@ The following are the relational operators in C#:
`<=` | less than or equal to | `(2 <= 2)` is `true`

Note: `==` and `!=` can only be used on data of the same type. For example:
```java
```csharp
int x=3;
double y=3.0;
x==y; // This would produce an error because x and y are of different types.
Expand All @@ -81,7 +82,7 @@ x==y; // This would produce an error because x and y are of different types.
{{% notice tip %}}
Using 1 equal sign assigns a value to the variable. Using 2 equal signs compares the values of two elements.

```java
```csharp
age = 3; // sets the value of age to 3
age == 3; // checks to see if the value of age is 3
```
Expand All @@ -91,7 +92,7 @@ age == 3; // checks to see if the value of age is 3

1. Use the program below to practice using the comparison operators. Change the numbers to see the answers.

<a class="my-2 mx-4 btn btn-info" href="https://replit.com/@nuevofoundation/CSharpBasicsRelationalOperators" target="_blank">Launch Replit</a>
<iframe width="100%" height="475" src="https://dotnetfiddle.net/Widget/tZs8tb" frameborder="0"></iframe>

## Exam Statistics!

Expand Down Expand Up @@ -138,4 +139,4 @@ Let's write it with the help of operators 🎵!

{{% /notice %}}

<a class="my-2 mx-4 btn btn-info" href="https://replit.com/@nuevofoundation/CSharpBasicsOperatorPractice" target="_blank">Launch Replit</a>
<iframe width="100%" height="475" src="https://dotnetfiddle.net/Widget/mM7xbj" frameborder="0"></iframe>
Loading