Aller au contenu principal

C# Cheat Sheet

  • **Variables **
  • **Types de données **
  • **Opérateurs **
  • **Instructions conditionnelles **
  • **Boucles **
  • **Fonctions/Procédures **
  • **Tableaux/Listes **
  • **Objets **
  • **Classes **
  • **Paradigmes **
  • **Héritage **
  • **Portée **
  • **Exceptions **
  • **Bibliothèques/API **
  • **Entrée/Sortie(input/output) **

Fondamentaux avec le langage C#

**Variables **

int age = 25;
string name = "John";

**Types de données **

int num = 10;
double pi = 3.14;
bool isActive = true;
char letter = 'A';

**Opérateurs **

int sum = 5 + 10;
int difference = 20 - 5;

**Instructions conditionnelles **

if (age > 18)
{
Console.WriteLine("Majeur ");
}
else
{
Console.WriteLine("Mineur");
}

**Boucles **

for (int i = 0; i < 5; i++)
{
Console.WriteLine(i);
}

**Fonctions **

void SayHello()
{
Console.WriteLine("Hello!");
}

**Tableaux **

int[] numbers = {1, 2, 3, 4, 5};

**Objets et Classes **

public class Person
{
public string Name { get; set; }
public int Age { get; set; }
}

Person person = new Person() { Name = "John", Age = 25 };

**Héritage **

public class Employee Person
{
public string JobTitle { get; set; }
}

**Portée **

public int globalVar = 10;

public void SomeMethod()
{
int localVar = 5;
}

**Exceptions **

try
{
int result = 10 / 0;
}
catch (DivideByZeroException e)
{
Console.WriteLine("impossible de diviser par zero!");
}

**Entrée/Sortie **

string userInput = Console.ReadLine();
Console.WriteLine("Vous avez écrit" + userInput);

Exemple de code pour créer un quiz c# pour unity

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class QuizCode : MonoBehaviour
{
public List<QuestionAndAnswers> QnA;
public GameObject[] options;
public int currentQuestion;
public Text QuestionTxt;

private void Start()
{
generateQuestion();
}

public void correct()
{
QnA.RemoveAt(currentQuestion);
generateQuestion();
}

void setAnswers()
{
for(int i = 0; i < options.Length; i++)
{
options[i].GetComponent<AnswerScript>().isCorrect = false;
options[i].transform.GetChild(0).GetComponent<Text>().text = QnA[currentQuestion].Answers[i];

if (QnA[currentQuestion].CorrectAnswer == i + 1)
{
options[i].GetComponent<AnswerScript>().isCorrect = true;
}

}
}

void generateQuestion()
{
currentQuestion = Random.Range(0, QnA.Count);

QuestionTxt.text = QnA[currentQuestion].Question;
setAnswers();

}
}