*
*
|
|
C# Sample Code
Zamples is proud to announce support for C# code examples, powered by Mono.
If you are logged in, you can create new discussion threads or reply to
an existing posting. After you click on a 'Try It!' button in a community posting, or after you press a 'New' button
to create a posting, edit your code, select the type of program (C#), and then 'Run It!' until it works.
When you click on the 'Save' button, the New Posting Wizard will appear. The wizard will prompt
you for information about your code example, and then save it to the database. Each posting contains
a code example, plus comments about the code. Next time you refresh this page your new posting will appear.
You can modify and/or delete your posting once it is saved.
Learn more about Zamples and
play with samples for other APIs.
We would love to hear your feedback.
Hello Zamples
First C# Sample Program
Purpose: First C# Sample Program
Description: here , we are imported namesapce System by using keyword 'using' so we can use directly all the classes and methods from the System namespace. |
using System; //NameSpace
class HelloCsharp
{
public static void Main()
{
Console.WriteLine("Hello CSharp World");
}
}
|
Hello Zamples
Purpose: The famous "Hello World" program, written in C# and brought to life by Zamples.
Description: Modify the message from "Hello Zamples!" to something else and rerun the example. |
public class Test {
public static void Main() {
System.Console.WriteLine("Hello Zamples!");
}
}
|
Sample reply
Purpose: This is what a reply looks like
Description: The code fragment is not shown for replies |
|
| to add a new posting or reply to an existing posting. |
|
Print Command Line Arguments
From the MSDN C# Tutorials.
Command Line Arguments Version 1
Purpose: This shows one way of detecting and displaying command line arguments.
Description: Modify the arguments and rerun the example. |
using System;
public class CommandLine {
public static void Main(string[] args) {
// The Length property is used to obtain the length of the array.
// Notice that Length is a read-only property:
Console.WriteLine("Number of command line parameters = {0}", args.Length);
for (int i = 0; i < args.Length; i++) {
Console.WriteLine("Arg[{0}] = [{1}]", i, args[i]);
}
}
}
|
|
| to add a new posting or reply to an existing posting. |
|
Sample output:
Number of command line parameters = 3
Arg[0] = [one]
Arg[1] = [two]
Arg[2] = [three]
Here is another program that does the same thing, but uses a foreach statement instead of a for statement:
Command Line Arguments Version 2
Purpose: This is another way of detecting and displaying command line arguments, using the foreach statement.
Description: Modify the arguments and rerun the example. |
using System;
public class CommandLine2 {
public static void Main(string[] args) {
Console.WriteLine("Number of command line parameters = {0}", args.Length);
foreach(string s in args) {
Console.WriteLine(s);
}
}
}
|
|
| to add a new posting or reply to an existing posting. |
|
Arrays
From the MSDN C# Tutorials.
Arrays
Purpose: Shows how to define arrays, assign values to them and print them out.
Description: Looks a lot like Java except for the syntax of multi-dimensional array definition. |
using System;
class DeclareArraysSample {
public static void Main() {
// Single-dimensional array
int[] numbers = new int[5];
// Multidimensional array
string[,] names = new string[5,4];
// Array-of-arrays (jagged array)
byte[][] scores = new byte[5][];
// Create the jagged array
for (int i = 0; i < scores.Length; i++) {
scores[i] = new byte[i+3];
}
// Print length of each row
for (int i = 0; i < scores.Length; i++) {
Console.WriteLine("Length of row {0} is {1}", i, scores[i].Length);
}
}
}
|
|
| to add a new posting or reply to an existing posting. |
|
Sample output:
Length of row 0 is 3
Length of row 1 is 4
Length of row 2 is 5
Length of row 3 is 6
Length of row 4 is 7
Simple Properties
From the MSDN C# Tutorials.
Simple Properties
Purpose: Shows how getters and setters work on properties
Description: Note the default value for each property (myName and myAge). |
using System;
class Person {
private string myName ="N/A";
private int myAge = 0;
// Declare a Name property of type string:
public string Name {
get { return myName; }
set { myName = value; }
}
// Declare an Age property of type int:
public int Age {
get { return myAge; }
set { myAge = value; }
}
public override string ToString() {
return "Name = " + Name + ", Age = " + Age;
}
public static void Main() {
Console.WriteLine("Simple Properties");
// Create a new Person object:
Person person = new Person();
// Print out the name and the age associated with the person:
Console.WriteLine("Person details - {0}", person);
// Set some values on the person object:
person.Name = "Joe";
person.Age = 99;
Console.WriteLine("Person details - {0}", person);
// Increment the Age property:
person.Age += 1;
Console.WriteLine("Person details - {0}", person);
}
}
|
|
| to add a new posting or reply to an existing posting. |
|
Sample output:
Simple Properties
Person details - Name = N/A, Age = 0
Person details - Name = Joe, Age = 99
Person details - Name = Joe, Age = 100
Versioning
From the MSDN C# Tutorials.
We corrected an error - the declaration for Meth3() was missing the override attribute.
It is common for sample code to have errors unless it is live, such as provided with Zamples.
Versioning
Purpose: This shows how to override a virtual method in a base class.
Description: Unlike Java, the overriding method must be declared with the 'override' attribute. |
using System;
class Person {
private string myName ="N/A";
private int myAge = 0;
// Declare a Name property of type string:
public string Name {
get { return myName; }
set { myName = value; }
}
// Declare an Age property of type int:
public int Age {
get { return myAge; }
set { myAge = value; }
}
public override string ToString() {
return "Name = " + Name + ", Age = " + Age;
}
public static void Main() {
Console.WriteLine("Simple Properties");
// Create a new Person object:
Person person = new Person();
// Print out the name and the age associated with the person:
Console.WriteLine("Person details - {0}", person);
// Set some values on the person object:
person.Name = "Joe";
person.Age = 99;
Console.WriteLine("Person details - {0}", person);
// Increment the Age property:
person.Age += 1;
Console.WriteLine("Person details - {0}", person);
}
}
|
|
| to add a new posting or reply to an existing posting. |
|
Sample output:
MyDerived-Meth1
MyBase-Meth2
MyDerived-Meth3
Collections
From the MSDN C# Tutorials.
Collections
Purpose: This example shows how to parse tokens and use an Enumerator to walk through each token.
Description: The inner class does all the work. |
17 public IEnumerator GetEnumerator() { return new TokenEnumerator(this); }
18
19 // Inner class implements IEnumerator interface:
20 private class TokenEnumerator : IEnumerator {
21 private int position = -1;
22 private Tokens t;
23
24 public TokenEnumerator(Tokens t) { this.t = t; }
25
26 // Declare the MoveNext method required by IEnumerator:
27 public bool MoveNext() {
28 if (position < t.elements.Length - 1) {
29 position++;
30 return true;
31 } else {
32 return false;
33 }
34 }
35
36 // Declare the Reset method required by IEnumerator:
37 public void Reset() { position = -1; }
38
39 // Declare the Current property required by IEnumerator:
40 public object Current {
41 get { return t.elements[position]; }
42 }
43 }
44
45 // Test Tokens, TokenEnumerator
46 static void Main() {
47 // Testing Tokens by breaking the string into tokens:
48 Tokens f = new Tokens("This is a well-done program.", new char[] {' ','-'});
49 foreach (string item in f) {
50 Console.WriteLine(item);
51 }
52 }
|
|
| to add a new posting or reply to an existing posting. |
|
Sample output:
This
is
a
well
done
program.
Add New Code Samples Here
If you have code samples that don't fit into any of the topics above which you would like to contribute,
please put them here.
We'll sort them out into their own sections as appropriate later.
|
| to add a new posting or reply to an existing posting. |
|
|