C#/.NET Jacob Lewallen. C# vs.NET.NET is a platform. Many languages compile to.NET: –VB.NET...

24
C#/.NET Jacob Lewallen
  • date post

    21-Dec-2015
  • Category

    Documents

  • view

    218
  • download

    3

Transcript of C#/.NET Jacob Lewallen. C# vs.NET.NET is a platform. Many languages compile to.NET: –VB.NET...

Page 1: C#/.NET Jacob Lewallen. C# vs.NET.NET is a platform. Many languages compile to.NET: –VB.NET –Python.NET –Managed C++ –C#

C#/.NET

Jacob Lewallen

Page 2: C#/.NET Jacob Lewallen. C# vs.NET.NET is a platform. Many languages compile to.NET: –VB.NET –Python.NET –Managed C++ –C#

C# vs .NET

• .NET is a platform.

• Many languages compile to .NET:– VB.NET– Python.NET– Managed C++– C#

Page 3: C#/.NET Jacob Lewallen. C# vs.NET.NET is a platform. Many languages compile to.NET: –VB.NET –Python.NET –Managed C++ –C#

.NET

• Intermediate Language (IL) – “half-way”

• Interpreted locally using JIT (Just-In-Time) compilers.

• Has a very nice standard library, comparable to Java’s.

• Theme: More “open” to it’s hosted system than Java.

Page 4: C#/.NET Jacob Lewallen. C# vs.NET.NET is a platform. Many languages compile to.NET: –VB.NET –Python.NET –Managed C++ –C#

C# Basics

• Garbage Collected

• Very Java-esque:– Main is static method in some class

• Designed for .NET, rather than adapted.

• Simple operator overloading

• Everything is an Object

• Java-like inheritance

Page 5: C#/.NET Jacob Lewallen. C# vs.NET.NET is a platform. Many languages compile to.NET: –VB.NET –Python.NET –Managed C++ –C#

Safe vs Managed

• Unmanaged – native code - non-IL code.

• Unsafe code – code wrapped in C#’s unsafe mechanism:– Relaxed type checking– Pointers– Flagged as unsafe and requires more trust.

• Unsafe code is still managed code.

Page 6: C#/.NET Jacob Lewallen. C# vs.NET.NET is a platform. Many languages compile to.NET: –VB.NET –Python.NET –Managed C++ –C#

Syntax

• Think Java, if that fails think C++public class ExampleApp {

public static void Main() {

string str = “Hello, World”;

Console.WriteLine(str);}

}

Page 7: C#/.NET Jacob Lewallen. C# vs.NET.NET is a platform. Many languages compile to.NET: –VB.NET –Python.NET –Managed C++ –C#

Namespaces

• More like C++ namespaces, with some Java packages sprinkled in.

using System.Collections;

• Instead of using… System.Collections.Hashtable

• Assemblies (DLLs) are named for the namespaces they contain, usually.

Page 8: C#/.NET Jacob Lewallen. C# vs.NET.NET is a platform. Many languages compile to.NET: –VB.NET –Python.NET –Managed C++ –C#

Declaring a Namespace

• You can wrap your code in the namespace like so:

Namespace UCR.Technical.Seminar {

}

• Nearly all code I’ve ever seen has been wrapped in a Namespace.

Page 9: C#/.NET Jacob Lewallen. C# vs.NET.NET is a platform. Many languages compile to.NET: –VB.NET –Python.NET –Managed C++ –C#

Collections

• System.Collections.Hashtable• System.Collections.ArrayList

• Type-safe enumerations:foreach (string name in users) {

}

• .NET will do runtime type checking.

Page 10: C#/.NET Jacob Lewallen. C# vs.NET.NET is a platform. Many languages compile to.NET: –VB.NET –Python.NET –Managed C++ –C#

Memory

• All allocated memory is garbage collected.

• We use new to create object instances.

• We can override a finalizer for our classes to handle cleanup.

Page 11: C#/.NET Jacob Lewallen. C# vs.NET.NET is a platform. Many languages compile to.NET: –VB.NET –Python.NET –Managed C++ –C#

Value Types

• Categories: Struct, Enumeration, Numeric Types (integers, floats, bools)

• Assignments create copies of the assigned value.

• Value types cannot contain null.• int is an alias for System.Int32, all value

types have an alias.

Page 12: C#/.NET Jacob Lewallen. C# vs.NET.NET is a platform. Many languages compile to.NET: –VB.NET –Python.NET –Managed C++ –C#

Reference Types

• Also referred to as object’s.

• Store references to actual data.

• Passed by reference, by default.

Page 13: C#/.NET Jacob Lewallen. C# vs.NET.NET is a platform. Many languages compile to.NET: –VB.NET –Python.NET –Managed C++ –C#

Boxing

• Boxing - conversion from a value type to type object. It’s implicit:

int x = 23;

object o = 23; object r = x;

• x is an integer on the heap, value is 23.• o is an object, referencing a new value on

the heap, that’s 23.• r is an object, referencing the value x.

Page 14: C#/.NET Jacob Lewallen. C# vs.NET.NET is a platform. Many languages compile to.NET: –VB.NET –Python.NET –Managed C++ –C#

Unboxing

• Explicit conversion from a reference type, an object, to a value type.

object o = 23;

int x = (int)o;

• Type checking is done in the conversion.

• Can throw InvalidCastException’s.

Page 15: C#/.NET Jacob Lewallen. C# vs.NET.NET is a platform. Many languages compile to.NET: –VB.NET –Python.NET –Managed C++ –C#

Properties

• Replaces getter/setter paradigm.

• Wraps private member variables around more defined accessors.

• object.getName() you do object.Name.• object.setName(“Jacob”) becomes object.Name = “Jacob”;

• Standard library uses upper case names for all properties.

Page 16: C#/.NET Jacob Lewallen. C# vs.NET.NET is a platform. Many languages compile to.NET: –VB.NET –Python.NET –Managed C++ –C#

Properties

• Syntax for declaring a Property:String Name {

get { return (m_name); }

set { m_name = value; }

}

• Where m_name is our member variable.

• Read-only Properties have no set.

Page 17: C#/.NET Jacob Lewallen. C# vs.NET.NET is a platform. Many languages compile to.NET: –VB.NET –Python.NET –Managed C++ –C#

Events/Delegates

• Calling/invoking methods w/o knowing anything about the method’s object.

• Defining a Delegate:

public delegate void ButtonDelegate(string name);

• This delegate takes a string and returns nothing.

Page 18: C#/.NET Jacob Lewallen. C# vs.NET.NET is a platform. Many languages compile to.NET: –VB.NET –Python.NET –Managed C++ –C#

Defining a Delegate

• Declare a variable for out delegate:private ButtonDelegate m_presses;

• We can create an instance of her:m_presses = new ButtonDelegate(SayHello);

public bool SayHello(string name) { … }

• Now, we can invoke/trigger the delegate:m_presses(“Hello, World”);

Page 19: C#/.NET Jacob Lewallen. C# vs.NET.NET is a platform. Many languages compile to.NET: –VB.NET –Python.NET –Managed C++ –C#

Using Delegates

• Delegates are used exclusively for event handling in .NET GUI’s.

• Many design patterns (publisher/subscribe)

Page 20: C#/.NET Jacob Lewallen. C# vs.NET.NET is a platform. Many languages compile to.NET: –VB.NET –Python.NET –Managed C++ –C#

Microsoft Whining

• You can download the .NET SDK from Microsoft. You’ll get:– All the necessary command line utilities for

developing with C#.– A few graphical tools for inspecting IL.– Help via http://msdn.microsoft.com/

• Visual Studio is NOT required.

Page 21: C#/.NET Jacob Lewallen. C# vs.NET.NET is a platform. Many languages compile to.NET: –VB.NET –Python.NET –Managed C++ –C#

Mono

• Open Source .NET/C# implementation

• http://www.go-mono.com/

Page 22: C#/.NET Jacob Lewallen. C# vs.NET.NET is a platform. Many languages compile to.NET: –VB.NET –Python.NET –Managed C++ –C#

Assembly

• Think shared-object - *.dll or *.so.

Page 23: C#/.NET Jacob Lewallen. C# vs.NET.NET is a platform. Many languages compile to.NET: –VB.NET –Python.NET –Managed C++ –C#

ADO.NET

Page 24: C#/.NET Jacob Lewallen. C# vs.NET.NET is a platform. Many languages compile to.NET: –VB.NET –Python.NET –Managed C++ –C#

System.XML