Verified Commit 51b8af5e authored by Kiryuu Sakuya's avatar Kiryuu Sakuya 🎵
Browse files

Initial commit

parents
.vscode/
.vs/
\ No newline at end of file

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.30204.135
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CSharpTutorial", "CSharpTutorial\CSharpTutorial.csproj", "{74F98888-3B8F-41F9-8518-0049FF324FF4}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{40CC5698-E42C-4742-BB02-067CA8A777D0}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{74F98888-3B8F-41F9-8518-0049FF324FF4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{74F98888-3B8F-41F9-8518-0049FF324FF4}.Debug|Any CPU.Build.0 = Debug|Any CPU
{74F98888-3B8F-41F9-8518-0049FF324FF4}.Release|Any CPU.ActiveCfg = Release|Any CPU
{74F98888-3B8F-41F9-8518-0049FF324FF4}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {F7B793CF-38D6-4A48-98CF-652D03652F56}
EndGlobalSection
EndGlobal
# OS junk files
[Tt]humbs.db
*.DS_Store
# Visual Studio / MonoDevelop
*.[Oo]bj
*.exe
*.dll
*.pdb
*.user
*.aps
*.pch
*.vspscc
*.vssscc
*_i.c
*_p.c
*.mdb
*.ncb
*.suo
*.tlb
*.tlh
*.bak
*.ilk
*.lib
*.sbr
*.sdf
*.opensdf
*.resources
*.res
ipch/
obj/
[Bb]in
[Dd]ebug*/
[Rr]elease*/
Ankh.NoLoad
*.gpState
.vscode/
*.exp
.vs/
# Tooling
_ReSharper*/
*.resharper
.idea/*
[Tt]est[Rr]esult*
*.orig
*.rej
# NuGet packages
!.nuget/*
[Pp]ackages/*
![Pp]ackages/repositories.config
# Temporary Files
~.*
~$*
\ No newline at end of file
using System;
using System.Collections.Generic;
using System.Text;
namespace CSharpTutorial
{
class Book
// Tell us what the book is
{
public string title;
public string author;
public int pages;
// Create a method
public Book(string inputTitle, string inputAuthor, int inputPages)
{
// This is a constructor, 构造函数
// Anytime you make a method like this, this is going to be the constructor of the class.
// Basically, this is a special method inside in our class, and it's gonna be called when we create an object of this class.
title = inputTitle;
author = inputAuthor;
pages = inputPages;
}
public Book() {
// Do nothing :D
}
}
}
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>
</Project>
using System;
using System.Collections.Generic;
using System.Text;
namespace CSharpTutorial
{
public class Chef
{
public void MakeChicken()
{
Console.WriteLine("The Chef makes chicken");
}
public void MakeSalad()
{
Console.WriteLine("The Chef makes salad");
}
// virtual means this method can be overwritten in any subclasses.
public virtual void MakeSpecialDish()
{
Console.WriteLine("The Chef makes bbq ribs");
}
}
}
\ No newline at end of file
using System;
using System.Collections.Generic;
using System.Text;
namespace CSharpTutorial
{
// Awesome chef, it can do everything normal chef do.
// Which means, inheritance everything from a class like chef, to a class like ItalianChef.
public class ItalianChef : Chef
{
public void MakePasta() {
Console.WriteLine("The Chef makes pasta");
}
// Override :D
public override void MakeSpecialDish()
{
Console.WriteLine("The Chef makes chicken parm");
}
}
}
\ No newline at end of file
using System;
using System.Collections.Generic;
using System.Text;
namespace CSharpTutorial
{
class Movie
{
public string title;
public string director;
// public -> access by anyone
// private -> only the code inside this Movie class can access it
private string rating;
public Movie(string inputTitle, string inputDirector, string inputRating)
{
title = inputTitle;
director = inputDirector;
// not rating, but Rating!
// Actually calling the setter down there :D
Rating = inputRating;
}
// Getters and setters
public string Rating
{
// Allow us to read the rating.
get
{
return rating;
}
// Allow us to modify the rating.
set
{
// value represent anyting passed in
if (value == "G" || value == "PG" || value == "PG-13" || value == "R" || value == "NR")
{
rating = value;
}
else
{
rating = "NR";
}
}
}
}
}
using System;
namespace CSharpTutorial
{
class Program
{
static void Main(string[] args)
{
String characterName = "John";
int characterAge;
characterAge = 35;
Console.WriteLine("There once was a man named " + characterName);
Console.WriteLine("He was " + characterAge + " years old");
characterName = "251";
Console.WriteLine("He really liked the name " + characterName);
Console.WriteLine("But didn't like being " + characterAge);
// Keep the window up!
Console.ReadLine();
// --------------------------------------------------------------
string phrase = "ur mom gay";
char grade = 'A';
int age = 22;
double gpa = 3.3;
bool _isMale = true;
// --------------------------------------------------------------
string _phrase = "Fuck\nYou" + "\nlol";
Console.WriteLine(_phrase.Length);
Console.WriteLine(_phrase.ToUpper());
Console.WriteLine(_phrase.Contains("You"));
Console.WriteLine(_phrase[1]); // Start with 0
Console.WriteLine(_phrase.IndexOf("You")); // Where the "You" starts at.
Console.WriteLine(_phrase.Substring(5, 3)); // Grab a part of the string, set where to start, then where to end.
// --------------------------------------------------------------
Console.WriteLine(-1.114514);
Console.WriteLine(69 + 420);
Console.WriteLine(Math.Abs(-40)); // Returns the absolute value
Console.WriteLine(Math.Pow(3, 2)); // Returns a specified number raised to the specified power, like 3^2
Console.WriteLine(Math.Sqrt(36)); // Returns the square root of a specified number, like 6.
Console.WriteLine(Math.Max(251, 420));
Console.WriteLine(Math.Min(69, 251));
Console.WriteLine(Math.Round(4.6)); // 4.6->5, 4.3->4
// --------------------------------------------------------------
Console.Write("Enter your name: ");
string name = Console.ReadLine();
Console.Write("Enter your age: ");
string _age = Console.ReadLine();
Console.WriteLine("Hello, " + name + ", your age is " + _age);
// --------------------------------------------------------------
int num = Convert.ToInt32("45");
// "56asdf" won't work.
Console.WriteLine(num + 6);
Console.Write("Enter a number: ");
double num1 = Convert.ToDouble(Console.ReadLine());
Console.Write("Enter another number: ");
double num2 = Convert.ToDouble(Console.ReadLine());
Console.WriteLine(num1 + num2);
// --------------------------------------------------------------
string color, pluralNoun, celebrity;
Console.Write("Please enter your favourite color: ");
color = Console.ReadLine();
Console.Write("Please enter a plural none: ");
pluralNoun = Console.ReadLine();
Console.Write("Please enter your favourite celebrity: ");
celebrity = Console.ReadLine();
Console.WriteLine("Roses are " + color);
Console.WriteLine(pluralNoun + " are blue");
Console.WriteLine($"I love {celebrity}");
// --------------------------------------------------------------
int[] luckyNumbers = {1, 7, 9, 69, 251, 420};
string[] friends = new string[3]; // Only hold 3 values
friends[0] = "Jesus";
friends[1] = "ppy";
Console.WriteLine(luckyNumbers[4]);
luckyNumbers[2] = 8;
Console.WriteLine(luckyNumbers[2]);
// --------------------------------------------------------------
// Methods
SayHi("251", 9);
int cubedNumber = cube(5);
Console.WriteLine(cubedNumber);
bool isMale = false;
bool isTall = true;
if (isMale && isTall) {
// Both of them must be true
// Or: isMale || isTall
// One of them must be true
Console.WriteLine("You are a tall male!");
} else if (isMale && !isTall) {
Console.WriteLine("You are a short male!");
} else if (!isMale && isTall) {
Console.WriteLine("You are not a male but you are tall!");
} else {
Console.WriteLine("You are either not male or not tall, or both!");
}
Console.WriteLine(GetMax(251, 69, 420));
// --------------------------------------------------------------
Console.Write("Enter a number: ");
double _num1 = Convert.ToDouble(Console.ReadLine());
Console.Write("Enter Operator: ");
string op = Console.ReadLine();
Console.Write("Enter another number: ");
double _num2 = Convert.ToDouble(Console.ReadLine());
if (op == "+")
{
Console.WriteLine(_num1 + _num2);
} else if (op == "-") {
Console.WriteLine(_num1 - _num2);
} else if (op == "*") {
Console.WriteLine(_num1 * _num2);
} else if (op == "/") {
Console.WriteLine(_num1 / _num2);
} else {
Console.WriteLine("Invalid Operator.");
}
// --------------------------------------------------------------
Console.WriteLine(GetDay(1));
// --------------------------------------------------------------
int index = 1;
while (index <= 5) {
Console.WriteLine(index);
index++;
}
// or do {} while ( sth. );
// --------------------------------------------------------------
// Word guess game
string secretWord = "giraffe";
string guess = "";
int guessCount = 0;
int guessLimit = 3;
bool outOfGuesses = false;
do
{
if (guessCount < guessLimit)
{
Console.Write("Enter guess: ");
guess = Console.ReadLine();
guessCount++;
}
else
{
outOfGuesses = true;
}
} while (guess != secretWord && !outOfGuesses);
if (outOfGuesses == false) {
Console.WriteLine("You win!");
} else {
Console.WriteLine("You Lose!");
}
// --------------------------------------------------------------
int[] _luckyNumbers = { 1, 7, 9, 69, 251, 420 };
for (int i = 0; i < _luckyNumbers.Length; i++)
{
Console.WriteLine(_luckyNumbers[i]);
}
// --------------------------------------------------------------
Console.WriteLine(GetPow(3, 2));
// --------------------------------------------------------------
int[,] numberGrid = {
// int[,,] <- Three dimension
{1, 2},
{3, 4},
{5, 6}
};
// or do this
int[,] myArray = new int[2, 3];
Console.WriteLine(numberGrid[1, 1]);
// --------------------------------------------------------------
// Create Book.cs
Book book1 = new Book("Tanmy RBQ", "251", 400); // Book Object, like a phycial book.
// new Book(); <- This is being called in Book.cs
// define what the book is
// Old way:
// book1.title = "Tanmy RBQ";
// book1.author = "251";
// book1.pages = 400;
Book book2 = new Book("Tanmy is not RBQ", "251", 600);
// Old way:
// book2.title = "Tanmy is not RBQ";
// book2.author = "251";
// book2.pages = 600;
Book book3 = new Book();
Console.WriteLine(book2.title);
book2.title = "Nothing";
Console.WriteLine(book2.title);
// --------------------------------------------------------------
// Object Methods
Student student1 = new Student("Jim", "Business", 2.8);
Student student2 = new Student("Pam", "Art", 3.6);
Console.WriteLine(student1.HasHonors());
Console.WriteLine(student2.HasHonors());
// --------------------------------------------------------------
// Getters & Setters
Movie avengers = new Movie("The Avengers", "Joss Whedon", "PG-13");
// Rating: G, PG, PG-13, R, NR
// But you can easily do this:
// avengers.rating = "F@#K Rating";
// So how we can prevent this?
Movie shrek = new Movie("Shrek", "Adam Adamson", "PG");
/*
First, you need to colse the access of rating.
Now if you use this, it's not gonna work:
Console.WriteLine(avengers.rating);
Then, create a Getter & Setter for rating.
This will allow outside code to access, modify and set the rating.
You need a special method called property, 属性
*/
// Then it's safe!
Console.WriteLine(avengers.Rating);
avengers.Rating = "Cat";
Console.WriteLine(avengers.Rating);
avengers.Rating = "R";
Console.WriteLine(avengers.Rating);
// Just make your class more secure!
// --------------------------------------------------------------
// Static Class Attributes
Song holiday = new Song("Holiday", "Green Day", 200);
Console.WriteLine(Song.songCount);
Song kashmir = new Song("Kashmir", "Led Zeppelin", 150);
Console.WriteLine(Song.songCount);
Console.WriteLine(kashmir.getSongCount());
// songCount is a static attribute belong to the class.
// You can't use Song.artist, because it's the object of the class, not the class itself.
Console.WriteLine(holiday.title);
// --------------------------------------------------------------
// Static Methods & Classes
// Example: Math is a static method, which belongs to the class
// We don't need to create instance of the class to use it
Console.WriteLine(Math.Sqrt(144));
// Create UseFulTools.cs
// Access the method!
// Cannot declare a variable of static type 'UseFulTools'
// if you use static class UseFulTools
// UseFulTools tools= new UseFulTools();
UseFulTools.SayHi("Mike");
// --------------------------------------------------------------
// Inheritance 继承
Chef chef = new Chef();
chef.MakeChicken();
chef.MakeSpecialDish();
ItalianChef italianChef = new ItalianChef();
// Calling on the ItalianChef
italianChef.MakeChicken();
italianChef.MakePasta();
// Overwrite a method
italianChef.MakeSpecialDish();
// --------------------------------------------------------------
// Methods
static void SayHi(string name, int age)
{
Console.WriteLine($"Hello {name}, you are {age}!");
}
static int cube(int num)
{
int result = num * num * num;
return result;
}
static int GetMax(int num1, int num2, int num3)
{
int result;
if (num1 >= num2 && num1 >= num3)
{
result = num1;
}
else if (num2 >= num1 && num2 >= num3)
{
result = num2;
}
else
{
result = num3;
}
return result;
}
static string GetDay(int dayNum)
{
string dayName;
switch (dayNum)
{
case 0:
dayName = "Sunday";
break;
case 1:
dayName = "Monday";
break;
case 2:
dayName = "Tuesday";
break;
case 3:
dayName = "Wednesday";
break;
case 4:
dayName = "Thursday";
break;
case 5:
dayName = "Friday";
break;
case 6:
dayName = "Saturday";
break;
default:
dayName = "Invalid Day Number";
break;
}