About Data Types and Casting in C#
 
 
 

C# is a strongly typed language. All basic types are part of the the System namespace (see Comparing Data Types across Languages for more information on type in C#).

Unlike scripting, C# doesn't do type coercion automatically if the calling function has arguments that don't match the value so the caller must perform the type cast explicitly:

oCmd.SetFlag( (int)siCommandCapabilities.siCannotBeUsedInBatch, true );

Comparing C# to Python and JScript

This table sketches the similarities and differences between C#, Python and JScript:

C#

Python

JScript

int x = 3;
string y = "foo";
FooBarQux fbq = make_fbq ();
x = 3
y = "foo"
fbq = make_fbq()
var x = 3;
var y = "foo";
var fbq = make_fbq ();
expr_1 = expr_2 = expr_3;
expr_1 = expr_2 = expr_3
expr_1 = expr_2 = expr_3;
Class c = new Class (params)
c = Class(params)
var c = new Class (params)
Class[] c = new Class [size]
c = array(Class, size)
var c = new Array(size)
Type[] l = new Type[] { expr_1, expr_1, ..., expr_n }
l = (expr_1, expr_1, ..., expr_n)
var l = new Array { expr_1, expr_1, ..., expr_n }
if (cond) return foo;
do_something ();
return bar;
if cond:
    return foo

//You can also say:
return foo if cond
do_something()
return bar
if (cond) return foo;
do_something ();
return bar;
if (cond) answer = 42;
if cond:
    answer = 42
//or
answer = 42 if cond
if (cond) answer = 42;
if (!cond) answer = 42;
answer = 42 if not cond
if (!cond) answer = 42;
try ...
catch (FooException e) { ... }
catch (BarException e) { ... }
try:
    ...
except e as FooException:
    ...
except e as BarException:
    ...
try ...
catch (e) { ... }
try { foo (); bar (); }
catch (Exception e) { baz (); }
finally { qux (); }
try:
    foo()
    bar()
except e:
    baz()
ensure:
    qux()
try { foo (); bar (); }
catch (e) { baz (); }
finally { qux (); }
throw new System.ArgumentException ("foo");
raise System.ArgumentException("foo")
throw new Error("foo");
type t = ((type) expr)
//exception if cast fails:
t = cast(type, expr)
//or
//null if cast fails:
t = expr as type

(type is loose in JScript, implicit casting)

using System.Windows.Forms;
Button button = control as Button;
if (button != null) ...
else ...
import System.Windows.Forms
button = control as Button
if button != null:
    ...
else:
    ...

JScript doesn't really have the concept of modules, but you can import other JScript libraries by reading them into global memory:

var g_fso = new ActiveXObject( "Scripting.FileSystemObject" );
var g_ts = g_fso.OpenTextFile( "MyJSFunctions.js" );
var g_lib = g_ts.ReadAll();
g_ts.Close();
eval( g_lib );
using System;
using SWF = System.Windows.Forms;
using System.Xml;
...
Console.WriteLine ("foo");
SWF.Form x = new SWF.Form ();
XmlDocument doc = new XmlDocument ();
import System
import System.Windows.Forms as SWF
import System.Xml

print "foo"
x = SWF.Form()
doc = XmlDocument()

JScript doesn't use namespaces.

x++;
++x;
x++
++x
x++;
++x;
readonly int X = 2;
const int Y = 3;
final X = 2
static final Y = 3

There are no constants or read-only variables in JScript.