C#
- Appeared in:
- 2002
- Influenced by:
- Influenced:
- Paradigm:
- Typing discipline:
- Dialects:
- Versions and implementations (Collapse all | Expand all):
C# is a modern general-purpose programming language designed for CLI (Common Language Infrastructure) specification.
Language development started in 1999 under the name Cool (“C-like Object Oriented language”). It was renamed to “C#” later to avoid trademark conflict and to emphasis both its origin from C++ and its superiority compared to it.
C# 1.0 was released in 2002 by Microsoft.
The official standards of the language are ECMA-334 and ISO/IEC 23270:2006. Microsoft Visual C# is C# reference implementation, though there exist several other implementations.
C# was designed as a simple modern language similar to C++ which should implement a number of features which improve safety and robustness of code as well as development efficiency and code portability.
C# is still a relatively young language, but it gains popularity fast.
Examples:
Hello, World!:
Example for versions Microsoft Visual C# 2008, gmcs 2.0.1using System;
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello, World!");
}
}
Factorial:
Example for versions Microsoft Visual C# 2008, gmcs 2.0.1This example uses recursive factorial definition.
using System;
class Program
{
static long Factorial(int n)
{
if (n == 0)
return 1;
else
return n * Factorial(n - 1);
}
static void Main(string[] args)
{
for (int i = 0; i < 17; i++)
Console.WriteLine("{0}! = {1}",i,Factorial(i));
}
}
Fibonacci numbers:
Example for versions Microsoft Visual C# 2008, gmcs 2.0.1This example uses recursive definition of Fibonacci numbers.
using System;
class Program
{
static long Fibonacci(int n)
{
if (n < 3)
return 1;
else
return Fibonacci(n - 1) + Fibonacci(n - 2);
}
static void Main(string[] args)
{
for (int i = 1; i < 17; i++)
Console.Write("{0}, ", Fibonacci(i));
Console.WriteLine("...");
}
}
Quadratic equation:
Example for versions Microsoft Visual C# 2008, gmcs 2.0.1using System;
class Program
{
static void Main(string[] args)
{
int A, B, C, D;
try
{ Console.Write("A = ");
A = Convert.ToInt32(Console.ReadLine());
Console.Write("B = ");
B = Convert.ToInt32(Console.ReadLine());
Console.Write("C = ");
C = Convert.ToInt32(Console.ReadLine());
}
catch
{ Console.WriteLine("Invalid input");
return;
}
if (A == 0)
{ Console.WriteLine("Not a quadratic equation.");
return;
}
D = B * B - 4 * A * C;
if (D == 0)
Console.WriteLine("x = {0}", -B / 2.0 / A);
else if (D > 0)
Console.WriteLine("x1 = {0}\nx2 = {1}", (-B + Math.Sqrt(D)) / 2 / A, (-B - Math.Sqrt(D)) / 2 / A);
else
Console.WriteLine("x1 = ({0},{1})\nx2 = ({0},-{1})", -B/2.0/A, Math.Sqrt(-D)/2/A);
}
}
CamelCase:
Example for versions gmcs 2.0.1First line of Main
method reads the input string from console and converts it to lowercase. Second line replaces all sequences of 1 or more non-alpha characters with spaces. Two next lines get object of class TextInfo and use it to convert the string to title case (all words start with capital letters). Finally, spaces are removed from the resulting string, and the result is written to console.
using System;
using System.Globalization;
using System.Text.RegularExpressions;
public class Program
{ public static void Main(string[] args)
{ string text = Console.ReadLine().ToLower();
text = Regex.Replace(text,"([^a-z]+)"," ");
TextInfo ti = new CultureInfo("en-US",false).TextInfo;
text = ti.ToTitleCase(text);
text = text.Replace(" ","");
Console.WriteLine(text);
}
}
CamelCase:
Example for versions gmcs 2.0.1This example uses only regular expressions. First pass replaces all maximum sequences of letters with result of applying CapitalizePart to them (i.e., makes first character uppercase). Second pass replaces all non-letters with empty string.
using System;
using System.Text.RegularExpressions;
class Program
{ static string CapitalizePart(Match m)
{ string x = m.ToString();
return char.ToUpper(x[0]) + x.Substring(1, x.Length-1);
}
static void Main()
{ string text = Console.ReadLine().ToLower();
string cc = Regex.Replace(text, "([a-z]+)", new MatchEvaluator(Program.CapitalizePart));
cc = Regex.Replace(cc, "[^a-zA-Z]+", string.Empty);
Console.WriteLine(cc);
}
}
Comments
]]>blog comments powered by Disqus
]]>