c#

474 Words
// C# Syntax Overview Example using System; // Class definition class Car { public string Brand; public int Year; public Car(string brand, int year) { Brand = brand; Year = year; } public void Honk() { Console.WriteLine(Brand + " goes beep beep!"); } } class Program { // Function declaration static int Add(int a, int b) { return a + b; } static void Main(string[] args) { // Variables and Data Types int age = 25; double weight = 70.5; char grade = 'A'; bool isStudent = true; string name = "Alice"; // Output Console.WriteLine("Name: " + name); Console.WriteLine("Age: " + age); // Conditional Statements if (age > 18) { Console.WriteLine("Adult"); } else { Console.WriteLine("Minor"); } // Loops for (int i = 0; i 18) { Console.WriteLine("Adult"); } else { Console.WriteLine("Minor"); } // Loops for (int i = 0; i 18) { Console.WriteLine("Adult"); } else { Console.WriteLine("Minor"); } // Loops for (int i = 0; i < 5; i++) { Console.Write(i + " "); } Console.WriteLine(); // Function call int result = Add(5, 3); Console.WriteLine("Sum: " + result); // Object creation Car myCar = new Car("Chevrolet", 2020); myCar.Honk(); } }
Free reading for new users
Scan code to download app
Facebookexpand_more
  • author-avatar
    Writer
  • chap_listContents
  • likeADD