using System; public class Welcome3 { public static void Main( string[] args ) { Console.WriteLine( "Welcome to C# Programming!" ); } }
Development Class
Printing one line of text with multiple statements.
using System; public class Welcome2 { public static void Main(string[] args) { Console.Write("Welcome to "); Console.WriteLine("C# Programming!"); } }
WriteLine and Write
using System; class ConsoleAdder { public static void Main() { int a = 15; int b = 744; int c = a + b; Console.Write("The sum of "); Console.Write(a); Console.Write(" and "); Console.Write(b); Console.Write(" equals "); Console.WriteLine(c); Console.WriteLine("The sum of " + a + " and " + b + " equals " + c); Console.WriteLine("The sum of {0} and {1} equals {2}", a, b, c); } }
Read a line of string and check its length
using System;
class MainEntryPoint {
static void Main(string[] args) {
Console.WriteLine(“Type in a string”);
string input;
input = Console.ReadLine();
if (input == “”) {
Console.WriteLine(“You typed in an empty string”);
} else if (input.Length < 5) {
Console.WriteLine("The string had less than 5 characters");
} else if (input.Length < 10) {
Console.WriteLine("The string had at least 5 but less than 10 characters");
}
Console.WriteLine("The string was " + input);
}
}
[/csharp]
Echo some stats
using System; using System.Collections.Generic; using System.Text; class Program { static void Main(string[] args) { Console.Write("Enter your name: "); string s; s = Console.ReadLine(); Console.WriteLine("Hello, {0} ", s); Console.Write("Enter your age: "); s = Console.ReadLine(); Console.WriteLine("You are {0} years old", s); } }
input a series of numbers separated by commas, parse them into integers and output the sum
using System;
class Class1 {
public static void Main(string[] args) {
Console.WriteLine(“Input a series of numbers separated by commas:”);
string input = Console.ReadLine();
char[] cDividers = { ',', ' ' };
string[] segments = input.Split(cDividers);
int nSum = 0;
foreach (string s in segments) {
if (s.Length > 0) {
if (IsAllDigits(s)) {
int num = Int32.Parse(s);
Console.WriteLine(“Next number = {0}”, num);
nSum += num;
}
}
}
Console.WriteLine(“Sum = {0}”, nSum);
}
public static bool IsAllDigits(string sRaw) {
string s = sRaw.Trim();
if (s.Length == 0) {
return false;
}
for (int index = 0; index < s.Length; index++) { if (Char.IsDigit(s[index]) == false) { return false; } } return true; } } [/csharp]
constructs sentences by concatenating user input until the user enters one of the termination characters
using System; public class MainClass { public static void Main(string[] args) { string sSentence = ""; for (; ; ) { Console.WriteLine("Enter a string"); string sLine = Console.ReadLine(); if (IsTerminateString(sLine)) { break; } sSentence = String.Concat(sSentence, sLine); Console.WriteLine(" You've entered: {0}", sSentence); } } public static bool IsTerminateString(string source) { string[] sTerms = {"EXIT","exit","QUIT","quit"}; foreach (string sTerm in sTerms) { if (String.Compare(source, sTerm) == 0) { return true; } } return false; } }