Skip to main content

C# Cheat Sheet

  • Variables
  • Data types
  • Operators
  • Conditional statements
  • Curls
  • Functions/Procedures
  • Tables/Lists
  • Objects
  • Classes
  • Paradigms
  • Heritage
  • Scope
  • Exceptions
  • Libraries/APIs
  • **Input/Output (input/output) **

Fundamentals with the C# language

Variables

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

Data types

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

Operators

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

Conditional Statements

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

Loops

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

Functions

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

**Paintings **

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

Objects and Classes

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

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

**Legacy **

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

**Scope **

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!");
}

Entry/Exit

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

Example code to create a c# quiz for 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();

}
}