Friday, June 5, 2026 Sign InRegister FREE My Account Help
FDN Portal
FDN » .NET Framework » C# Language Primer

C# Language Primer

C# Language Primer

C# (pronounced "C sharp") is the flagship language of the .NET Framework. It combines the power of C++ with the productivity of Visual Basic.

Hello World

using System;

class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine("Hello, Flamenet Developer Network!");
    }
}

Classes and Objects

public class Employee
{
    // Fields
    private string _firstName;
    private string _lastName;

    // Properties
    public string FirstName
    {
        get { return _firstName; }
        set { _firstName = value; }
    }

    public string LastName
    {
        get { return _lastName; }
        set { _lastName = value; }
    }

    public string FullName
    {
        get { return _firstName + " " + _lastName; }
    }

    // Constructor
    public Employee(string firstName, string lastName)
    {
        _firstName = firstName;
        _lastName = lastName;
    }

    // Method
    public override string ToString()
    {
        return FullName;
    }
}

// Usage
Employee emp = new Employee("John", "Smith");
Console.WriteLine(emp.FullName);  // "John Smith"

Data Types

C# Type.NET TypeDescription
intSystem.Int3232-bit integer
longSystem.Int6464-bit integer
floatSystem.Single32-bit floating point
doubleSystem.Double64-bit floating point
decimalSystem.Decimal128-bit precise (financial)
boolSystem.Booleantrue or false
stringSystem.StringUnicode string (immutable)
objectSystem.ObjectBase type of all types

Control Flow

// if-else
if (age >= 18)
    Console.WriteLine("Adult");
else
    Console.WriteLine("Minor");

// switch
switch (role)
{
    case "admin":
        Console.WriteLine("Administrator");
        break;
    case "mod":
        Console.WriteLine("Moderator");
        break;
    default:
        Console.WriteLine("Member");
        break;
}

// for loop
for (int i = 0; i < 10; i++)
    Console.WriteLine(i);

// foreach
string[] names = { "Alice", "Bob", "Charlie" };
foreach (string name in names)
    Console.WriteLine(name);

// while
while (reader.Read())
    Console.WriteLine(reader["Name"]);

Exception Handling

try
{
    int result = 10 / 0;
}
catch (DivideByZeroException ex)
{
    Console.WriteLine("Error: " + ex.Message);
}
finally
{
    Console.WriteLine("Cleanup code runs always.");
}
« Previous ADO.NET Data Access
Next » Migrating from ASP to ASP.NET
More in .NET Framework
« Back to .NET Framework « Back to FDN
FlameNet Weekly: the best of the forum, freshest listings, top Q&A — delivered every Sunday.
13 members · 0 new today · 0 online now · 767 posts in last 24h