1 / 34

From Java to C#

.NET from Java developer's perspective. From Java to C#. Purpose of these slides. Direct the flow of the presentation AND Serve as a place for you to check details on .NET stuff "I remember short syntax for if-elseif-else but can't remember details..." These two are equally important.

druce
Download Presentation

From Java to 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. .NET from Java developer's perspective From Java to C#

  2. Purpose of these slides • Direct the flow of the presentation AND • Serve as a place for you to check details on .NET stuff • "I remember short syntax for if-elseif-else but can't remember details..." These two are equally important

  3. Summary • History • .NET basics • Comparison • Syntax • IDE: Visual Studio + demo • Highlights + demo • Q+A 

  4. History 5000 BC - C 1990 Visual Basic, DLL hell 1995 Java -> automates memory management 2000 .NET -> MS steals Java's idea and does it better. MS focuses almost ALL of its software developing to .NET platform 2010 .NET still under active developing .NET versions 2.0    Propertys 3.0    LINQ, Lambda 3.5    WCF 4.0 Parallel computing, PLINQ, F#

  5. Basics - .NET • Modern object-oriented programming framework • Though supports other programming paradigms aswell •  Automatic memory management • Though supports pointers • Developed actively • basis of all software development with MS tools • Supports multiple programming languages • Huuge support for almost everything, eg: • Desktop applications (Windows Forms, WPF) • Internet applications (ASP.NET, ASP.NET MVC) • Server applications (windows services, WCF, ADO.NET) • etc..

  6. Comparison Java • Object-oriented • Java VM • Open source • Multi-OS • bytecode • functions • packages • .jar • package .NET • Object-oriented • CLR • Proprietary • Windows, multi-OS unofficial • IL • methods • namespaces • exe • assembly

  7. Comparison, features Java • Automatic memory management • Generics • Annotations • Field .NET • Automatic memory management • Generics • Attributes • Propertys • Delegates (function pointers) • Extension methods • Overloading operators • LINQ • Lambda functions • ...

  8. Syntax C-like syntax using System; namespace Hello_World_CSharp{    classClass1    {        staticvoidMain(string[] args)       { Console.WriteLine("Hello, world! <3");             Console.Readline();         }     } }

  9. Syntax, part II Resembles closely Java but is not Java delegateint del(int i); staticvoid Main(string[] args) { del myDelegate = x => x * x; int j = myDelegate(5); //j = 25 }

  10. IDE: Visual Studio • demo

  11. Highlights • Cheat sheet • Namespaces • Variables • Instantiating • Operators • Propertys (NOT in Java) • Loops • Delegates - function pointers (NOT in Java) • Generics • Attributes (Annotations) • Instantiating objects • Logical operators • Configuration • Resources / culture • Extension methods (NOT in Java) • Overloading operators (NOT in Java) • Reflection

  12. 1. Cheat sheet Ctrl + alt           intellisense F5                    compile Shift+alt+enter Fullscreen  Ctrl+F5            Debug Ctrl+K,Ctrl+C  comment code Ctrl+K,Ctrl+U  uncomment code Ctrl+K,Ctrl+D  format code Method/class/field/property commenting: /// in VS2010 gives javadoc-like documentation  (These shortcuts are for Web developer installation of VS2010)

  13. 2. Namespaces Where to find methods? Hint: write something you know have to exist (eg. ArrayList) and then right-click object type and choose resolve.  System.Linq System.IO System.Resources System.Collection Sometimes you have to add assembly reference to use methods from one namespace + DEMO

  14. 3. Variables Common Type System (CTS) Two types of variables:  value type, eg. int a = 5; reference type, eg: String s = "hi"; Object is a base class for everything Every class implements ToString() method Nullable: int x = null; // syntax error int? x = null; // compiles

  15. 4. Instantiatingobjects, fancy syntax static void Main(string[] args)         {             new a() { a = "5", b = "5" };         }         public class a         {             public string a { get; set; }             public string b { get; set; }         }

  16. 5. Operators Java: string1.equals(string2); C#: string1 == string2

  17. 6. Propertys • Way to access class's variables • Very neat! • No more getters / setters! • Can include logic Java: car.setModel("ford"); string carmodel = car.getModel(); C#: car.Model = "ford"; string carmodel = car.Model;

  18. 6. Propertys Java: class a {     int value;     public void setValue(int v) {         value = v;     }     public int getValue() {        return value;     } } C#:  class a   {        int _a = 0;         public int A      {     get { return _a / 5; }    set { if (_a > 0) _a = value; }     }         }

  19. 6. Propertys, different syntax class car {     public string Owner { get; set; }     public string Model { get; private set; } }

  20. 7. Loops    for (int i = 0; i < 5; i++)    {         // do sthg     }    List<string> strings = new List<string>();    foreach (string s in strings)    {           // do sth;     } => in reality, you will NOT use loops. Instead, you will use LINQ & Lambda expressions in MOST cases

  21. 8. Delegates Function pointers     public partial class Form1 : Form     {         public Form1()         {             InitializeComponent();             button.Click += new EventHandler(button1_Click);         }         private void button1_Click(object sender, EventArgs e)         {            // OH YEAH         }     }

  22. 9. Generics • Very similar to Java             System.Collections.Generic.List<string> strings = new List<string>();             strings.Add("moi");             int count = strings.Count // 5

  23. 10. Attributes • = Java Annotations • You will face to attributes eg. when programming service-layer logic [DataContract()] class Person : IExtensibleDataObject { privatestring LastNameValue; // Apply the DataMemberAttribute to fields (or properties) // that must be serialized. [DataMember()] publicstring FirstName; [DataMember] publicstring LastName { get; set; } }

  24. 11. Instantiating objects, fancy syntax static void Main(string[] args)         {             new a() { a = "5", b = "5" };         }         public class a         {             public string a { get; set; }             public string b { get; set; }         }

  25. 12. Logical operators int? x = null;             string y; // method 1  if (x == null)             {                 y = "missing value";             }             else             {                 y = x.ToString();             } // method 2 y = (x == null) ? "missing value" : x.ToString(); // method 3 y = x.ToString() ?? "missing value";

  26. 13. Configuration / app.config/web.config  way to change application configuration on run time, without compiling <?xmlversion="1.0"encoding="utf-8" ?> <configuration>   <appSettings>     <addkey="Setting1"value="Very" />     <addkey="Setting2"value="Easy" />   </appSettings> </configuration>

  27. 13. Configuration / code namespace AppSettings {     class Program     {         static void ShowConfig()         {             foreach (string key in ConfigurationManager.AppSettings)             {                 string value = ConfigurationManager.AppSettings[key];                 Console.WriteLine("Key: {0}, Value: {1}", key, value);             }         }

  28. 14. Resources & localization .NET programs use culture of machine to define localization Resources stored in Resource files. Access: (two resource files, fi-fi.resx and en-us.resx)             string value, desiredLanguage = "fi";             value = (desiredLanguage == "fi-fi") ? Resources-fi.String1 : Resources-en.String1;             Console.WriteLine(value);             Console.ReadLine();

  29. 15. Extension methods • Lets you "add" method to existing types without creating new derived type • Can be very useful sometimes • Eg. DateTime.Now.AddBusinessDays() method

  30. 15. Extension methods, code namespace ExtensionMethods { publicstaticclass MyExtensions { publicstaticint WordCount(this String str) { return str.Split(newchar[] { ' ', '.', '?' }, StringSplitOptions.RemoveEmptyEntries).Length; } } } using ExtensionMethods;string s = "Hello Extension Methods"; int i = s.WordCount();

  31. 16. Overloading operators • You can overload basic operators (+, -, ++, etc) • Can be useful sometimes • Moslty: equity (== operator) class HumanBeing {  ... // magic happens here } // compares weight of humans if (HumanBeing1 > HumanBeing2) {  // do sthg }

  32. 17. Reflection using System; using System.Collections; namespace ConsoleApplication {     class Program     {         static void Main(string[] args)         {             System.Collections.ArrayList UnknownStuff = new ArrayList();             UnknownStuff.Add("aa"); // string             UnknownStuff.Add(4); // int             foreach (object o in UnknownStuff) {                 if (o.GetType() == typeof(String))                 {                     Console.WriteLine("found string!");                 }             }         }     } }

  33. More?     • MSDN – API documentation • TechNet – more high-level documentation • Blogs – good tips’n’tricks • Problems? -> Google • .NET is popular

  34. Any questions? • In case of sudden questions, please refer to our .NET experts • Juha • Samuli • Majid

More Related