Difference between revisions of "Csharp programming syntax"

From WireCAD Online Help
Jump to: navigation, search
(Created page with "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...")
 
 
(17 intermediate revisions by the same user not shown)
Line 1: Line 1:
 +
'''What follows is a basic look at the C# programming language.<br>'''
 +
 
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.
 
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.
  
Line 7: Line 9:
 
C# uses the ; to terminate statements. As a result statements can have whitespace formatting characters(tabs, line feeds, carriage returns, etc.).
 
C# uses the ; to terminate statements. As a result statements can have whitespace formatting characters(tabs, line feeds, carriage returns, etc.).
  
Example:
+
'''Example:'''
//This is a comment and will not execute
+
<syntaxhighlight lang="csharp">
 
  CallSomeFunction(param1, param2, param3);
 
  CallSomeFunction(param1, param2, param3);
 
+
</syntaxhighlight>
is the same as:
+
'''is the same as:'''<br>
 
+
<syntaxhighlight lang="csharp">
 +
 
  CallSomefunction(param1,
 
  CallSomefunction(param1,
 
  :param2,
 
  :param2,
 
  :param3);
 
  :param3);
 +
</syntaxhighlight>
  
 +
Comments are lines of text that describe your code. Good comments help you remember what you were doing, aid others reading your code, and hide code from the compiler.
 +
Comments in C# can take two forms:
 +
# The single line comment "\\" appears anywhere on a line. Any text following the \\is ignored by the compiler.
 +
# The Multiline/Inline/Block comment opens the comment with /* and closes it with */. Anything between opening and closing is ignored by the compiler.
  
 +
Example:
  
 +
<syntaxhighlight lang="csharp">
 +
//The following line of code do something
 +
DoSomething(); //we just did something
  
 +
/*Here
 +
we
 +
have a
 +
multiline comment */
 +
DoSomething(/*Here we pass our parameter a*/ a); //look at that. We just used a comment inline.
  
 +
</syntaxhighlight>
  
===Variable Declaration==
 
  
 +
 +
===Variable Declaration===
 +
//value types can be initialized upon instantiation<br>
 +
[scope] Type name = value;
 +
 +
//reference types can be initialized upon instantiation<br>
 
[scope] Type name = new Type(contructor params);
 
[scope] Type name = new Type(contructor params);
 +
 +
//or not<br>
 +
[scope] Type name = null;
 +
 +
 +
 +
Value Type Example:
 +
<syntaxhighlight lang="csharp">
 +
private int myInt = 0;
 +
</syntaxhighlight>
 +
 +
Reference Type Example:
 +
<syntaxhighlight lang="csharp">
 +
private string myNewString = "fred";
 +
string myNewString = null;
 +
SomeObject myObject = new SomeObject(Param1);
 +
SomeObject myOtherObject = null;
 +
</syntaxhighlight>
 +
 +
===Operators===
 +
Assignment Operator: =  example: this.Text = "fred"; will assign the name fred to the form caption<br>
 +
Equality Operator: == example: if(this.Text == "fred")do something here;<br>
 +
OR Operator: || example: if(this || that) -- read as: if this OR that then<br>
 +
AND Operator: && example: if(this && that) -- read as: if this AND that then<br>
 +
Addition: + example: 1+1 = 2<br>
 +
Subtraction: - example: 1-1 = 0<br>
 +
Multiplication: * example: 2*1 = 2<br>
 +
Division: / example: 10/2=5<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>
 +
 +
 +
 +
===Conditional Execution===
 +
====The If Statement====
 +
<br style="clear:both"/>
 +
<syntaxhighlight lang="csharp">//the if statement<br>
 +
if(a == a)<br>
 +
{
 +
//do something;
 +
....
 +
}
 +
else if(a == b)
 +
{
 +
//do something else;
 +
....
 +
}
 +
else
 +
{
 +
//nothing else evaluates to true so do this;
 +
....
 +
}
 +
 +
//simplified
 +
if(1==1)
 +
SingleStatementToExecute;
 +
else
 +
SomethingElse;
 +
</syntaxhighlight >
 +
 +
====The Switch Statement====
 +
The switch statement is there to help you avoid writing complex else if() statements.
 +
 +
<syntaxhighlight lang="csharp">
 +
//the switch statement
 +
switch(SomeVar)
 +
{
 +
case someConstant: doStuffHere; //same as if(SomeVar == someConstant)then
 +
break;
 +
 +
case someOtherConstant: doOtherStuffHere; //same as if(SomeVar == someOtherConstant)then
 +
break;
 +
 +
default:
 +
break;
 +
}
 +
</syntaxhighlight >
 +
 +
 +
==Class Declaration==
 +
 +
<syntaxhighlight lang="csharp">
 +
 +
Class MySample : MyClass, IMyInterface
 +
{
 +
int myInt;
 +
public MySample(int myInt)
 +
{
 +
this.myInt = myInt ;
 +
}
 +
void Inc()
 +
{
 +
++myInt;
 +
}
 +
void EmptyMethod()
 +
{
 +
}
 +
}
 +
</syntaxhighlight>

Latest revision as of 19:09, 24 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);

Comments are lines of text that describe your code. Good comments help you remember what you were doing, aid others reading your code, and hide code from the compiler. Comments in C# can take two forms:

  1. The single line comment "\\" appears anywhere on a line. Any text following the \\is ignored by the compiler.
  2. The Multiline/Inline/Block comment opens the comment with /* and closes it with */. Anything between opening and closing is ignored by the compiler.

Example:

//The following line of code do something
DoSomething(); //we just did something
 
/*Here 
we 
have a
multiline comment */
DoSomething(/*Here we pass our parameter a*/ a); //look at that. We just used a comment inline.


Variable Declaration

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

//reference types can be initialized upon 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


//the if statement<br>
if(a == a)<br>
{
 //do something;
 ....
}
else if(a == b)
{
 //do something else;
 ....
}
else 
{
 //nothing else evaluates to true so do this;
 ....
}
 
//simplified
if(1==1)
SingleStatementToExecute;
else
SomethingElse;

The Switch Statement

The switch statement is there to help you avoid writing complex else if() statements.

//the switch statement
switch(SomeVar)
{
 case someConstant: doStuffHere; //same as if(SomeVar == someConstant)then
 break;
 
 case someOtherConstant: doOtherStuffHere; //same as if(SomeVar == someOtherConstant)then
 break;
 
 default:
 break;
}


Class Declaration

Class MySample : MyClass, IMyInterface
{
	int myInt;
	public MySample(int myInt)
	{
	 this.myInt = myInt ;
	}
	void Inc()
	{
		++myInt;
	}
	void EmptyMethod()
	{
	}
}