250 likes | 339 Views
Getting Started With C#. SigWin 2004. Outline. Class basics Inheritance Methods Properties Types Important C# concepts Garbage collection Namespaces Using statement Event Handling Delegates Event processing More advanced stuff Error handling Best practices. Basic C# features.
E N D
Getting Started With C# SigWin 2004
Outline • Class basics • Inheritance • Methods • Properties • Types • Important C# concepts • Garbage collection • Namespaces • Using statement • Event Handling • Delegates • Event processing • More advanced stuff • Error handling • Best practices
Basic C# features • Automatic garbage collection (don’t have to delete objects) • No pointers • Only booleans allowed in if statements • Everything inherits from System.Object
Class basics • Classes look like this: class myClass { private string myVar; public myClass() { } protected void myMethod(int parameter) { //do stuff here } }
Inheritance • C# only supports single inheritance • Specify inheritance like so: class myClass : myBaseClass
Methods • 2 types of methods: • Static methods • Non-static methods • Yes, there are others, but we won’t talk about them (virtual, override, etc.)
Method example class myClass { private string myVar; public myClass() { } public void myMethod(int parameter) { //do stuff here } public static void PrintStuff() { } } class myCallerClass { public myCallerClass() { myClass.PrintStuff(); myClass myInstance= new myClass(); myInstance.myMethod(5); } }
What’s wrong with this code? class myClass : myBaseClass, myRealBaseClass { myClass() {} publicint DoSomething(string myString) { return 100000000000000; } }
Properties • Properties are NOT fields • Fields are variables declared in the class • Properties are “wrappers” for fields • Get/Set: private string mystring; public string MyString { get { return mystring; } set {mystring = value; } }
Types • Value Types vs. Reference Types • Value Types are: • int, byte, double, etc. • Reference types are: • Everything else! (including string) • Reference types=pointers • Can pass value types by reference by using the “ref” keyword
What happens here? class myUtil { public myUtil() { int thenumber=0; System.Collections.ArrayList myList=new System.Collections.ArrayList(); myList.Add(1); myList.Add(2); DoStuff(thenumber, myList); System.Console.WriteLine(thenumber+"and"+myList.Count); } privatevoid DoStuff(int mynumber, System.Collections.ArrayList myListThingy) { mynumber=5; myListThingy.Clear(); } }
Garbage collection • The .NET framework takes care of getting rid of your objects • It runs automatically • You can run it manually by calling GC.Collect() • BUT: • NEVER DO THIS!!! • Why?
Namespaces • This is where I somehow manage to describe a namespace
“Using” • Using tells the compiler what prefixes to try on stuff it can’t figure out: using System.Collections; ArrayList myList=new ArrayList()
What’s wrong with this code? namespace myProgram { class theClass { staticvoid Main() { Console.WriteLine("blah"); } } }
Event handling • Events are how programs communicate • Delegates are function pointers • To capture an event, you say something like this: myButton.Click+=new EventHandler(myButton_click_eventhandler);
What’s wrong with this code? using System; class myClass { private System.Windows.Forms.Button myButton; public myClass() { myButton.Click+=new EventHandler(myButtonEvent); } private void myButtonEvent(object sender, System.EventArgs e) { } }
Error handling • System.Exception • Gets code information like: • Stack trace • Line and file info • Detailed error message • Use in try…catch blocks
Error example … ArrayList myList; try { myList.Clear(); } catch (System.NullReferenceException e) { Console.WriteLine(“variable not initialized”); }
More error stuff • To throw an error say: throw <exception object> Or: throw new <exception class> Exceptions are very costly, don’t throw them unless there is an error
What’s wrong with this code? (hard version) try { bool success=false; success=SomeClass.SomeMethod(); } catch (MyCustomExcecption e) { Console.WriteLine(success); }
What’s wrong with this code? (really hard version) try { SomeClass.SomeMethod(); } catch (System.Exception e) { //process error and rethrow throw e; }
What’s wrong with this code? //Allocate a lot of objects int myvar; for (int i=0; i<10000; i++) { ArrayList myList=new ArrayList(); } GC.Collect(); Console.WriteLine(myvar);
What’s wrong here? class myClass { public myClass() {} private string userSetsThis; public string UserSetsThis { get {return userSetsThis; } } public myMethod() { //Do stuff based on value of userSetsThis }}
What’s the output here? (this is really damn hard) struct myStruct { int value; public myStruct(int value) { this.value = value; } public void SetValue(int value) { this.value=value; } public int Value { get {return value;} } } class myClass { public myClass() { myStruct[] myFoos=new myStruct[2]; myFoos[0]=new myStruct(5); myFoos[1]=new myStruct(10); foreach (myStruct foo in myFoos) { foo.SetValue(99); Console.WriteLine(foo.Value); } Console.WriteLine(); foreach (myStruct foo in myFoos) { Console.WriteLine(foo.Value); } }