Florian Rappl, MVP Visual C#
2 + 1im # => Complex{Int64}
2//3 # => Rational{Int64}
fib(n) = n > 2 ? fib(n - 1) + fib(n - 2) : n
@time fib(40) # measure execution time
code_native(fib, (Int32,)) # native code for Int32
2 < 3 < 2 # chained comparison
"2 + 2 = $(2 + 2)" # => "2 + 2 = 4"
vector = [1, 2, 3, 5]
range = [1:5] # => 5-element Int64 Array: [1,2,3,4,5]
matrix = [1 2; 3 4] # => 2x2 Int64 Array: [1 2; 3 4]
a, b, c = (1, 2, 3) # create and unpack tuples
a, b = b, a # swap variables
type Tiger
taillength::Float64
coatcolor # not including a type annotation leads to `::Any`
end
tiger = Tiger(3.5, "red")
typeof(Tiger)
subtypes(Number)
abstract Cat
type Lion <: Cat # Lion is a subtype of Cat
mane_color
roar::String
end
Lion(roar::String) = Lion("green",roar)
package main
import ("fmt")
func multiple(x, y int) (sum, prod int) {
return x + y, x * y // multiple return values
}
func main() {
var x int // declared only
var fv1 float64 = 0.75 // initialized directly
x = 3 // assign value
y := 4 // define value
sum, prod := multiple(x, y) // resolve multiple
fmt.Println("sum:", sum, "prod:", prod)
}
use std;
fn add2(x: i32, y: i32) -> i32 {
x + y // implicit return (no semicolon)
}
fn main() {
let x: i32 = 1; // immutable
let mut y = 1; // mutable
struct Point { x: i32, y: i32 } // C-like
enum Optional<T> { SomeVal(T), NoVal }
for i in 0u32..10 { // range
print!("{} ", i); // printf Macro
}
}
fn peep_inside_box(borrowed_int: &i32) {
println!("This int is: {}", borrowed_int);
}
fn main() {
let mut integer = 5i32;
{
let ref_to_integer = &_integer;// Borrow `integer`
integer = 4; // Error - `integer` is frozen
} // `ref_to_integer` goes out of scope
integer = 4; // `integer` unfrozen
}
import UIKit
let π = 3.1415926 // unicode variable names
let piText = "Pi = \(π), 2 Pi = \(π * 2)" // string interpolation
var shoppingList = ["catfish", "water", "lemons"] // mutable array
func getGasPrices() -> (Double, Double, Double) { // multiple return
return (3.59, 3.69, 3.79)
}
var suitValue: Suit = .Hearts // no need for repetition
extension Int { // extend built-in types
var customProperty: String { return "This is \(self)" }
func multiplyBy(num: Int) -> Int { return num * self }
}
import std.stdio;
enum withAlgorithm = true;
static if (withAlgorithm == true)
import std.algorithm;
void main() pure nothrow @safe {
ulong lines = 0;
double sum = 0;
foreach (line; stdin.byLine()) {
++lines;
sum += line.length;
}
}
Types | C# | JavaScript | Modern C++ |
---|---|---|---|
Value |
struct Val { } |
class Val { }; |
|
Reference |
class Foo : Base { public override void Bar() { } } |
class Foo : public Base { public: void bar() override { } }; |
|
Interface |
interface IFoo { int Bar(); } |
class IFoo { public: virtual int bar() = 0; }; |
|
Generic |
class Map<TKey, TValue> { } |
template<class TKey, class TValue> class Map { }; |
|
Optional |
Nullable<int> i = 10; |
var i = 10; |
boost::optional<int> i = 10; |
Dynamic |
dynamic x = "str"; x = 10; |
var x = "str"; x = 10; |
boost::any x = "string"; x = 10; |
// C-style without safety
enum Suit { Diamonds, Hearts, Clubs, Spades };
Suit good_hand = Clubs;
int other_type = Hearts;
Suit bad_hand = 1;//won't compile, thankfully
// Modern C++ with type-safety
enum class Suit { Diamonds, Hearts, Clubs, Spades };
Suit good_hand = Suit::Clubs;
int other_type = Suit::Hearts;//won't compile
Suit bad_hand = 1;//won't compile
#include <vector>
std::vector<double> values = { 1, 2, 3, 4, 5 };
std::vector<double> squares;
for (const auto value : values) {
squares.push_back(value * value);
}
#include <algorithm>
#include <functional>
std::function<double(double)> f = [](double x){
return x * x;
};
std::vector<double> values = { 1, 2, 3, 4, 5 };
std::vector<double> squares(values.size());
std::transform(values.begin(), values.end(), squares.begin(), f);
#include <thread>
#include <future>
auto future = std::async([&]() {
return "Hello World";
});
auto consumer = std::thread([&]() {
std::cout << future.get();
});
const auto a = u8"I'm a UTF-8 string.";
const auto b = u"This is a UTF-16 string.";
const auto c = U"This is a UTF-32 string.";
#include <string>
std::string operator "" _s(const char* ptr, size_t n) {
return std::string(ptr, n);
}
auto mychr = "Hello World!";//auto is char*
auto mystr = "Hello World!"_s;//auto is std::string
#include <tuple>
std::tuple<int, int, std::string> vals = std::make_tuple(1, 3, "Foo");
int first = get<0>(vals);
int second = get<1>(vals);
std::string third = get<2>(vals);
get<2>(vals) = "Bar";
Choosing C++ is a case of premature optimization.
Miguel de Icaza
Xamarin CTO
C++ | C# | |
---|---|---|
LINQ | Via Operators | Extension methods |
Reflection | Extensive preprocessor | Integrated |
Inheritence | Multiple, FCoI, NVI | Single-base, Interfaces |
Functional | Lambdas, Closures | Lambdas, Closures |
Meta | TMP, Preprocessor | PostSharp (AoP), T4 |
Memory | Stack, Heap (opt. RCSP, GC) | Stack, GCed Heap |
There are only two kinds of languages: the ones people complain about and the ones nobody uses.
Bjarne Stroustrup
Created C++