C#/.NET Jacob Lewallen. C# vs.NET.NET is a platform. Many languages compile to.NET: –VB.NET...
-
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...
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.
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
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.
Syntax
• Think Java, if that fails think C++public class ExampleApp {
public static void Main() {
string str = “Hello, World”;
Console.WriteLine(str);}
}
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.
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.
Collections
• System.Collections.Hashtable• System.Collections.ArrayList
• Type-safe enumerations:foreach (string name in users) {
…
}
• .NET will do runtime type checking.
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.
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.
Reference Types
• Also referred to as object’s.
• Store references to actual data.
• Passed by reference, by default.
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.
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.
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.
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.
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.
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”);
Using Delegates
• Delegates are used exclusively for event handling in .NET GUI’s.
• Many design patterns (publisher/subscribe)
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.
Assembly
• Think shared-object - *.dll or *.so.
ADO.NET
System.XML