mirror of
https://github.com/adambard/learnxinyminutes-docs.git
synced 2024-12-23 09:41:36 +00:00
Proofread parts of C#
This commit is contained in:
parent
4b18cc66f8
commit
322122c80f
@ -158,7 +158,7 @@ on a new line! ""Wow!"", the masses cried";
|
|||||||
|
|
||||||
// Arrays - zero indexed
|
// Arrays - zero indexed
|
||||||
// The array size must be decided upon declaration
|
// The array size must be decided upon declaration
|
||||||
// The format for declaring an array is follows:
|
// The format for declaring an array is
|
||||||
// <datatype>[] <var name> = new <datatype>[<array size>];
|
// <datatype>[] <var name> = new <datatype>[<array size>];
|
||||||
int[] intArray = new int[10];
|
int[] intArray = new int[10];
|
||||||
|
|
||||||
@ -172,7 +172,7 @@ on a new line! ""Wow!"", the masses cried";
|
|||||||
|
|
||||||
// Lists
|
// Lists
|
||||||
// Lists are used more frequently than arrays as they are more flexible
|
// Lists are used more frequently than arrays as they are more flexible
|
||||||
// The format for declaring a list is follows:
|
// The format for declaring a list is
|
||||||
// List<datatype> <var name> = new List<datatype>();
|
// List<datatype> <var name> = new List<datatype>();
|
||||||
List<int> intList = new List<int>();
|
List<int> intList = new List<int>();
|
||||||
List<string> stringList = new List<string>();
|
List<string> stringList = new List<string>();
|
||||||
@ -182,14 +182,14 @@ on a new line! ""Wow!"", the masses cried";
|
|||||||
// Lists don't default to a value;
|
// Lists don't default to a value;
|
||||||
// A value must be added before accessing the index
|
// A value must be added before accessing the index
|
||||||
intList.Add(1);
|
intList.Add(1);
|
||||||
Console.WriteLine("intList @ 0: " + intList[0]);
|
Console.WriteLine("intList at 0: " + intList[0]);
|
||||||
|
|
||||||
// Others data structures to check out:
|
// Other data structures to check out:
|
||||||
// Stack/Queue
|
// Stack/Queue
|
||||||
// Dictionary (an implementation of a hash map)
|
// Dictionary (an implementation of a hash map)
|
||||||
// HashSet
|
// HashSet
|
||||||
// Read-only Collections
|
// Read-only Collections
|
||||||
// Tuple (.Net 4+)
|
// Tuple (.NET 4+)
|
||||||
|
|
||||||
///////////////////////////////////////
|
///////////////////////////////////////
|
||||||
// Operators
|
// Operators
|
||||||
@ -222,20 +222,20 @@ on a new line! ""Wow!"", the masses cried";
|
|||||||
| Bitwise inclusive OR
|
| Bitwise inclusive OR
|
||||||
*/
|
*/
|
||||||
|
|
||||||
// Incrementations
|
// Incrementing
|
||||||
int i = 0;
|
int i = 0;
|
||||||
Console.WriteLine("\n->Inc/Dec-rementation");
|
Console.WriteLine("\n->Inc/Dec-rement");
|
||||||
Console.WriteLine(i++); //Prints "0", i = 1. Post-Incrementation
|
Console.WriteLine(i++); //Prints "0", i = 1. Post-Increment
|
||||||
Console.WriteLine(++i); //Prints "2", i = 2. Pre-Incrementation
|
Console.WriteLine(++i); //Prints "2", i = 2. Pre-Increment
|
||||||
Console.WriteLine(i--); //Prints "2", i = 1. Post-Decrementation
|
Console.WriteLine(i--); //Prints "2", i = 1. Post-Decrement
|
||||||
Console.WriteLine(--i); //Prints "0", i = 0. Pre-Decrementation
|
Console.WriteLine(--i); //Prints "0", i = 0. Pre-Decrement
|
||||||
|
|
||||||
///////////////////////////////////////
|
///////////////////////////////////////
|
||||||
// Control Structures
|
// Control Structures
|
||||||
///////////////////////////////////////
|
///////////////////////////////////////
|
||||||
Console.WriteLine("\n->Control Structures");
|
Console.WriteLine("\n->Control Structures");
|
||||||
|
|
||||||
// If statements are c-like
|
// If statements are C-like
|
||||||
int j = 10;
|
int j = 10;
|
||||||
if (j == 10)
|
if (j == 10)
|
||||||
{
|
{
|
||||||
@ -288,7 +288,7 @@ on a new line! ""Wow!"", the masses cried";
|
|||||||
// For Each Loop
|
// For Each Loop
|
||||||
// foreach loop structure => foreach(<iteratorType> <iteratorName> in <enumerable>)
|
// foreach loop structure => foreach(<iteratorType> <iteratorName> in <enumerable>)
|
||||||
// The foreach loop loops over any object implementing IEnumerable or IEnumerable<T>
|
// The foreach loop loops over any object implementing IEnumerable or IEnumerable<T>
|
||||||
// All the collection types (Array, List, Dictionary...) in the .Net framework
|
// All the collection types (Array, List, Dictionary...) in the .NET framework
|
||||||
// implement one or both of these interfaces.
|
// implement one or both of these interfaces.
|
||||||
// (The ToCharArray() could be removed, because a string also implements IEnumerable)
|
// (The ToCharArray() could be removed, because a string also implements IEnumerable)
|
||||||
foreach (char character in "Hello World".ToCharArray())
|
foreach (char character in "Hello World".ToCharArray())
|
||||||
@ -297,7 +297,7 @@ on a new line! ""Wow!"", the masses cried";
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Switch Case
|
// Switch Case
|
||||||
// A switch works with the byte, short, char, and int data types.
|
// A switch works with byte, short, char, and int data types.
|
||||||
// It also works with enumerated types (discussed in Enum Types),
|
// It also works with enumerated types (discussed in Enum Types),
|
||||||
// the String class, and a few special classes that wrap
|
// the String class, and a few special classes that wrap
|
||||||
// primitive types: Character, Byte, Short, and Integer.
|
// primitive types: Character, Byte, Short, and Integer.
|
||||||
@ -316,7 +316,7 @@ on a new line! ""Wow!"", the masses cried";
|
|||||||
break;
|
break;
|
||||||
// You can assign more than one case to an action
|
// You can assign more than one case to an action
|
||||||
// But you can't add an action without a break before another case
|
// But you can't add an action without a break before another case
|
||||||
// (if you want to do this, you would have to explicitly add a goto case x
|
// (if you want to do this, you would have to explicitly add a goto case x)
|
||||||
case 6:
|
case 6:
|
||||||
case 7:
|
case 7:
|
||||||
case 8:
|
case 8:
|
||||||
@ -337,14 +337,14 @@ on a new line! ""Wow!"", the masses cried";
|
|||||||
// this will throw a FormatException on failure
|
// this will throw a FormatException on failure
|
||||||
int.Parse("123"); // returns an integer version of "123"
|
int.Parse("123"); // returns an integer version of "123"
|
||||||
|
|
||||||
// try parse will default to type default on failure
|
// TryParse will default to the type's default value on failure
|
||||||
// in this case: 0
|
// in this case 0
|
||||||
int tryInt;
|
int tryInt;
|
||||||
if (int.TryParse("123", out tryInt)) // Function is boolean
|
if (int.TryParse("123", out tryInt)) // Function is boolean
|
||||||
Console.WriteLine(tryInt); // 123
|
Console.WriteLine(tryInt); // 123
|
||||||
|
|
||||||
// Convert Integer To String
|
// Convert Integer To String
|
||||||
// Convert class has a number of methods to facilitate conversions
|
// The Convert class has a number of methods to facilitate conversions
|
||||||
|
|
||||||
// String to int
|
// String to int
|
||||||
|
|
||||||
@ -388,7 +388,7 @@ on a new line! ""Wow!"", the masses cried";
|
|||||||
Console.Read();
|
Console.Read();
|
||||||
} // End main method
|
} // End main method
|
||||||
|
|
||||||
// Available in C# 9 and later, this is basically a syntactic sugar for a class. Records are immutable*.
|
// Available in C# 9 and later, this is basically syntactic sugar for a class. Records are immutable*.
|
||||||
public record ARecord(string Csharp);
|
public record ARecord(string Csharp);
|
||||||
|
|
||||||
// CONSOLE ENTRY - A console application must have a main method as an entry point
|
// CONSOLE ENTRY - A console application must have a main method as an entry point
|
||||||
@ -428,7 +428,7 @@ on a new line! ""Wow!"", the masses cried";
|
|||||||
|
|
||||||
// GENERICS
|
// GENERICS
|
||||||
// The classes for TKey and TValue is specified by the user calling this function.
|
// The classes for TKey and TValue is specified by the user calling this function.
|
||||||
// This method emulates the SetDefault of Python
|
// This method emulates Python's dict.setdefault()
|
||||||
public static TValue SetDefault<TKey, TValue>(
|
public static TValue SetDefault<TKey, TValue>(
|
||||||
IDictionary<TKey, TValue> dictionary,
|
IDictionary<TKey, TValue> dictionary,
|
||||||
TKey key,
|
TKey key,
|
||||||
|
Loading…
Reference in New Issue
Block a user