Archive for the ‘C#’ Category

Warning and error in C#

Sunday, January 25th, 2009

WARNING.CS
===

#warning This is A warning
class zzz
{
//#error This is An error
public static void Main()
{
System.Console.WriteLine(”MAIN”);
}
}

===

For other C# code or website scripts source code please visit http://fullycoded.com/

While and Writeline in c sharp

Tuesday, December 9th, 2008

C# code for While and Writeline

1. While1.cs :

class zzz
{
static void Main()
{
int ii;
ii=1;
while ( ii < = 5 )
{
System.Console.WriteLine (”Hi {0}”, ii);
ii++;
}
}
}

2. Writeline1.cs :

// created on 3/1/2008 at 12:36 AM
class callingfunction
{
static void Main(){

System.Console.WriteLine(”Hundred ={0}”,100);

}
}

Virtual Override

Monday, November 24th, 2008

Similar to C# code for virtual we provide you Virtual OverRide code

class zzz
{
public static void Main()
{
yyy a = new yyy();xxx b = new xxx();yyy c = new xxx();
a.abc();a.pqr();a.xyz();
b.abc();b.pqr();b.xyz();
c.abc();c.pqr();c.xyz();
}}
class yyy
{
public virtual void abc()
{System.Console.WriteLine(”yyy abc”);}
public virtual void pqr()
{System.Console.WriteLine(”yyy pqr”);}
public virtual void xyz()
{System.Console.WriteLine(”yyy xyz”);}
}
class xxx : yyy
{
public override void abc()
{System.Console.WriteLine(”xxx abc”);}
public new void pqr()
//New means that the function pqr is a new function and it has absolutely nothing to do with the pqr in the base class
{
System.Console.WriteLine(”xxx pqr”);
}
public void xyz()
{
System.Console.WriteLine(”xxx xyz”);
}}

Virtual.cs C# code

Wednesday, November 5th, 2008

Virtual.cs
===

class zzz
{
public static void Main()
{
yyy a = new xxx();
yyy b = new vvv();
xxx c = new vvv();
a.abc();a.pqr();a.xyz();b.abc();b.pqr();b.xyz();c.abc();c.pqr();c.xyz();}}
class yyy
{
public void abc()
{System.Console.WriteLine(”yyy abc”);}
public virtual void pqr()
{System.Console.WriteLine(”yyy pqr”);}
public virtual void xyz()
{System.Console.WriteLine(”yyy xyz”);}
}
class xxx : yyy
{
public virtual void abc()
{System.Console.WriteLine(”xxx abc”);}
public new void pqr()
{System.Console.WriteLine(”xxx pqr”);}
public override void xyz()
{System.Console.WriteLine(”xxx xyz”);}
}
class vvv : xxx
{
public override void abc()
{System.Console.WriteLine(”vvv abc”);}
public void xyz()
{System.Console.WriteLine(”vvv xyz”);}
}

Using.cs
===

using System;
using ashish;
class zzz {
static void Main()
{
yyy.abc();
}
}
namespace ashish
{
class yyy
{
public static void abc()
{
Console.WriteLine(”abc”);
}
}
}
//After the word using you can only write the name of a namespace…

Unsafe code in C#

Tuesday, October 21st, 2008

Unsafe : This means pointers are allowed in C# code. If you wish to compile unsafe code then should specify the /unsafe compiler option in the command line(This is not required if you are using Visual Studio.NET).

using System;
class zzz {
public static void Main() {
yyy a = new yyy();
a.abc();
}
}
unsafe class yyy {
unsafe public void abc() {
int *i; //declaring pointer
int j=1;
i = &j; //pointing
Console.WriteLine((int)i);
*i = 10;
Console.WriteLine(j);
}
}

C# code for Throw

Friday, October 10th, 2008

class zzz
{
public static void Main()
{
yyy a;
a=new yyy();
try
{
a.abc();
System.Console.WriteLine(”Bye”);
}
catch (System.Exception e)
{
System.Console.WriteLine(”In Exception”+ e.ToString());
}
System.Console.WriteLine(”After Exception”);
System.Console.Read();
}
}
class yyy {
public void abc()
{
throw new System.Exception(”C# Throw code exception”);
System.Console.WriteLine(”C# Throw code“);
}
}

Threads in C# programming

Tuesday, September 30th, 2008

1. C# Threads Example 1
using System.Threading;
public class yyy
{
public void abc()
{
for ( int i = 0; i< =3;i++)
{
System.Console.Write(i + ” “);
}
}
public void pqr()
{
for ( int i = 0; i<=3;i++)
{
Thread.Sleep(1000);
System.Console.Write(i+ “…”);
}
}
}
public class zzz
{
public static void Main()
{
yyy a = new yyy();
Thread t = new Thread(new ThreadStart(a.abc));
Thread t1 = new Thread(new ThreadStart(a.pqr));
t.Start();
t1.Start();
}
}

2. Example 2 of threading in C Sharp (C#)
using System;
using System.Threading;

class App {
public static void Main() {

Console.WriteLine(” (Hit or Press Enter to terminate the sample)”);
Timer timer = new Timer(new TimerCallback(CheckStatus), null, 1, 2000);

Console.WriteLine(”Press Enter to close window”);
Console.Read();
}

// The callback method’s signature MUST match that of a System.Threading.TimerCallback
// delegate (it takes an Object parameter and returns void)
static void CheckStatus(Object state) {
Console.WriteLine(”Threads in C # sharp”);

}
}

System Variables in C#

Sunday, August 31st, 2008

C# code for System Variables

class zzz
{
public static void Main(){
System.Collections.IDictionary i=System.Environment.GetEnvironmentVariables ();
System.Collections.IDictionaryEnumerator d=i.GetEnumerator();
System.Console.WriteLine(”Content-Type:text/html\n”);
System.Console.WriteLine(i.Count + “
“);
while (d.MoveNext())
{
System.Console.WriteLine(”{0}={1}
“,d.Key,d.Value);
}}}

Struct program in C#

Friday, July 25th, 2008

One of the simplest program for Struct…

===
class zzz
{
public static void Main()
{
xxx a = new xxx(10);
}
}
struct xxx
{
public int i,j;
public xxx(int p)
{
i = p;
j = 0;
}
}
===

Just compile it and run it to understand the c# code better.

Static in C#

Tuesday, June 3rd, 2008

Static in c#
===
class zzz
{
static void Main()
{
yyy a=new yyy();
a.abc();
yyy.pqr();
}
}
class yyy
{
public void abc()
{
System.Console.WriteLine(”C# at abc”);
}
public static void pqr()
{
System.Console.WriteLine(”C# at pqr”);
}
}

PS : VB Developers have the biggest problem understanding static when migrating from programming language Visual basic to a C#.

Basics of C#

Wednesday, May 14th, 2008

WriteLine.cs and Return.cs :

1. WriteLine.cs
===
// created on 3/1/2002 at 12:36 AM
class callingfunction
{
static void Main(){

System.Console.WriteLine(”Hundred ={0}”,100);

}
}
===

2. Return.cs
===
class zzz
{
static void Main()
{
int ii = abc();
System.Console.WriteLine(”hi {0}”,ii);
}
static int abc()
{
System.Console.WriteLine(”abc”);
return 100;
// after return everything ignored
System.Console.WriteLine(”abc”);
}
}
===

Reflection in c#

Tuesday, April 29th, 2008

Reflection is a process by which an application can read and collect data from assemby and metadata.

Reflection.cs
===
using System;
using System.Reflection;
class zzz
{
public static void Main()
{
Type m;
m = typeof(int);
System.Console.WriteLine(m.Name + ” ” + m.FullName);
m = typeof(System.Int32);
System.Console.WriteLine(m.Name + ” ” + m.FullName);
m = typeof(yyy);
System.Console.WriteLine(m.Name + ” ” + m.FullName);
m=typeof(string);
MemberInfo [] n;
n = m.GetMembers();
Console.WriteLine(n.Length);
foreach ( MemberInfo a in n)
{
Console.Write(a.Name+”,”);
}
System.Console.WriteLine(m.Name + ” ” + m.FullName);
}
}
class yyy
{
}
===

Read from Web using C#

Tuesday, April 22nd, 2008

Here again we will use System.Net and the classes we shall use in the c# code from the .NET framework are WebRequest and WebResponse. WebRequest and WebResponse classes from the .NET Library are used to to request web-pages from the internet.

Readnet.cs
===
using System;
using System.Net;
using System.IO;
class zzz
{
public static void Main()
{
WebRequest r = WebRequest.Create(”http://www.jimmysvalueworld.com”);
WebResponse re = r.GetResponse();
Stream s = re.GetResponseStream();
Byte[] a = new Byte[51200];
int b = s.Read(a, 0, 51200);
Console.WriteLine(b);
Console.Write(System.Text.Encoding.ASCII.GetString(a, 0, b));
}
}
===

We wish you a Happy new year

Tuesday, January 1st, 2008

The team at Fullycoded.com wishes you a very happy and prosperous new year :)

We promise to deliver you better website scripts and vb source code this year.

Regards,
ASHISH H THAKKAR and the People at Fullycoded.com

Read a file in C#

Friday, November 2nd, 2007

Read0.cs
===
using System;
using System.IO;
namespace ns{
class zzz
{
public static void Main()
{
FileStream f = new FileStream(”c:\\c\\pointers.cs”, FileMode.Open, FileAccess.Read);
byte [] a;
a = new byte[512];
int b = f.Read(a, 0, 512);
Console.WriteLine(b);
string s = System.Text.Encoding.ASCII.GetString(a, 0, b);
Console.Write(s);
}
}
}
==

Read.cs
===
using System.IO;
class read{
public static void Main()
{
File a=new File(”C:\\jimmy\\Comp\\Microsoft\\C#\\C#.txt”);
Stream sr=a.OpenRead();
int fp;
do{
fp=sr.ReadByte();
if(fp != -1)
System.Console.WriteLine(fp.ToString());
}
while( fp != -1);
sr.Close();
}
}
==

C# source code : Print page

Sunday, January 21st, 2007

Print.cs
using System;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Printing;
using System.IO;
public class zzz
{
public void abc()
{
PrintDocument pd = new PrintDocument();
pd.PrintPage += new PrintPageEventHandler(pqr);
pd.Print();
}
void pqr(object o, PrintPageEventArgs e)
{
Image i = Image.FromFile(”code.jpg”);
Point p = new Point(100, 100);
e.Graphics.DrawImage(i, p);
e.Graphics.DrawString (”C sharp source code”, new Font(”Courier New”,10), Brushes.Black, 100, 600);
}
public static void Main()
{
zzz a = new zzz();
a.abc();
}
}

Pointers in C#

Sunday, December 10th, 2006

Pointer.cs

using System;
class un{
public static void Main()
{
un1 o=new un1();
o.meth();
}
}
class un1
{
unsafe public void meth()
{
int *i;
int j=103;
i=&j;
Console.WriteLine((int)i);
Console.WriteLine(j);
}}

Pointer1.cs

using System;
class zzz
{
public static void Main()
{
yyy a = new yyy();
a.abc();
}}
class yyy
{
unsafe public void abc()
{
int *i;
int j=1, k = 1;
//An & in front of any variable tells us where it starts in memory
i = &j;
//pointing to j
Console.WriteLine((int)i);
*i = 10;
//changing value of j thru this
Console.WriteLine(j);
i = &k;
Console.WriteLine((int)i);
*i = 100;
Console.WriteLine(k + ” ” + j);
}}

Override in C#

Saturday, November 25th, 2006

Override.cs

class zzz
{
public static void Main()
{
yyy a = new yyy();xxx b = new xxx();yyy c = new xxx();
a.abc();a.pqr();a.xyz();
b.abc();b.pqr();b.xyz();
c.abc();c.pqr();c.xyz();
}}
class yyy
{
public virtual void abc()
{ System.Console.WriteLine(”1″); }
public virtual void pqr()
{ System.Console.WriteLine(”2″); }
public virtual void xyz()
{ System.Console.WriteLine(”3″); }
}
class xxx : yyy
{
public override void abc()
{ System.Console.WriteLine(”4″); }
public new void pqr()
{ System.Console.WriteLine(”5″); }
public void xyz()
{ System.Console.WriteLine(”6″); }
}

out and ref in c#

Wednesday, October 25th, 2006

out.cs

class zzz
{
public static void Main()
{
yyy a;
int i;
a=new yyy();
a.abc(out i);
System.Console.WriteLine(i);
}
}
class yyy {
public void abc( out int i) {
i=10;
}
}
//bcoz of out 10 else 100
//out makes available the changes outside class

ref.cs

class zzz
{
public static void Main()
{
yyy a;
int i=100;//have to initialize in ref
a=new yyy();
a.abc(ref i);
System.Console.WriteLine(i);
}
}
class yyy
{
public void abc( ref int i)
{
i=10;
}
}

Object data type in C#

Wednesday, September 27th, 2006

Object.cs

using System;
class Test
{
static void Main() {
string s = “Test”;
string t = string.Copy(s);
Console.WriteLine(s == t);
Console.WriteLine((object)s == (object)t);
}
}

ObjectDataType.cs

class Point
{
public int x, y;
public Point(int x, int y) {
this.x = x;
this.y = y;
}
}
class Test
{
static void Main() {
Point[] points = new Point[100];
for (int i = 0; i < 100; i++)
{
points[i] = new Point(i, i*i);
System.Console.WriteLine("{0}",points[i]);
}
}
}