// Single-line comments start with // /* Multi-line comments look like this */ /// <summary> /// This is an XML documentation comment which can be used to generate external /// documentation or provide context help within an IDE /// </summary> //public void MethodOrClassOrOtherWithParsableHelp() {} // Specify the namespaces this source code will be using // The namespaces below are all part of the standard .NET Framework Class Libary using System; using System.Collections.Generic; using System.Dynamic; using System.Linq; using System.Net; using System.Threading.Tasks; using System.IO; // But this one is not: using System.Data.Entity; // In order to be able to use it, you need to add a dll reference // This can be done with the NuGet package manager: `Install-Package EntityFramework` // Namespaces define scope to organize code into "packages" or "modules" // Using this code from another source file: using Learning.CSharp; namespace Learning.CSharp { // Each .cs file should at least contain a class with the same name as the file // you're allowed to do otherwise, but shouldn't for sanity. public class LearnCSharp { // BASIC SYNTAX - skip to INTERESTING FEATURES if you have used Java or C++ before public static void Syntax() { // Use Console.WriteLine to print lines Console.WriteLine("Hello World"); Console.WriteLine( "Integer: " + 10 + " Double: " + 3.14 + " Boolean: " + true); // To print without a new line, use Console.Write Console.Write("Hello "); Console.Write("World"); /////////////////////////////////////////////////// // Types & Variables // // Declare a variable using <type> <name> /////////////////////////////////////////////////// // Sbyte - Signed 8-bit integer // (-128 <= sbyte <= 127) sbyte fooSbyte = 100; // Byte - Unsigned 8-bit integer // (0 <= byte <= 255) byte fooByte = 100; // Short - 16-bit integer // Signed - (-32,768 <= short <= 32,767) // Unsigned - (0 <= ushort <= 65,535) short fooShort = 10000; ushort fooUshort = 10000; // Integer - 32-bit integer int fooInt = 1; // (-2,147,483,648 <= int <= 2,147,483,647) uint fooUint = 1; // (0 <= uint <= 4,294,967,295) // Long - 64-bit integer long fooLong = 100000L; // (-9,223,372,036,854,775,808 <= long <= 9,223,372,036,854,775,807) ulong fooUlong = 100000L; // (0 <= ulong <= 18,446,744,073,709,551,615) // Numbers default to being int or uint depending on size. // L is used to denote that this variable value is of type long or ulong // Double - Double-precision 64-bit IEEE 754 Floating Point double fooDouble = 123.4; // Precision: 15-16 digits // Float - Single-precision 32-bit IEEE 754 Floating Point float fooFloat = 234.5f; // Precision: 7 digits // f is used to denote that this variable value is of type float // Decimal - a 128-bits data type, with more precision than other floating-point types, // suited for financial and monetary calculations decimal fooDecimal = 150.3m; // Boolean - true & false bool fooBoolean = true; // or false // Char - A single 16-bit Unicode character char fooChar = 'A'; // Strings -- unlike the previous base types which are all value types, // a string is a reference type. That is, you can set it to null string fooString = ""escape" quotes and add (new lines) and (tabs)"; Console.WriteLine(fooString); // You can access each character of the string with an indexer: char charFromString = fooString[1]; // => 'e' // Strings are immutable: you can't do fooString[1] = 'X'; // Compare strings with current culture, ignoring case string.Compare(fooString, "x", StringComparison.CurrentCultureIgnoreCase); // Formatting, based on sprintf string fooFs = string.Format("Check Check, {0} {1}, {0} {1:0.0}", 1, 2); // Dates & Formatting DateTime fooDate = DateTime.Now; Console.WriteLine(fooDate.ToString(