summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
-rw-r--r--bash.html.markdown64
-rw-r--r--csharp.html.markdown81
2 files changed, 122 insertions, 23 deletions
diff --git a/bash.html.markdown b/bash.html.markdown
index 708131bd..4d80545e 100644
--- a/bash.html.markdown
+++ b/bash.html.markdown
@@ -4,6 +4,7 @@ tool: bash
contributors:
- ["Max Yankov", "https://github.com/golergka"]
- ["Darren Lin", "https://github.com/CogBear"]
+ - ["Alexandre Medeiros", "http://alemedeiros.sdf.org"]
filename: LearnBash.sh
---
@@ -35,8 +36,22 @@ VARIABLE = "Some string"
# Using the variable:
echo $VARIABLE
echo "$VARIABLE"
+echo '$VARIABLE'
# When you use the variable itself — assign it, export it, or else — you write
# its name without $. If you want to use variable's value, you should use $.
+# Note that ' (single quote) won't expand the variables!
+
+# String substitution in variables
+echo ${VARIABLE/Some/A}
+# This will substitute the first occurance of "Some" with "A"
+
+# Bultin variables:
+# There are some useful builtin variables, like
+echo "Last program return value: $?"
+echo "Script's PID: $$"
+echo "Number of arguments: $#"
+echo "Scripts arguments: $@"
+echo "Scripts arguments separeted in different variables: $1 $2..."
# Reading a value from input:
echo "What's your name?"
@@ -44,13 +59,18 @@ read NAME # Note that we didn't need to declare new variable
echo Hello, $NAME!
# We have the usual if structure:
-if true
+# use 'man test' for more info about conditionals
+if [ $NAME -ne $USER ]
then
- echo "This is expected"
+ echo "Your name is you username"
else
- echo "And this is not"
+ echo "Your name isn't you username"
fi
+# There is also conditional execution
+echo "Always executed" || echo "Only executed if first command fail"
+echo "Always executed" && echo "Only executed if first command does NOT fail"
+
# Expressions are denoted with the following format:
echo $(( 10 + 5 ))
@@ -67,6 +87,13 @@ ls -l # Lists every file and directory on a separate line
# txt files in the current directory:
ls -l | grep "\.txt"
+# You can also redirect a command output, input and error output.
+python2 hello.py < "input.in"
+python2 hello.py > "output.out"
+python2 hello.py 2> "error.err"
+# The output error will overwrite the file if it exists, if you want to
+# concatenate them, use ">>" instead.
+
# Commands can be substitued within other commands using $( ):
# The following command displays the number of files and directories in the
# current directory.
@@ -80,11 +107,36 @@ case "$VARIABLE" in
*) echo "It is not null.";;
esac
-#For loops iterate for as many arguments given:
-#The contents of var $VARIABLE is printed three times.
-for VARIABLE in x y z
+# For loops iterate for as many arguments given:
+# The contents of var $VARIABLE is printed three times.
+# Note that ` ` is equivalent to $( ) and that seq returns a sequence of size 3.
+for VARIABLE in `seq 3`
do
echo "$VARIABLE"
done
+# You can also define functions
+# Definition:
+foo ()
+{
+ echo "Arguments work just like script arguments: $@"
+ echo "And: $1 $2..."
+ echo "This is a function"
+ return 0
+}
+
+# Calling your function
+foo "My name is" $NAME
+
+# There are a lot of useful commands you should learn:
+tail -n 10 file.txt
+# prints last 10 lines of file.txt
+head -n 10 file.txt
+# prints first 10 lines of file.txt
+sort file.txt
+# sort file.txt's lines
+uniq -d file.txt
+# report or omit repeated lines, with -d it reports them
+cut -d ',' -f 1 file.txt
+# prints only the first column before the ',' character
```
diff --git a/csharp.html.markdown b/csharp.html.markdown
index d3adbd01..1471b833 100644
--- a/csharp.html.markdown
+++ b/csharp.html.markdown
@@ -3,6 +3,7 @@ language: c#
contributors:
- ["Irfan Charania", "https://github.com/irfancharania"]
- ["Max Yankov", "https://github.com/golergka"]
+ - ["Melvyn Laïly", "http://x2a.yt"]
filename: LearnCSharp.cs
---
@@ -95,17 +96,25 @@ namespace Learning
// Double - Double-precision 64-bit IEEE 754 Floating Point
// Precision: 15-16 digits
double fooDouble = 123.4;
+
+ // Decimal - a 128-bits data type, with more precision than other floating-point types,
+ // suited for financial and monetary calculations
+ decimal fooDecimal = 150.3m;
- // Bool - true & false
+ // Boolean - true & false
bool fooBoolean = true;
bool barBoolean = false;
// Char - A single 16-bit Unicode character
char fooChar = 'A';
- // Strings
+ // Strings -- unlike the previous base types which are all value types,
+ // a string is a reference type. That is, you can set it to null
string fooString = "My string is here!";
Console.WriteLine(fooString);
+ // You can access each character of the string with an indexer:
+ char charFromString = fooString[1]; // 'y'
+ // Strings are immutable: you can't do fooString[1] = 'X';
// formatting
string fooFs = string.Format("Check Check, {0} {1}, {0} {1:0.0}", 1, 2);
@@ -138,14 +147,21 @@ namespace Learning
const int HOURS_I_WORK_PER_WEEK = 9001;
// Nullable types
- // any type can be made nullable by suffixing a ?
+ // any value type (i.e. not a class) can be made nullable by suffixing a ?
// <type>? <var name> = <value>
int? nullable = null;
Console.WriteLine("Nullable variable: " + nullable);
- // In order to use nullable's value, you have to use Value property or to explicitly cast it
- string? nullableString = "not null";
- Console.WriteLine("Nullable value is: " + nullableString.Value + " or: " + (string) nullableString );
+ // In order to use nullable's value, you have to use Value property
+ // or to explicitly cast it
+ DateTime? nullableDate = null;
+ // The previous line would not have compiled without the '?'
+ // because DateTime is a value type
+ // <type>? is equivalent to writing Nullable<type>
+ Nullable<DateTime> otherNullableDate = nullableDate;
+
+ nullableDate = DateTime.Now;
+ Console.WriteLine("Nullable value is: " + nullableDate.Value + " or: " + (DateTime) nullableDate );
// ?? is syntactic sugar for specifying default value
// in case variable is null
@@ -153,6 +169,8 @@ namespace Learning
Console.WriteLine("Not nullable variable: " + notNullable);
// Var - compiler will choose the most appropriate type based on value
+ // Please note that this does not remove type safety.
+ // In this case, the type of fooImplicit is known to be a bool at compile time
var fooImplicit = true;
///////////////////////////////////////////////////
@@ -201,7 +219,7 @@ namespace Learning
// Others data structures to check out:
//
// Stack/Queue
- // Dictionary
+ // Dictionary (an implementation of a hash map)
// Read-only Collections
// Tuple (.Net 4+)
@@ -235,7 +253,6 @@ namespace Learning
~ Unary bitwise complement
<< Signed left shift
>> Signed right shift
- >>> Unsigned right shift
& Bitwise AND
^ Bitwise exclusive OR
| Bitwise inclusive OR
@@ -308,6 +325,18 @@ namespace Learning
//Iterated 10 times, fooFor 0->9
}
Console.WriteLine("fooFor Value: " + fooFor);
+
+ // For Each Loop
+ // foreach loop structure => foreach(<iteratorType> <iteratorName> in <enumerable>)
+ // The foreach loop loops over any object implementing IEnumerable or IEnumerable<T>
+ // All the collection types (Array, List, Dictionary...) in the .Net framework
+ // implement one or both of these interfaces.
+ // (The ToCharArray() could be removed, because a string also implements IEnumerable)
+ foreach (char character in "Hello World".ToCharArray())
+ {
+ //Console.WriteLine(character);
+ //Iterated over all the characters in the string
+ }
// Switch Case
// A switch works with the byte, short, char, and int data types.
@@ -327,6 +356,14 @@ namespace Learning
case 3:
monthString = "March";
break;
+ // You can assign more than one case to an action
+ // But you can't add an action without a break before another case
+ // (if you want to do this, you would have to explicitly add a goto case x
+ case 6:
+ case 7:
+ case 8:
+ monthString = "Summer time!!";
+ break;
default:
monthString = "Some other month";
break;
@@ -335,7 +372,7 @@ namespace Learning
///////////////////////////////////////
- // Converting Data Types And Typcasting
+ // Converting Data Types And Typecasting
///////////////////////////////////////
// Converting data
@@ -389,7 +426,7 @@ namespace Learning
// Class Declaration Syntax:
- // <public/private/protected> class <class name>{
+ // <public/private/protected/internal> class <class name>{
// //data fields, constructors, functions all inside.
// //functions are called as methods in Java.
// }
@@ -404,17 +441,20 @@ namespace Learning
string name; // Everything is private by default: Only accessible from within this class
// Enum is a value type that consists of a set of named constants
+ // It is really just mapping a name to a value (an int, unless specified otherwise).
+ // The approved types for an enum are byte, sbyte, short, ushort, int, uint, long, or ulong.
+ // An enum can't contain the same value twice.
public enum Brand
{
AIST,
BMC,
- Electra,
+ Electra=42, //you can explicitly set a value to a name
Gitane
}
// We defined this type inside a Bicycle class, so it is a nested type
// Code outside of this class should reference this type as Bicycle.Brand
- public Brand brand; // After declaing an enum type, we can declare the field of this type
+ public Brand brand; // After declaring an enum type, we can declare the field of this type
// Static members belong to the type itself rather then specific object.
static public int bicyclesCreated = 0;
@@ -459,7 +499,7 @@ namespace Learning
// <public/private/protected> <return type> <function name>(<args>)
// classes can implement getters and setters for their fields
- // or they can implement properties
+ // or they can implement properties (this is the preferred way in C#)
// Method declaration syntax:
// <scope> <return type> <method name>(<args>)
@@ -474,13 +514,14 @@ namespace Learning
cadence = newValue;
}
- // virtual keyword indicates this method can be overridden
+ // virtual keyword indicates this method can be overridden in a derived class
public virtual void SetGear(int newValue)
{
gear = newValue;
}
- // Method parameters can have defaut values. In this case, methods can be called with these parameters omitted
+ // Method parameters can have default values.
+ // In this case, methods can be called with these parameters omitted
public void SpeedUp(int increment = 1)
{
_speed += increment;
@@ -500,6 +541,12 @@ namespace Learning
get { return _hasTassles; }
set { _hasTassles = value; }
}
+
+ // You can also define an automatic property in one line
+ // this syntax will create a backing field automatically.
+ // You can set an access modifier on either the getter or the setter (or both)
+ // to restrict its access:
+ public bool IsBroken { get; private set; }
// Properties can be auto-implemented
public int FrameSize
@@ -525,7 +572,7 @@ namespace Learning
// Methods can also be static. It can be useful for helper methods
public static bool DidWeCreateEnoughBycles()
{
- // Within a static method, we only can reference static class memebers
+ // Within a static method, we only can reference static class members
return bicyclesCreated > 9000;
} // If your class only needs static members, consider marking the class itself as static.
@@ -564,7 +611,7 @@ namespace Learning
interface IBreakable
{
- bool Broken { get; } // interfaces can contain properties as well as methods, fields & events
+ bool Broken { get; } // interfaces can contain properties as well as methods & events
}
// Class can inherit only one other class, but can implement any amount of interfaces