Monday, March 3, 2008

How to use Operators in C#?

_____________________________________________

How to use Operators in C# ?

_____________________________________________

Comparison
== < > <= >= !=

Arithmetic
+ - * /
% (mod)
/ (integer division if both operands are ints)
Math.Pow(x, y)

Assignment
= += -= *= /= %= &= |= ^= <<= >>= ++ --


____________________________________________________________________

Note:Applicable for .net also asp.net.
____________________________________________________________________

How to use loops statement in C# ?

_____________________________________________

How to use loops statement in C# ?

_____________________________________________

Pre-test Loops:

// no "until" keyword
while (c < 10)
c++;

for (c = 2; c <= 10; c += 2)
Console.WriteLine(c);



Post-test Loop:

do
c++;
while (c < 10);



// Array or collection looping
string[] names = {"Fred", "Sue", "Barney"};
foreach (string s in names)
Console.WriteLine(s);

// Breaking out of loops
int i = 0;
while (true) {
if (i == 5)
break;
i++;
}

// Continue to next iteration
for (i = 0; i < 5; i++) {
if (i < 4)
continue;
Console.WriteLine(i); // Only prints 4
}


____________________________________________________________________

Note:Applicable for .net also asp.net.
____________________________________________________________________

How to use if statement in C# ?

_____________________________________________

How to use if statement in C#?

_____________________________________________

greeting = age <>? "What's up?" : "Hello";

if (age < greeting = "What's up?">else
greeting = "Hello";

// Multiple statements must be enclosed in {}
if (x != 100 && y <>

No need for _ or : since ; is used to terminate each statement.





if
(x > 5)
x *= y;
else if (x == 5)
x += y;
else if (x <>else
x /= y;



// Every case must end with break or goto case
switch (color) { // Must be integer or string
case "pink":
case "red": r++; break;
case "blue": b++; break;
case "green": g++; break;
default: other++; break; // break necessary on default
}


____________________________________________________________________

Note:Applicable for .net also asp.net.
____________________________________________________________________

Namespaces of C#

_____________________________________________

Namespaces of C#.

_____________________________________________

namespace Harding.Compsci.Graphics {
...
}

// or

namespace Harding {
namespace Compsci {
namespace Graphics {
...
}
}
}

using Harding.Compsci.Graphics;

____________________________________________________________________

Note:Applicable for .net also asp.net.
____________________________________________________________________

File I/O of C#

_____________________________________________

File I/O of C#.

_____________________________________________

using System.IO;

// Write out to text file
StreamWriter writer = File.CreateText("c:\\myfile.txt");
writer.WriteLine("Out to file.");
writer.Close();

// Read all lines from text file
StreamReader reader = File.OpenText("c:\\myfile.txt");
string line = reader.ReadLine();
while (line != null) {
Console.WriteLine(line);
line = reader.ReadLine();
}
reader.Close();

// Write out to binary file
string str = "Text data";
int num = 123;
BinaryWriter binWriter = new BinaryWriter(File.OpenWrite("c:\\myfile.dat"));
binWriter.Write(str);
binWriter.Write(num);
binWriter.Close();

// Read from binary file
BinaryReader binReader = new BinaryReader(File.OpenRead("c:\\myfile.dat"));
str = binReader.ReadString();
num = binReader.ReadInt32();
binReader.Close();


____________________________________________________________________

Note:Applicable for .net also asp.net.
____________________________________________________________________

Constructors / Destructors of C#

_____________________________________________

Program Structure in C#.

_____________________________________________

class SuperHero {
private int _powerLevel;

public SuperHero() {
_powerLevel = 0;
}

public SuperHero(int powerLevel) {
this._powerLevel= powerLevel;
}

~SuperHero() {
// Destructor code to free unmanaged resources.
// Implicitly creates a Finalize method
}
}


____________________________________________________________________

Note:Applicable for .net also asp.net.
____________________________________________________________________

Program Structure in C#

_____________________________________________

Program Structure in C#.

_____________________________________________

End Namespace using System;

namespace Hello {
public class HelloWorld {
public static void Main(string[] args) {
string name = "C#";

// See if an argument was passed from the command line
if (args.Length == 1)
name = args[0];

Console.WriteLine("Hello, " + name + "!");
}
}
}

____________________________________________________________________

Note:Applicable for .net also asp.net.
____________________________________________________________________