c# and javascript

the very basics

C# and javascript are free to use programming languages

they share a common ancestor in the C language - so what works in one will usually work in the other:

( parenthese are used in both to order arguments and group parameters )

{ braces are used in each to describe object bodies }

[ brackets are used to access indexes and declare arrays ]

" and then there are double quotes for strings... "

( parenthese are used in both to order arguments and group parameters )

parenthese order arguments:

answer = 10 * 10 + 10 / 10

given the statement:

what is answer?

answer = (10 * 10) + (10 / 10)
answer == 101


answer = (10 * 10 + 10) / 10
answer == 11

answer = 10 * (10 + 10 / 10)
answer == 110

answer = 10 * (10 + 10) / 10
answer == 20

parenthese order arguments:

answer = 10 * 10 + 10 / 10

given the statement:

answer = (10 * 10) + (10 / 10)
answer == 101

the operators, that is *, + and / have to work in a given order: * then / then +

parenthese group parameters:

function(parameter, parameter, parameter)

remember operators? operators are functions too.

a + b is the same as writing add(a, b)

operators are functions taking 2 parameters;

left operand operator right operand

operator(left operand, right operand)

{ braces are used in each to describe object bodies }

defining object bodies:

defining object bodies:

there are fundamentally two kinds of object:

an object that is something: a number, a string, a class...

an object that does something: a function, a method, a procedure...

an object that is something

with objects that do something:

a class has data

and functions

defining object bodies:

an object is:

10 // the number ten literal

and this object has:

10.toString()
10.toFixed(numberOfDecimalPlaces)
10.toFixed(numberOfDigits)

an object is:

var msg = "hello" // a string assignment

and this object has:

msg.toUpperCase()
msg.indexOf(needle) // find a substring
msg.repeat(numberOfTimes)

an object is:

var chrome = new ChromeDriver();

and this object has:

chrome.FindElement(logic)
chrome.Url // a property to get data
chrome.Navigate().GoToUrl()

and objects can contain objects, and functions can return objects

// javascript

defining object bodies:

a customer object:

// javascript

class Customer {
    constructor() {
        this._name = null;
    }
    get name() {
        return this._name;
    }
    set name(value) {
        this._name = value;
    }

    toString() {
        return "Customer: " + this._name;
    }
}

var customer = new Customer();

customer.Name = "Bob";

console.log(customer);

// > "Customer: Bob"
// c#

public class Customer {
    string _name;
    public string Name { 
        get {
            return this._name;
        }
        set {
            this._name = value;
        }   
    }

    public override string ToString() {
        return "Customer: " + this._name;
    }
}

var customer = new Customer();

customer.Name = "Bob";

Console.WriteLine(customer);

// > "Customer: Bob"

defining and using arrays:

[ brackets are used to access indexes and declare arrays ]

defining and using arrays:

arrays are a kind of object that have indexed access

you would use an array when you wanted to store more than one of the same type of object

// javascript
var primeNumbers = [2, 3, 5, 7, 11, 13,
                    17, 19, 23, 29];

var names = ["Bob", "Alice", "Fred"];

for (var i in names) {
    console.log(names[i]);
}

// > "Bob"
// > "Alice"
// > "Fred"
// c#
var primeNumbers = new[] {2, 3, 5, 7, 11, 13,
                    17, 19, 23, 29};

var names = new [] { "Bob", "Alice", "Fred" };

for (var i = 0; i < names.Length; i++) {
    Console.WriteLine(names[i]);
}

// > "Bob"
// > "Alice"
// > "Fred"

quotes and strings:

javascript and c# also differ slightly in the way they allow strings to be created

// javascript

var doubleStr = "hello world";

var singleStr = 'hello world';

var backtickStr = `hello world`;

var singleInDouble = "string in 'a string'";

var doubleInSingle = 'another "string"';

var codeInBackticks = `1 + 1 = ${1 + 1}`



var concatString = "hello" + " " + "world"
// c#

var doubleStr = "hello world";





var singleInDouble = "string in 'a string'";





var codeInBackticks = $"1 + 1 = {1 + 1}";

var concatString = "hello" + " " + "world"

var formatString = string.Format("{0} {1}",
                          "hello", "world");

sharpdevelop

free c# development environment

http://www.icsharpcode.net/opensource/sd/

visual studio

microsoft's c# development environment

http://www.visualstudio.com/

visual studio code

lightweight javascript and c# development

http://code.visualstudio.com/

github atom

lightweight hackable development

http://atom.io/

linqpad

fast c# development and experimentation

http://linqpad.net/

node.js

chrome v8 javascript engine

http://nodejs.org/

Think of a number. Add 4, then multiply the result by 4. Subtract 8, then divide the result by 4. Finally take away your original secret number. The answer is 2.

syntax in c# and js

By Benjamin Babik

syntax in c# and js

  • 170