C Sharp Cheat Sheet

Posted on  by 



This cheatsheet glances over some of the common syntax of F# 3.0. If you have any comments, corrections, or suggested additions, please open an issue or send a pull request to https://github.com/dungpa/fsharp-cheatsheet.

Int c23; //c is an array of 2 arrays of three ints. A10 follows a02 Array variables (e.g. A,b,c above) cannot be made to point to other arrays Strings are represented as character arrays terminated by ASCII zero. Pointers are indicated by left associative asterisk (.) in the type declarations: int a.a; // a is a pointer to an integer.

C# — Style Guide Cheat Sheet. Nikita Starichenko. C♯ Minor Cheat Sheet. All Cheat Sheets. (F#, C#, G#, and D#). It is the 9th most popular key among Minor keys and the 17th most popular among all keys. Minor keys, along with major keys, are a common choice for popular music. The three most important chords, built off the 1st, 4th and 5th scale degrees are all minor chords (C♯ minor, F. The markdown file of this sheet is hosted on GitHub. Contributions, bug fixes, additions, and improvements will be much appreciated. Prepared by ConstructG.com. ConstructG is an online game development academy. C# Introduction What is C#? C# is pronounced “C-Sharp”.

Comments

Block comments are placed between (* and *). Line comments start from // and continue until the end of the line.

XML doc comments come after /// allowing us to use XML tags to generate documentation.

Strings

F# string type is an alias for System.String type.

Use verbatim strings preceded by @ symbol to avoid escaping control characters (except escaping ' by ').

We don't even have to escape ' with triple-quoted strings.

Backslash strings indent string contents by stripping leading spaces.

Basic Types and Literals

Most numeric types have associated suffixes, e.g., uy for unsigned 8-bit integers and L for signed 64-bit integer.

Other common examples are F or f for 32-bit floating-point numbers, M or m for decimals, and I for big integers.

See Literals (MSDN) for complete reference.

Functions

The let keyword also defines named functions.

Pipe and composition operators

Pipe operator |> is used to chain functions and arguments together. Double-backtick identifiers are handy to improve readability especially in unit testing:

This operator is essential in assisting the F# type checker by providing type information before use:

Composition operator >> is used to compose functions:

Recursive functions

The rec keyword is used together with the let keyword to define a recursive function:

Mutually recursive functions (those functions which call each other) are indicated by and keyword: Active development mobile phones & portable devices driver download.

Pattern Matching

Pattern matching is often facilitated through match keyword.

In order to match sophisticated inputs, one can use when to create filters or guards on patterns:

Pattern matching can be done directly on arguments:

Cheat

or implicitly via function keyword:

For more complete reference visit Pattern Matching (MSDN).

Collections

Lists

A list is an immutable collection of elements of the same type.

Arrays

Arrays are fixed-size, zero-based, mutable collections of consecutive data elements.

Sequences

A sequence is a logical series of elements of the same type. Individual sequence elements are computed only as required, so a sequence can provide better performance than a list in situations in which not all the elements are used.

Higher-order functions on collections

The same list [ 1; 3; 5; 7; 9 ] or array [| 1; 3; 5; 7; 9 |] can be generated in various ways.

  • Using range operator .

  • Using list or array comprehensions

  • Using init function

Lists and arrays have comprehensive sets of higher-order functions for manipulation.

  • fold starts from the left of the list (or array) and foldBack goes in the opposite direction

  • reduce doesn't require an initial accumulator

  • map transforms every element of the list (or array)

  • iterate through a list and produce side effects

All these operations are also available for sequences. The added benefits of sequences are laziness and uniform treatment of all collections implementing IEnumerable<'T>.

Tuples and Records

A tuple is a grouping of unnamed but ordered values, possibly of different types:

The first and second elements of a tuple can be obtained using fst, snd, or pattern matching:

Records represent simple aggregates of named values, optionally with members:

Records can be augmented with properties and methods:

Records are essentially sealed classes with extra topping: default immutability, structural equality, and pattern matching support.

Discriminated Unions

Discriminated unions (DU) provide support for values that can be one of a number of named cases, each possibly with different values and types.

F# Core has a few built-in discriminated unions for error handling, e.g., Option and Choice.

Single-case discriminated unions are often used to create type-safe abstractions with pattern matching support:

Exceptions

The failwith function throws an exception of type Exception.

Exception handling is done via try/with expressions.

The try/finally expression enables you to execute clean-up code even if a block of code throws an exception. Here's an example which also defines custom exceptions.

Classes and Inheritance

This example is a basic class with (1) local let bindings, (2) properties, (3) methods, and (4) static members.

Call a base class from a derived one.

Upcasting is denoted by :> operator.

Dynamic downcasting (:?>) might throw an InvalidCastException if the cast doesn't succeed at runtime.

Interfaces and Object Expressions

Declare IVector interface and implement it in Vector'.

Another way of implementing interfaces is to use object expressions.

Active Patterns

Complete active patterns:

Parameterized active patterns:

Partial active patterns share the syntax of parameterized patterns but their active recognizers accept only one argument.

Compiler Directives

Load another F# source file into FSI.

Reference a .NET assembly (/ symbol is recommended for Mono compatibility).

Include a directory in assembly search paths.

Other important directives are conditional execution in FSI (INTERACTIVE) and querying current directory (__SOURCE_DIRECTORY__).

val result : bool
Full name: fsharpcheatsheet.result

The `let` keyword defines an (immutable) value
val hello : string
Full name: fsharpcheatsheet.hello

Create a string using string concatenation
val verbatimXml : string
Full name: fsharpcheatsheet.verbatimXml
val tripleXml : string
Full name: fsharpcheatsheet.tripleXml
val b : byte
Full name: fsharpcheatsheet.b
val l : int64
Full name: fsharpcheatsheet.l
val f : float
Full name: fsharpcheatsheet.f
val bi : System.Numerics.BigInteger
Full name: fsharpcheatsheet.bi
val negate : x:int -> int
Full name: fsharpcheatsheet.negate
val square : x:int -> int
Full name: fsharpcheatsheet.square
val print : x:int -> unit
Full name: fsharpcheatsheet.print
val printfn : format:Printf.TextWriterFormat<'T> -> 'T
Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.printfn
val squareNegateThenPrint : x:int -> unit
Full name: fsharpcheatsheet.squareNegateThenPrint
val ( square, negate, then print ) : x:int -> unit
Full name: fsharpcheatsheet.( square, negate, then print )
val sumOfLengths : xs:string [] -> int
Full name: fsharpcheatsheet.sumOfLengths
Multiple items
val string : value:'T -> string
Full name: Microsoft.FSharp.Core.Operators.string
--------------------
type string = System.String
Full name: Microsoft.FSharp.Core.string
val map : mapping:('T -> 'U) -> array:'T [] -> 'U []
Full name: Microsoft.FSharp.Collections.Array.map
property System.String.Length: int
val sum : array:'T [] -> 'T (requires member ( + ) and member get_Zero)
Full name: Microsoft.FSharp.Collections.Array.sum
val squareNegateThenPrint' : (int -> unit)
Full name: fsharpcheatsheet.squareNegateThenPrint'
val fact : x:int -> int
Full name: fsharpcheatsheet.fact
val even : x:int -> bool
Full name: fsharpcheatsheet.even
val odd : x:int -> bool
Full name: fsharpcheatsheet.odd
val fib : n:int -> int
Full name: fsharpcheatsheet.fib
val sign : x:int -> int
Full name: fsharpcheatsheet.sign
val fst' : x:'a * 'b -> 'a
Full name: fsharpcheatsheet.fst'
val fib' : _arg1:int -> int
Full name: fsharpcheatsheet.fib'

Similar to `fib`; using `function` for pattern matching
val list1 : string list
Full name: fsharpcheatsheet.list1
val list2 : string list
Full name: fsharpcheatsheet.list2
val list3 : string list
Full name: fsharpcheatsheet.list3
val sum : list:int list -> int
Full name: fsharpcheatsheet.sum
Multiple items
val list : int list
--------------------
type 'T list = List<'T>
Full name: Microsoft.FSharp.Collections.list<_>
val array1 : string []
Full name: fsharpcheatsheet.array1
val first : string
Full name: fsharpcheatsheet.first
val seq1 : seq<int>
Full name: fsharpcheatsheet.seq1
Multiple items
val seq : sequence:seq<'T> -> seq<'T>
Full name: Microsoft.FSharp.Core.Operators.seq
--------------------
type seq<'T> = System.Collections.Generic.IEnumerable<'T>
Full name: Microsoft.FSharp.Collections.seq<_>
val ys : int []
Full name: fsharpcheatsheet.ys
val zs : int list
Full name: fsharpcheatsheet.zs
Multiple items
module List
from Microsoft.FSharp.Collections
--------------------
type List<'T> =
| ( [] )
| ( :: ) of Head: 'T * Tail: 'T list
interface IEnumerable
interface IEnumerable<'T>
member Head : 'T
member IsEmpty : bool
member Item : index:int -> 'T with get
member Length : int
member Tail : 'T list
static member Cons : head:'T * tail:'T list -> 'T list
static member Empty : 'T list
Full name: Microsoft.FSharp.Collections.List<_>

C# 8 Cheat Sheet

val init : length:int -> initializer:(int -> 'T) -> 'T list
Full name: Microsoft.FSharp.Collections.List.init
val fold : folder:('State -> 'T -> 'State) -> state:'State -> array:'T [] -> 'State
Full name: Microsoft.FSharp.Collections.Array.fold
val sprintf : format:Printf.StringFormat<'T> -> 'T
Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.sprintf
val last : xs:'a list -> 'a
Full name: fsharpcheatsheet.last
val reduce : reduction:('T -> 'T -> 'T) -> list:'T list -> 'T
Full name: Microsoft.FSharp.Collections.List.reduce
val ys' : int []
Full name: fsharpcheatsheet.ys'
val iter : action:('T -> unit) -> list:'T list -> unit
Full name: Microsoft.FSharp.Collections.List.iter
val x : int * string
Full name: fsharpcheatsheet.x
val y : string * string * string
Full name: fsharpcheatsheet.y
val b' : string
Full name: fsharpcheatsheet.b'
val fst : tuple:('T1 * 'T2) -> 'T1
Full name: Microsoft.FSharp.Core.Operators.fst
val snd : tuple:('T1 * 'T2) -> 'T2
Full name: Microsoft.FSharp.Core.Operators.snd
val print' : 'a * 'b -> unit
Full name: fsharpcheatsheet.print'
val a : 'a
type Person =
{Name: string;
Age: int;}
member Info : string * int
Full name: fsharpcheatsheet.Person
Person.Age: int
Multiple items
val int : value:'T -> int (requires member op_Explicit)
Full name: Microsoft.FSharp.Core.Operators.int
--------------------
type int = int32
Full name: Microsoft.FSharp.Core.int
--------------------
type int<'Measure> = int
Full name: Microsoft.FSharp.Core.int<_>
val paulsTwin : Person
Full name: fsharpcheatsheet.paulsTwin
member Person.Info : string * int
Full name: fsharpcheatsheet.Person.Info
val isPaul : person:Person -> bool
Full name: fsharpcheatsheet.isPaul
type Tree<'T> =
| Node of Tree<'T> * 'T * Tree<'T>
| Leaf
Full name: fsharpcheatsheet.Tree<_>
union case Tree.Node: Tree<'T> * 'T * Tree<'T> -> Tree<'T>
val depth : _arg1:Tree<'a> -> int
Full name: fsharpcheatsheet.depth
val r : Tree<'a>
val max : e1:'T -> e2:'T -> 'T (requires comparison)
Full name: Microsoft.FSharp.Core.Operators.max
val optionPatternMatch : input:int option -> unit
Full name: fsharpcheatsheet.optionPatternMatch
union case Option.Some: Value: 'T -> Option<'T>
type OrderId = | Order of string
Full name: fsharpcheatsheet.OrderId
val orderId : OrderId
Full name: fsharpcheatsheet.orderId
val divideFailwith : x:int -> y:int -> int
Full name: fsharpcheatsheet.divideFailwith
val failwith : message:string -> 'T
Full name: Microsoft.FSharp.Core.Operators.failwith
val divide : x:int -> y:int -> int option
Full name: fsharpcheatsheet.divide
Multiple items
type DivideByZeroException =
inherit ArithmeticException
new : unit -> DivideByZeroException + 2 overloads
Full name: System.DivideByZeroException
--------------------
System.DivideByZeroException() : unit
System.DivideByZeroException(message: string) : unit
System.DivideByZeroException(message: string, innerException: exn) : unit
exception InnerError of string
Full name: fsharpcheatsheet.InnerError
exception OuterError of string
Full name: fsharpcheatsheet.OuterError
val handleErrors : x:'a -> y:'a -> unit (requires equality)
Full name: fsharpcheatsheet.handleErrors
val y : 'a (requires equality)
val raise : exn:System.Exception -> 'T
Full name: Microsoft.FSharp.Core.Operators.raise
Multiple items
type Vector =
new : x:float * y:float -> Vector
member Scale : s:float -> Vector
member Mag : float
member X : float
member Y : float
static member ( + ) : a:Vector * b:Vector -> Vector
Full name: fsharpcheatsheet.Vector
--------------------
new : x:float * y:float -> Vector
Multiple items
val float : value:'T -> float (requires member op_Explicit)
Full name: Microsoft.FSharp.Core.Operators.float
--------------------
type float = System.Double
Full name: Microsoft.FSharp.Core.float
--------------------
type float<'Measure> = float
Full name: Microsoft.FSharp.Core.float<_>
val mag : float
val sqrt : value:'T -> 'U (requires member Sqrt)
Full name: Microsoft.FSharp.Core.Operators.sqrt
member Vector.X : float
Full name: fsharpcheatsheet.Vector.X
member Vector.Y : float
Full name: fsharpcheatsheet.Vector.Y

C# Cheatsheet 2020

member Vector.Mag : float
Full name: fsharpcheatsheet.Vector.Mag
member Vector.Scale : s:float -> Vector
Full name: fsharpcheatsheet.Vector.Scale
val a : Vector
property Vector.X: float
Multiple items
type Animal =
new : unit -> Animal
member Rest : unit -> unit
Full name: fsharpcheatsheet.Animal
--------------------
new : unit -> Animal
member Animal.Rest : unit -> unit
Full name: fsharpcheatsheet.Animal.Rest
Multiple items
type Dog =
inherit Animal
new : unit -> Dog
member Run : unit -> unit
Full name: fsharpcheatsheet.Dog
--------------------
new : unit -> Dog
member Dog.Run : unit -> unit
Full name: fsharpcheatsheet.Dog.Run
val animal : Animal
Full name: fsharpcheatsheet.animal
val shouldBeADog : Dog
Full name: fsharpcheatsheet.shouldBeADog
type IVector =
interface
abstract member Scale : float -> IVector
end
Full name: fsharpcheatsheet.IVector
abstract member IVector.Scale : float -> IVector
Full name: fsharpcheatsheet.IVector.Scale
Multiple items
type Vector' =
interface IVector
new : x:float * y:float -> Vector'
member X : float
member Y : float
Full name: fsharpcheatsheet.Vector'
--------------------
new : x:float * y:float -> Vector'
override Vector'.Scale : s:float -> IVector
Full name: fsharpcheatsheet.Vector'.Scale
member Vector'.X : float
Full name: fsharpcheatsheet.Vector'.X
member Vector'.Y : float
Full name: fsharpcheatsheet.Vector'.Y
type ICustomer =
interface
abstract member Age : int
abstract member Name : string
end
Full name: fsharpcheatsheet.ICustomer
abstract member ICustomer.Name : string
Full name: fsharpcheatsheet.ICustomer.Name
abstract member ICustomer.Age : int
Full name: fsharpcheatsheet.ICustomer.Age
val createCustomer : name:string -> age:int -> ICustomer
Full name: fsharpcheatsheet.createCustomer
val age : int
property ICustomer.Age: int
val testNumber : i:int -> unit
Full name: fsharpcheatsheet.testNumber

C# Syntax Cheat Sheet

active recognizer Even: int -> Choice<unit,unit>
Full name: fsharpcheatsheet.( |Even|Odd| )

C# Programming Cheat Sheet

active recognizer Odd: int -> Choice<unit,unit>
Full name: fsharpcheatsheet.( |Even|Odd| )

C Sharp Cheat Sheet

val fizzBuzz : _arg1:int -> string
Full name: fsharpcheatsheet.fizzBuzz

C Sharp Syntax Cheat Sheet

active recognizer DivisibleBy: int -> int -> unit option
Full name: fsharpcheatsheet.( |DivisibleBy|_| )




Coments are closed