using System;
class MainClass{
public static void Main() {
// Initialize a variable at declaration
short x = 5;
// Initialize a variable as a copy of another
int y = x;
double z = y + 10.25;
int a = (int)z;
Console.WriteLine("X = {0} Y = {1} Z = {2}", x, y, z);
Console.WriteLine("A = {0}", a);
}
}
ASP.NET & Csharp (C#)
ASP.NET & Csharp (C#) , Here you can find my posts about .NET c# .
Declaring a variable.
class MainClass {
public static void Main() {
short x;
int y;
double z = 0;
x = 6;
y = 10;
z = x + y;
System.Console.WriteLine("X = {0} Y = {1} Z = {2}", x, y, z);
}
}
Demonstrate using statement
/*
C#: The Complete Reference
by Herbert Schildt
Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/
// Demonstrate using statement.
using System;
using System.IO;
public class UsingDemo {
public static void Main() {
StreamReader sr = new StreamReader("test.txt");
// Use object inside using statement.
using(sr) {
Console.WriteLine(sr.ReadLine());
sr.Close();
}
// Create StreamReader inside the using statement.
using(StreamReader sr2 = new StreamReader("test.txt")) {
Console.WriteLine(sr2.ReadLine());
sr2.Close();
}
}
}
This version does not include the using System statement
/*
C#: The Complete Reference
by Herbert Schildt
Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/
// This version does not include the using System statement.
public class ExampleConsoleWriteLine {
// A C# program begins with a call to Main().
public static void Main() {
// Here, Console.WriteLine is fully qualified.
System.Console.WriteLine("A simple C# program.");
}
}
Compare pointer
// compile with: /unsafe
class CompareOperators
{
unsafe static void Main()
{
int x = 234;
int y = 236;
int* p1 = &x;
int* p2 = &y;
System.Console.WriteLine(p1 < p2); System.Console.WriteLine(p2 < p1); } } [/csharp]
Pointer for struct
// compile with: /unsafe
struct Point
{
public int x, y;
}
class MainClass12
{
unsafe static void Main()
{
Point pt = new Point();
Point* pp = &pt;
pp->x = 123;
pp->y = 456;
Console.WriteLine("{0} {1}", pt.x, pt.y);
}
}
Use unsage code to swap two integers
public class TestUnsafeApp
{
unsafe public static void Swap(int* pi, int* pj)
{
int tmp = *pi;
*pi = *pj;
*pj = tmp;
}
public static void Main(string[] args)
{
int i = 3;
int j = 4;
Console.WriteLine("BEFORE: i = {0}, j = {1}", i, j);
unsafe { Swap(&i, &j); }
Console.WriteLine("AFTER: i = {0}, j = {1}", i, j);
}
}