-
-
Save petrucci34/bf41c6440010def2c77243ce808f4c3d to your computer and use it in GitHub Desktop.
c#-in-a-nutshell
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Check out https://www.microsoft.com/net/tutorials/csharp/getting-started for the comprehensive tutorial. | |
using System; | |
// In C#, a namespace is a scope in which developers organize their code, | |
// and you've been using them throughout the previous lessons. | |
namespace GettingStartedWithCSharp { | |
public class Program { | |
public static void Main() { | |
// Basic for loop. | |
for (int i = 0; i < 10; i++) { | |
Console.WriteLine(i); | |
} | |
var myList = new List<string>(){ "one", "two", "three" }; | |
foreach (var item in myList) { | |
Console.WriteLine(item); | |
} | |
// Holds 3 elements, with indexes of 0, 1, and 2. | |
int[] someIntegers = new int[3]; | |
// Initializes the values of the array. | |
int[] moreIntegers = new int[] { 1, 2, 3, 4, 5 }; | |
// You can omit `int` and just specify []; type is inferred. | |
int[] otherIntegers = new [] { 1, 3, 5, 7, 9 }; | |
// Exception handling. | |
try { | |
int sum = SumNumberStrings(new List<string> {"5", "4"}); | |
Console.WriteLine(sum); | |
} catch (System.FormatException) { | |
Console.WriteLine("List of numbers contained an invalid entry."); | |
} | |
} | |
} | |
} | |
// Classes have members, which consist of methods, properties, and fields. | |
// You should rarely expose fields from your classes, and instead use properties | |
// to control external access to your object's state. | |
public class Speedometer { | |
private int _currentSpeed; | |
public int CurrentSpeed { | |
get { | |
return _currentSpeed; | |
} | |
set { | |
if (value < 0) return; | |
if (value > 120) return; | |
// Value is a keyword used in setters representing the new value. | |
_currentSpeed = value; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment