230 likes | 392 Vues
This guide provides an overview of the foundational concepts in C# programming, focusing on class structure, the required syntax, and coding conventions. You'll learn about the essential components like classes, methods, and their declarations using various examples, including the placement of braces, semicolons, and indentation rules. It also covers error types in C#, including syntax and runtime errors, with illustrative examples. By the end of this guide, you will have a solid understanding of how to write and format basic C# programs.
E N D
C# Syntax and Output Lab 0A
A bare bones class! public class CompSci { } All C# programs start with a class.
bare bones + a Main! public class CompSci { public static void Main(String[] args) { Console.WriteLine("Comp Sci!"); } } OUTPUT Comp Sci!
syntax rules public class CompSci { //open brace public static void Main(String[] args) { Console.WriteLine ("Comp Sci!"); } } //close brace Braces – You gotta have ‘em! Every class and every method must have a { and a } .
syntax rules public class CompSci { public static void main(String[] args) { Console.WriteLine("Comp Sci!"); } } You must put a semi-colon at the end of all C# program statements ( ; ).
{ and ; rule Never put a ; before an open { brace ;{ //illegal }; //legal
indentation public class CompSci { public static void Main(String[] args) { Console.WriteLine("Comp Sci!"); } } Indent all code 3 spaces to make it easier to read. 123
C# Output reference command / method Console.Write("compsci"); OUTPUT compsci
C# Output Console.Write("compsci"); Console.Write("compsci"); OUTPUT compscicompsci
C# Output Console.WriteLine("compsci"); OUTPUT compsci
C# Output Console.WriteLine("compsci"); Console.WriteLine("compsci"); OUTPUT compscicompsci
C# Output Escape Sequences \n newline \t tab \r carriage return \b backspace Console.WriteLine("c\tompsci"); OUTPUT c ompsci
C# Output Escape Sequences \n newline \t tab \r carriage return \b backspace Console.WriteLine("com\tpsci"); OUTPUT com psci
C# Output Escape Sequences \n newline \t tab \r carriage return \b backspace Console.WriteLine("comp\nsci"); OUTPUT compsci
C# Output Escape Sequences \\ outs \ \" outs " \’ outs ’ Console.WriteLine("\\compsci\"/"); OUTPUT \compsci"/
C# Output Escape Sequences \\ outs \ \" outs " \’ outs ’ Console.WriteLine("\\'comp\'sci\'/"); OUTPUT \'comp'sci'/
C# Comments // single-line comments /* */ block comments //this line prints stuff on the screen Console.WriteLine("stuff");
C# Comments // single-line comments /* */ block comments /* this line prints stuff on the screen */ Console.WriteLine("stuff");
Errors Syntax errors occur when you type something in wrong, causing the code to not compile. //missing semicolon - ; expected Console.WriteLine("stuff") //case problem – should be System Console.WriteLine("stuff")
Errors Runtime errors occur when something goes wrong while the program is running. //an out of bounds exception is thrown String s = "runtime_error"; Console.WriteLine( s[15] );