C#-1 Source Code Files
=== Intro.Intro.Program.cs ===================================
using System;
namespace Datatypes
{
///
/// IntDatatype Example: Illustrate some methods and
/// properties of the int datatype.
///
///
/// All datatypes (reference or value type) are derived
/// from the object datatype.
///
public class Program
{
public static void Main()
{
// Create and initialize Int32 variable.
int b = 95847;
// Test inherited object methods.
Console.WriteLine(b.ToString() + " " + b.GetHashCode());
// Display value (invoke ToString method implicitly
// and get full CLR name of int datatype.
Console.WriteLine(b + " " + b.GetType().FullName + "\n");
// Get minimum and maximum values of int datatype.
// Note that b.MinValue and b.MaxValue are not legal
// because MinValue and MaxValue are static methods.
Console.WriteLine(int.MinValue + " " + int.MaxValue + "\n");
// Use int parse method to convert string to int.
// A FormatException will be thrown is the parse is
// not successful.
int c = int.Parse("123");
Console.WriteLine(c);
// Use the int TryParse method if you are not sure that
// the string to parse can be converted to an int.
int s, t;
bool flagS = int.TryParse("123", out s);
bool flagT = int.TryParse("x45", out t);
Console.WriteLine(s + " " + flagS + " " + t + " " + flagT);
Console.WriteLine();
// Print methods of int datatype.
foreach (object x in b.GetType().GetMethods())
Console.WriteLine(x);
Console.WriteLine("\n");
// Hold console window for user to read.
Console.ReadLine();
}
}
}
// Console Output:
95847 95847
95847 System.Int32
-2147483648 2147483647
123
123 True 0 False
Int32 CompareTo(Int32)
Boolean Equals(System.Object)
Boolean Equals(Int32)
Int32 GetHashCode()
System.String ToString()
System.String ToString(System.IFormatProvider)
System.String ToString(System.String, System.IFormatProvider)
System.TypeCode GetTypeCode()
Int32 CompareTo(System.Object)
System.String ToString(System.String)
Int32 Parse(System.String)
Int32 Parse(System.String, System.Globalization.NumberStyles)
Int32 Parse(System.String, System.IFormatProvider)
Int32 Parse(System.String, System.Globalization.NumberStyles, System.IFormatProv
ider)
Boolean TryParse(System.String, Int32 ByRef)
Boolean TryParse(System.String, System.Globalization.NumberStyles, System.IForma
tProvider, Int32 ByRef)
System.Type GetType()
=== FormatStrings.FormatStrings.Program.cs ===================
using System;
namespace FormatStrings
{
public class Program
{
///
/// FormatStrings Example: Use format strings in these three
/// methods: Console.WriteLine, Object.ToString and Format.String.
///
public static void Main()
{
// Declare variables;
string a = "apple";
int x = 985;
double y = 154392.3432;
string s;
// WriteLine with (string) signature.
Console.WriteLine(a);
// WriteLine with (int) signature.
Console.WriteLine(x);
//WriteLine with Double signature.
Console.WriteLine(y);
// WriteLine with () signature.
Console.WriteLine();
// WriteLine using a format string with arguments.
Console.WriteLine("Using WriteLine with format strings:");
Console.WriteLine("{0}; {1}; {2}", a, x, y);
// Use a format string with the Console.WriteLine method.
Console.WriteLine("!{0,4}! !{1,5:#0.00}!", x, y);
Console.WriteLine();
// Use a format string with the Object.ToString method.
Console.WriteLine(y.ToString("#,###,##0.000"));
Console.WriteLine();
// Use a format string with the String.Format method.
s = String.Format("{0}; {1}; {2}", a, x, y);
Console.WriteLine(s);
Console.ReadLine();
}
}
}
// Console Output:
apple
985
154392.3432
Using WriteLine with format strings:
apple; 985; 154392.3432
! 985! !154392.34!
154,392.343
apple; 985; 154392.3432
=== Bitwise.Bitwise.Program.cs ===============================
using System;
namespace Bitwise
{
///
/// Bitwise Example: Compute the bitwise and (&), or (|),
/// and exclusive or (^) of two input bitstrings.
/// The input bitstrings are 00000101 and 00001100.
/// The unary operator ~ returns the bitwise complement of
/// an integer variable
///
///
/// Definitions: 0 is defined as False, 1 is defined as True.
/// 0 And 0 = 0; 1 And 0 = 0; 0 And 1 = 0; 1 And 1 = 1;
/// 0 Or 0 = 0; 1 Or 0 = 1; 0 Or 1 = 1; 1 Or 1 = 1;
/// 0 XOr 0 = 0; 1 XOr 0 = 1; 0 Xor 1 = 1; 1 Xor 1 = 0.
///
public class Program
{
public static void Main()
{
// 5 is 00000101 in binary.
byte x = 5;
// 12 is 00001100 in binary.
byte y = 12;
// Output results of bitwise operations.
Console.WriteLine("Bitwise And: " + (x & y));
Console.WriteLine("Bitwise Or : " + (x | y));
Console.WriteLine("Bitwise Xor: " + (x ^ y));
Console.WriteLine();
Console.WriteLine("Bitwise Complement: " + ~x);
Console.ReadLine();
}
}
}
// Console Output:
Bitwise And: 4
Bitwise Or : 13
Bitwise Xor: 9
Bitwise Complement: -6
=== ToBase64.ToBase64.Program.cs =============================
using System;
namespace ToBase64
{
///
/// ToBase64 Example: Convert a Byte array to a base-64 string.
///
///
/// The 64 base-64 digits are:
/// ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyx0123456789+/
/// Use the symbol = to show padding at the end.
/// The 16 hex digits are:
/// 0123456789ABCDEF
///
public class Program
{
public static void Main(string[] args)
{
// Create an array of unsigned byte containing
// the hex values 2A 9E 7D.
byte[] a = {0x2A, 0x9E, 0x7D};
string s;
// Base-64 uses one ascii character for each six bits of data.
// Hex uses one ascii character digit for each six bits for
// each eight bits of data represented in hex.
s = Convert.ToBase64String(a);
Console.WriteLine(s);
Console.ReadLine();
}
}
}
// Console Output:
Kp59
=== CreateResx.CreateResx.Program.cs =========================
using System;
using System.Drawing;
using System.Resources;
namespace ResX
{
public class Program
{
public static void Main()
{
Image img = Image.FromFile("../../happy.jpg");
ResXResourceWriter rsxw =
new ResXResourceWriter("happy.resx");
rsxw.AddResource("happy.jpg", img);
rsxw.Close();
}
}
}
// Output File happy.resx
/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAgGBgcG
BQgHBwcJCQgKDBQNDAsLDBkSEw8UHRofHh0aHBwg
JC4nICIsIxwcKDcpLDAxNDQ0Hyc5PTgyPC4zNDL/
2wBDAQkJCQwLDBgNDRgyIRwhMjIyMjIyMjIyMjIy
MjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIy
MjIyMjIyMjL/wAARCABjAGMDASIAAhEBAxEB/8QA
HwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL
/8QAtRAAAgEDAwIEAwUFBAQAAAF9AQIDAAQRBRIh
MUEGE1FhByJxFDKBkaEII0KxwRVS0fAkM2JyggkK
FhcYGRolJicoKSo0NTY3ODk6Q0RFRkdISUpTVFVW
... 36 lines of image omitted.
=== WeekDaysEnum.WeekDaysEnum.Program.cs =====================
using System;
namespace WeekDaysEnum
{
///
/// WeekDays Example: Show how to create and use
/// the user defined enumeration WeekDays.
///
public class Program
{
public static void Main()
{
WeekDays day = WeekDays.WED;
if (day == WeekDays.SUN || day == WeekDays.SAT)
Console.WriteLine("Is a weekend day.");
else
Console.WriteLine("Is not a weekend day.");
int code = (int) day;
Console.WriteLine(code);
Console.ReadLine();
}
}
}
// Console Output:
Is not a weekend day.
3
--- WeekDaysEnum.WeekDaysEnum.WeekDays.cs --------------------
using System;
namespace WeekDaysEnum
{
///
/// WeekDaysEnum Example: Create the user defined enumeration
/// WeekDays.
///
///
/// When to integer values are specifically assigned to
/// the enumeration values, the default values are assigned:
/// 0, 1, 2, 3, 4, 5, 6.
///
public enum WeekDays
{
SUN,
MON,
TUE,
WED,
THU,
FRI,
SAT
}
}
=== Array.Array.Program.cs ===================================
using System;
namespace ArrayExample
{
///
/// Array Example: Show how to create an array of random
/// integers. Also show how to use the Sort and
/// BinarySearch methods.
///
///
/// Use a Random object to generate random integers. The
/// method call r.Next(b, e+1); returns a random integer in
/// the range [b,e]. (b and e are the beginning and end of
/// the range, respectively.
///
public class Program
{
public static void Main()
{
int[] a = new int[15];
Random r = new Random();
// Create an array of 20 random
// integers in the range [1, 1000].
for (int i = 0; i <= a.GetUpperBound(0); i++)
a[i] = r.Next(1, 1001);
foreach (int x in a)
Console.Write("{0,3} ",x);
Console.WriteLine("\n");
// Sort array.
Array.Sort(a);
// Print sorted array.
foreach (int x in a)
Console.Write("{0,3} ", x);
Console.WriteLine("\n");
// Create and print array of string to search
// with BinarySearch method.
string[] b = {"apple", "apricot", "cherry",
"grape", "peach", "pear" };
foreach (string s in b)
Console.WriteLine(s + " ");
Console.WriteLine("\n");
// Use binary search to find index of 500.
// A negative index is returned if the item is
// not found.
Console.WriteLine("Index of grape is " +
Array.BinarySearch(b, "grape"));
Console.ReadLine();
}
}
}
// Console Output:
229 132 968 58 100 449 897 449 67 236 64 143 826 934 606
58 64 67 100 132 143 229 236 449 449 606 826 897 934 968
apple
apricot
cherry
grape
peach
pear
Index of grape is 3
=== Structure.Structure.Program.cs ===========================
using System;
namespace Structure
{
///
/// Structure Example: Define and use the user defined
/// structure Person.
///
///
/// Structures are value types, not reference types.
/// They are allocated on the stack, not on the heap.
///
public class Program
{
public static void Main()
{
Person x;
x.name = "Sally";
x.id = 3453;
x.gender = 'F';
x.age = 11;
Console.WriteLine("{0} {1} {2} {3}",
x.name, x.id, x.gender, x.age);
Console.WriteLine();
x.print();
Console.ReadLine();
}
}
}
// Console Output:
Sally 3453 F 11
Name: Sally
Gender: F
Age: 11
--- Structure.Structure.Person.cs ----------------------------
using System;
namespace Structure
{
///
/// Structure Example: Define and use the user defined
/// reference type Person.
///
public struct Person
{
// Define public instance variables.
public string name;
public int id;
public char gender;
public short age;
// Define public print method.
public void print()
{
Console.WriteLine("Name: " + name);
Console.WriteLine("Gender: " + gender);
Console.WriteLine("Age: " + age);
}
}
}
=== RepeatSentenct.RepeatSentence.Program.cs =================
using System;
namespace RepeatSentence
{
///
/// RepeatSentence Example: Call a subroutine to print a
/// sentence a specified number of times.
///
public class Program
{
public static void Main()
{
// Define input arguments.
string s = "I will not talk in class.";
int n = 10;
// Invoke user defined method.
RepeatSentence(s, n);
Console.ReadLine();
}
// Define user defined method with parameters
// sentence timesToRepeat.
public static void RepeatSentence(string sentence, int timesToRepeat)
{
for(int i = 1; i <= timesToRepeat; i++)
Console.WriteLine("{0,2}. {1}", i, sentence);
}
}
}
// Console Output:
1. I will not talk in class.
2. I will not talk in class.
3. I will not talk in class.
4. I will not talk in class.
5. I will not talk in class.
6. I will not talk in class.
7. I will not talk in class.
8. I will not talk in class.
9. I will not talk in class.
10. I will not talk in class.
=== Square.Square.Program.cs =================================
using System;
namespace Square
{
public class Program
{
///
/// Define and invoke a Square function, which inputs
/// an int and returns the square of an int.
///
///
/// The Square function is inspired by the Pascal
/// function sqr.
///
public static void Main()
{
int x = 5;
int y = square(x);
Console.WriteLine(y);
Console.ReadLine();
}
public static int square(int a)
{
int b;
b = a * a;
return b;
}
}
}
// Console Output:
25
=== MathMethods.MathMethods.Program.cs =======================
using System;
namespace MathMethods
{
public class Program
{
///
/// The System.Math class contains some useful
/// (and some not so useful) math functions and constants.
///
public static void Main()
{
Console.WriteLine(Math.Sqrt(2));
Console.WriteLine(Math.Max(34, 65) + " " + Math.Min(34, 65));
Console.WriteLine(Math.Abs(-34));
Console.WriteLine(Math.Floor(-2.432) + " " + Math.Ceiling(-2.432));
Console.WriteLine(Math.PI + " " + Math.Cos(Math.PI) + " " +
Math.Sin(Math.PI) + " " + Math.Tan(Math.PI));
Console.ReadLine();
}
}
}
// Console Methods:
1.4142135623731
65 34
34
-3 -2
3.14159265358979 -1 1.22460635382238E-16 -1.22460635382238E-16
=== CommandLineArgs.CommandLineArgs.Program.cs ===============
using System;
namespace CommandLineArgs
{
///
/// CommandLineArgs Example: Use command line arguments
/// to input data entered at the at the command line
/// after the executable name.
///
///
/// Command line args can also be set in the Debug
/// Window of the project properties.
///
public class Program
{
public static void Main(string[] args)
{
// Display command line args.
Console.WriteLine("Command Line args are " +
args[0] + " and " + args[1] + ".");
// Compute sum.
int x = int.Parse(args[0]);
int y = int.Parse(args[1]);
Console.WriteLine("Sum = " + (x + y));
Console.ReadLine();
}
}
}
// Command Window Display:
C:\TestCommandLineArgs>CommandLineArgs 5 7
Command Line args are 5 and 7.
Sum = 12
==============================================================