.NET Competency Group
- Started by Stephen
- Relatively new group
- We are always looking for new members, .NET veterans and newcomers
Overview of C#/.NET
Brownbag
February 2016
After today you will know more about the language that powers the following platforms:
and these...
Core .NET Languages
-
C# (v6.0 - 2015)
- General purpose OOP language
- C/C++-like syntax
-
Visual Basic .NET (v14.0 - 2015)
- General purpose, successor to VB
-
F# (v3.1.1 - 2014)
- Functional language, originated from ML
-
All compile down to CIL
- (Common Intermediate Language)
History of C#/.NET
- Versions
- Versions are almost always backwards compatible
- 3 Main .NET breakpoints (CLR versions)
- .NET Framework 1.0/1.1
- .NET Framework 3.5
- .NET Framework 4
- Paradigms have shifted over the versions of C#
C# 2.0
- Released in November 2005
-
Large paradigm changes from v1.x, including:
- Generics
- Iterators
- Delegates
- Static Classes
C# 3.0
- Released in November 2007
-
Key improvements, including:
- Auto Implemented Properties
- Extension Methods
- Lambdas
- Expression Trees
C# 4.0 and C# 5.0
- Released in November 2010 and August 2012
-
C# 4.0 introduced dynamic programming constructs:
- Dynamic binding (Dynamic Language Runtime)
- Named and optional arguments
- C# 5.0 introduced mainly:
- Async/Await
C# 6.0
- Released in July 2015
-
C# 6.0 introduced many improvements:
- Compiler as a service (Roslyn)
- Exception filters
- Await improvements
- Null safe propagator (objectInstance?.propertyA)
- String Interpolation
- Default values and initialization paradigms
Mono (Xamarin)
- Cross platform implementation of .NET
- Popular in mobile and game (Unity3D) dev
- Runs on Windows, Linux, Mac, etc
-
Basically supports everything except
- WPF
- WWF
- Limitations with WCF and ASP.NET
- http://www.mono-project.com/docs/about-mono/compatibility/
- http://www.mono-project.com/docs/about-mono/supported-platforms/
Getting Started
- For Windows, Visual Studio Community 2015
- For Mac, Xamarin Studio (MonoDevelop)
- For all platforms, Visual Studio Code
-
Other useful tools
- LINQPad, C# Scratchpad
- ILSpy, .NET Assembly Browser/Disassembler
.NET Package Manager
- NuGet
- An aside, there is a Windows equivalent to Mac's Homebrew called Chocolatey
Time for basics
C# Basic Types
Value Types
- Numerics (Structs)
- Integer (Int, Long, etc)
- Floats (Float, Double)
- Decimal
- Boolean
- Character (char)
- Enumerations
- Other Structs
- DateTime
Reference Types
- Object
- Base of Ref Types
- Interfaces
- Delegates
- String
- Equality/assignment like a value type
Classes/Objects
Overview
- Familiar, object-oriented programming language class structure
- Inherits from object type
- Instantiate instances of classes using constructors with the syntax of new() or new(param1, ...)
- Utilizes classical inheritance
- Does not support multiple inheritance
- More at: https://msdn.microsoft.com/en-us/library/x9afc042.aspx
Classes/Objects
Class vs Struct
- Classes are:
- Reference types
- Used for modeling complex behavior
- Used throughout the framework and in the wild
- Structures are:
- Value types
- Used for modeling simple data structures that typically do not change after assignment
- Used for the numerics and DateTime in the framework
Classes/Objects
Variables
- Store values or logic (lambdas)
- Strongly-typed, but local variable usage of var keyword in a method is allowed through compiler replacement
- Class variables types
- Static variables
- Instance variables
Parameters
- Value parameters
- Reference parameters - use with the ref keyword
- Output parameters - use with the out keyword
Classes/Objects
Access Modifiers
- Public
- Protected - containing class or in derived types
- Internal - in current assembly
- Private - containing class only
Some Other Modifiers
- Const - constant (compile-time)
- Readonly - set once per instance (run-time)
- Sealed - can not be inherited from
- Static - member belongs to type itself
- Abstract - class/members to be defined by derived class
- Virtual - allows implementation to be overridden
Classes/Objects
Interfaces
- Contain definitions for a group of related functionalities that a class or struct can implement
- Contract types that can be used in place of concrete types to allow for flexibility and reusable code
- A class can implement multiple interfaces for composition
Classes/Objects
Constructors
- Mainly used types
- Instance Constructor
- Private Constructor (commonly for singletons)
- When objects are initialized, the following execute:
- Base Constructors
- Derived Constructors
- Property Initialization
Classes/Objects
Garbage Collection
- .NET handles garbage collection automatically
- Exceptions include:
- File and I/O interactions
- Database connections
- Graphics
- Network sockets
- These require handling disposal via the IDisposable interface's .Dispose() method
- Syntactic sugar with using clauses
- More at: https://msdn.microsoft.com/en-us/library/0xy59wtx(v=vs.110).aspx
Generics
Generics
Overview
- Allows generalization of functionality
- Compiler generates code for each of the different type usages of the generalized functionality
- Using interfaces can allow run-time binding to a specific type
- Most commonly seen with collections in .NET
Generics
Generic Collections
- System.Collections.Generic
- List<T>
- Dictionary<T1, T2>
- HashSet<T>
- Tuple<T1, T2, ...>
- etc...
Generics
Custom Generics
- Specify generic type(s) and constraints (if applicable) in method signature
- Use generic types in place of concrete types or interfaces
Methods
Overview
- Types of Methods
- Static methods exist on type itself
- Can not make use of instance members (fields or methods)
- Instance methods exist on the instance of the class
- Can make use of static methods and fields
-
Anonymous functions/lambdas
- Can be defined with delegates & Func/Action types
- Static methods exist on type itself
- "Getters"/"Setters" are implemented with C# Properties
- Extension methods can add functionality to sealed classes
Methods
Method Signature
- Access Modifier
- Inheritance-related Modifier (optional)
- Asynchronous Modifier (optional)
- Return Type or "void"
- Generic Types <T1, T2, etc...> (optional)
- Input Parameters
- Generic Constraints (optional)
Methods
Lambdas
- Anonymous functions that can be used to create delegates or expression tree types
- Heavily used in LINQ due to code conciseness
- Can be implemented within a closure
Methods
Extension Methods
- Special static methods that appear to be on the class
- Extension methods are defined on public static classes
- Signature of an extension method is the following:
-
public static int WordCount(this String str)
-
- At compiler converts to a call to the static method
Conditionals and Loops
Conditionals Overview
- Pretty standard and similar to Java
- Conditionals: If/Switch
- Switch
- Doesn't fall through
- Can have multiple cases share same branch
- Ternary operator
- Newer versions of .NET have:
- Null coalesce (e.g. possiblyNullValue ?? fallbackValue)
- Null safe navigation (e.g. possiblyNullValue?.propA)
Conditionals and Loops
Looping Overview
- Pretty standard and similar to Java
- Loops: For/Do-while/While
- For IEnumerables, can use foreach
- break keyword breaks out of a loop
- continue keyword jumps to the next iteration
Reflection
Overview
- Allows the C# runtime to dynamically execute code through self-inspection of assemblies, modules, & types
- Reflection drives much of the behavior within of C#/.NET
- Used heavily by IoC container implementations
- Direct usage of reflection is best avoided when possible
- Large performance hit
- Lose benefit of strong typing
- Can cause vague exceptions and code execution paths
Attributes
Overview
- Used to attach declarative information to any type
- Comparable to Java Annotations
- Uses reflection under the covers
- Used heavily in:
- WCF (.NET SOAP/RPC solution, serialization hints)
- ASP.NET (.NET web platform - HTTP verb/method map)
- .NET Aspect-Oriented Programming solutions
- Are inherited in child classes
LINQ
Overview
- Language-Integrated Query
- Heavily used by Entity Framework and other .NET ORMs
- As the name implies it's a querying language for collections that derive from IEnumerable and IQueryable
- IEnumerable generally are in-memory collections
- IQueryable represent external data collections
- Can use for sorting, filtering, map/reduce actions
- Functional like programming
- Generally deferred executions with both collection types
- Exceptions: .ToList(), .First(), .Single(), .Take(), etc.
- These functions force evaluation
LINQ
Overview (con't)
- SQL-like syntax, but usually ends with the SELECT logic
- Two forms
- Query Comprehension
-
from num in numbers where num % 2 == 0 orderby num select num;
-
- Fluent Interface
- collectionToQuery.Where(...).Select(...)
- Query Comprehension
Learning Resources
- Microsoft Developer Network (MSDN) - C# Reference
- MSDN - 101 LINQ Samples in C#
- Dot Net Perls
- Pluralsight
- 6 months free with Visual Studio Dev Essentials
- http://www.pluralsight.com/training
Libraries
Popular .NET Libraries by Microsoft
- ASP.NET
- MVC - http://www.asp.net/mvc
- Web API - http://www.asp.net/web-api
- vNext (preview) - http://www.asp.net/vnext
- Entity Framework [ORM]
- WPF (Desktop Windows UI framework)
- Universal Apps
Popular 3rd-Party .NET Libraries
- Json.NET [JSON De/Serializer]
- NUnit [Unit Testing]
- AutoMapper [Object-Object mapper]
- AutoFac [IoC Container]
- PostSharp [AOP]
- NLog [Logger]
Conclusion
- .NET and C# are awesome
- Heavily used in mobile/cross-platform development
- Relatively quick to pick up for a Java developer
- New language features are always being added
- Compiler and core libraries are open source
Thanks!
Overview of C#/.NET Brownbag
By Andy Kong
Overview of C#/.NET Brownbag
- 1,192