Difference between revisions of "Csharp programming syntax"

From WireCAD Online Help
Jump to: navigation, search
Line 48: Line 48:
  
 
===Operators===
 
===Operators===
Assignment Operator: =<br>
+
Assignment Operator: = example: this.Text = "fred"; will assign the name fred to the form caption<br>
Equality Operator: ==<br>
+
Equality Operator: == example: if(this.Text == "fred")do something here;<br>
OR Operator: ||<br>
+
OR Operator: || example: if(this || that) -- read as if this OR that then<br>
AND Operator: &&<br>
+
AND Operator: && example: if(this && that) -- read as if this AND that then<br>
Addition: +<br>
+
Addition: + example: 1+1 = 2<br>
Subtraction: -<br>
+
Subtraction: - example: 1-1 = 0<br>
Multiplication: *<br>
+
Multiplication: * example: 2*1 = 2<br>
Division: /<br>
+
Division: / example: 10/2=5<br>
 
Add to existing: += example: x += y  is equivalent to  x = x + y<br>
 
Add to existing: += example: x += y  is equivalent to  x = x + y<br>
 
Subtract from existing: -= example: x -= y is equivalent to x = x - y<br>
 
Subtract from existing: -= example: x -= y is equivalent to x = x - y<br>

Revision as of 19:30, 23 December 2010

What follows is a basic look at the C# programming language.

C# is a strongly typed language. That means that the variable type matters. You cannot assign a variable of one type to another of a different type without performing a type cast. C# is compiled into IL code that is then compiled just in time by the .NET framework. The .NET framework handles things like program execution and memory management. As a result C# is considered "managed code." C# is case sensitive so Var1 is different from var1.


Basic Syntax

White Space and Comments

C# uses the ; to terminate statements. As a result statements can have whitespace formatting characters(tabs, line feeds, carriage returns, etc.).

Example:

 CallSomeFunction(param1, param2, param3);

is the same as:

 
 CallSomefunction(param1,
 :param2,
 :param3);


Variable Declaration

//value types can be initialized upon instantiation
[scope] Type name = value;

//reference types can be initialized up instantiation
[scope] Type name = new Type(contructor params);

//or not
[scope] Type name = null;


Value Type Example:

 private int myInt = 0;

Reference Type Example:

 private string myNewString = "fred";
 string myNewString = null;
 SomeObject myObject = new SomeObject(Param1);
 SomeObject myOtherObject = null;

Operators

Assignment Operator: = example: this.Text = "fred"; will assign the name fred to the form caption
Equality Operator: == example: if(this.Text == "fred")do something here;
OR Operator: || example: if(this || that) -- read as if this OR that then
AND Operator: && example: if(this && that) -- read as if this AND that then
Addition: + example: 1+1 = 2
Subtraction: - example: 1-1 = 0
Multiplication: * example: 2*1 = 2
Division: / example: 10/2=5
Add to existing: += example: x += y is equivalent to x = x + y
Subtract from existing: -= example: x -= y is equivalent to x = x - y


Conditional Execution


//the if statement<br>
if(a == a)<br>
{
 //do something;
 ....
}
else<br>
{
 //do something else;
 ....
}
 
//simplified
if(1==1)
SingalStatementToExecute;
else
SomethingElse;