1 / 25

Getting Started With C#

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.

zia-beck
Download Presentation

Getting Started With C#

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Getting Started With C# SigWin 2004

  2. 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

  3. 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

  4. Class basics • Classes look like this: class myClass { private string myVar; public myClass() { } protected void myMethod(int parameter) { //do stuff here } }

  5. Inheritance • C# only supports single inheritance • Specify inheritance like so: class myClass : myBaseClass

  6. Methods • 2 types of methods: • Static methods • Non-static methods • Yes, there are others, but we won’t talk about them (virtual, override, etc.)

  7. 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); } }

  8. What’s wrong with this code? class myClass : myBaseClass, myRealBaseClass { myClass() {} publicint DoSomething(string myString) { return 100000000000000; } }

  9. 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; } }

  10. 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

  11. 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(); } }

  12. 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?

  13. Namespaces • This is where I somehow manage to describe a namespace

  14. “Using” • Using tells the compiler what prefixes to try on stuff it can’t figure out: using System.Collections; ArrayList myList=new ArrayList()

  15. What’s wrong with this code? namespace myProgram { class theClass { staticvoid Main() { Console.WriteLine("blah"); } } }

  16. 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);

  17. 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) { } }

  18. Error handling • System.Exception • Gets code information like: • Stack trace • Line and file info • Detailed error message • Use in try…catch blocks

  19. Error example … ArrayList myList; try { myList.Clear(); } catch (System.NullReferenceException e) { Console.WriteLine(“variable not initialized”); }

  20. 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

  21. What’s wrong with this code? (hard version) try { bool success=false; success=SomeClass.SomeMethod(); } catch (MyCustomExcecption e) { Console.WriteLine(success); }

  22. What’s wrong with this code? (really hard version) try { SomeClass.SomeMethod(); } catch (System.Exception e) { //process error and rethrow throw e; }

  23. 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);

  24. 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 }}

  25. 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); } }

More Related