PLDB
Languages Features Calendar CSV Lists Blog About Donate Add Language
GitHub icon

C#

< >

C# is a programming language created in 2000 by Anders Hejlsberg.

#12on PLDB 23Years Old 217.3kUsers
3Books 0Papers 2mRepos

Try now: Riju ยท Replit

C# (pronounced as see sharp) is a multi-paradigm programming language encompassing strong typing, imperative, declarative, functional, generic, object-oriented (class-based), and component-oriented programming disciplines. It was developed by Microsoft within its .NET initiative and later approved as a standard by Ecma (ECMA-334) and ISO (ISO/IEC 23270:2006). C# is one of the programming languages designed for the Common Language Infrastructure. Read more on Wikipedia...


Example from Compiler Explorer:
class Program { static int Square(int num) => num * num; }
Example from Riju:
class main { static void Main(string[] args) { System.Console.WriteLine("Hello, world!"); } }
Example from hello-world:
System.Console.WriteLine("Hello World");
Example from Linguist:
๏ปฟusing System; namespace MongoDB.Serialization.Descriptors { internal class BsonPropertyValue { public bool IsDictionary { get; private set; } public Type Type { get; private set; } public object Value { get; private set; } public BsonPropertyValue(Type type, object value, bool isDictionary) { Type = type; Value = value; IsDictionary = isDictionary; } } }
Example from Wikipedia:
using System.Windows.Forms; class Program { static void Main(string[] args) { MessageBox.Show("Hello, World!"); System.Console.WriteLine("Is almost the same argument!"); } }

Keywords in C#

abstract add alias as ascending async await base bool break byte case catch char checked class const continue decimal default delegate descending do double dynamic else enum event explicit extern false finally fixed float for foreach from get global goto group if implicit in int interface internal into is join let lock long namespace new null object operator orderby out override params partial private protected public readonly ref remove return sbyte sealed select set short sizeof stackalloc static string struct switch this throw true try typeof uint ulong unchecked unsafe ushort using value var virtual void volatile where while yield

Language features

Feature Supported Token Example
Conditionals โœ“
Access Modifiers โœ“
Switch Statements โœ“
Exceptions โœ“
Constants โœ“
Classes โœ“
While Loops โœ“
Case Sensitivity โœ“
Function Composition โœ“
// Call example:
//   var c = Compose(f, g);
//
//   Func g = _ =>  ...
//   Func f = _ => ...

Func Compose(Func f, Func g) => _ => f(g(_));
Default Parameters Pattern โœ“
public void ExampleMethod(string optionalstr = "default string") {}
Line Comments โœ“ //
// A comment
Dispose Blocks Pattern โœ“
using (Resource resource = GetResource())
{
   // Perform actions with the resource.
   ...
}
Module Pattern โœ“
// In C#, namespaces are the semi-equivalent of Java's packages.
namespace com.test
{
   class Test {}
}
Operator Overloading โœ“
Namespaces โœ“
namespace MyNamespace
{
    class MyClass
    {
        public void MyMethod()
        {
            System.Console.WriteLine("Creating my namespace");
    }
  }
}
File Imports โœ“
using static System.Console;
using static System.Math;
class Program
{
    static void Main()
    {
        WriteLine(Sqrt(3*3 + 4*4));
    }
}
Type Casting โœ“
Animal animal = new Cat();

Bulldog b = (Bulldog) animal;   // if (animal is Bulldog), stat.type(animal) is Bulldog, else an exception
b = animal as Bulldog;          // if (animal is Bulldog), b = (Bulldog) animal, else b = null

animal = null;
b = animal as Bulldog;          // b == null
Directives โœ“
#define MAX_CLIENTS 200
int array[MAX_CLIENTS];

#if PRODUCTION
//code
#elif DEVELOPMENT
//code
#else
//code
#endif
Generators โœ“
// Method that takes an iterable input (possibly an array)
// and returns all even numbers.
public static IEnumerable GetEven(IEnumerable numbers) {
    foreach (int i in numbers) {
        if ((i % 2) == 0) {
            yield return i;
        }
    }
}
Constructors โœ“
public class MyClass
{
    private int a;
    private string b;

    // Constructor
    public MyClass() : this(42, "string")
    {
    }

    // Overloading a constructor
    public MyClass(int a, string b)
    {
        this.a = a;
        this.b = b;
    }
}
// Code somewhere
// Instantiating an object with the constructor above
MyClass c = new MyClass(42, "string");
Generics โœ“
// Declare the generic class.
public class GenericList
{
    public void Add(T input) { }
}
class TestGenericList
{
    private class ExampleClass { }
    static void Main()
    {
        // Declare a list of type int.
        GenericList list1 = new GenericList();
        list1.Add(1);

        // Declare a list of type string.
        GenericList list2 = new GenericList();
        list2.Add("");

        // Declare a list of type ExampleClass.
        GenericList list3 = new GenericList();
        list3.Add(new ExampleClass());
    }
}
Pointers โœ“
// Pointers supported only under certain conditions.
// Get 16 bytes of memory from the process's unmanaged memory
IntPtr pointer = System.Runtime.InteropServices.Marshal.AllocHGlobal(16);
Async Await โœ“
public async Task FindPageSize(Uri uri) 
{
    byte[] data = await new WebClient().DownloadDataTaskAsync(uri);
    return data.Length;
}
MultiLine Comments โœ“ /* */
/* A comment
*/
Print() Debugging โœ“ Console.WriteLine
Console.WriteLine("This is C#");
Assignment โœ“
int pldb = 80766866;
Integers โœ“
int pldb = 80766866;
Booleans โœ“ true false
Strings โœ“ "
"hello world"
Comments โœ“
// This is a comment
Semantic Indentation X

Books about C# from ISBNdb

title authors year publisher
C# Programming: From Problem Analysis to Program Design Doyle, Barbara 2015 Cengage Learning
Starting out with Visual C# Gaddis, Tony 2016 Pearson
Microsoft Visual C# 2013 Step by Step Sharp, John 2013 Microsoft Press
matlab.html ยท csharp.html ยท fortran.html

View source

PLDB - Build the next great programming language ยท v2023 ยท Day 153 ยท Docs ยท Build ยท Acknowledgements ยท Traffic Today ยท Traffic Trends ยท Mirrors ยท GitHub ยท feedback@pldb.com