C Sharp Programming Language

From Notes
Jump to navigation Jump to search

This page is meant to be a cheat-sheet for my experience with the C# Programming Language.

Hello, World!

using System;

namespace HelloWorld
{
    class HelloWorldProgram
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello, World!");
            Console.ReadLine();
        }
    }
}
using System;
 
namespace MyExampleNamespace
{
    public class Customer : IDisposable
    {
        private string _customerName;
        public string CustomerName 
        { 
            get 
            { 
                return _customerName; 
            }
            set
            {
                _customerName = value;
                _lastUpdated = DateTime.Now;
            }
        }
 
        private DateTime _lastUpdated;
 
        public DateTime LastUpdated
        {
            get
            {
                return _lastUpdated;
            }
            private set
            {
                _lastUpdated = value;
            }
        }
 
        public void UpdateCustomer(string newName)
        {
            if (!newName.Equals(CustomerName))
            {
                CustomerName = newName;
            }
        }
 
        public void Dispose()
        {
            //Do nothing
        }
    }
}


Includes

C#'s using keyword is similar to Java's import, but it operates one level above. That is, using a namespace exposes all declarations in that namespace to the global scope of the current source file.

For example, "using System;" in C# is equivalent to "import System.*;" in Java.


Conventions

Namespaces
upper CamelCased: namespace MyAwesomeNamespaceName
Classes
upper CamelCased: class MyAwesomeClass
Interfaces
Upper CamelCased, prefixed with "I": interface IMyAwesomeInterface
Functions
Upper CamelCased: void DoSomethingAwesome()
Event handlers use underscores to separate the bound object name and the event name: c_ThresholdReached.
Instance Variables
public and properties are Upper CamelCased: public int MyAwesomePublicMember
private and protected are prefixed with underscore and lower camelCased: private int _myAwesomePrivateMember
Local Variables and Function Parameters
lower camelCased: int myAwesomeVariable
UI Components
Hungarian naming convention based on widget type: Button btnMyAwesomeButton


Properties

Similar to @property in Objective-C in that it automatically defines getters and setters for the

interface IShape
{
    double X { get; set; }
    double Y { get; set; }
    void Draw();
}

class Square : IShape
{
    private double _mX, _mY;
    public void Draw() { ... }

    public double X 
    { 
        set { _mX = value; }
        get { return _mX; }  
    }

    public double Y 
    {
        set { _mY = value; }
        get { return _mY; }
    }
}


Events

Apparently C# overloads the += operator for event binding.

Dafuq.jpg
// or something like that...

class EventProvider
{
    public event Event;
}

class Application
{
    public static void Main(string[] args)
    {
        EventProvider ep = new EventProvider();
        ep.Event += ep_DoSomething;
    }

    public static void ep_DoSomething()
    {
        Console.PrintLine("Event callback!");
    }
}

What's more, as syntactically sweet this style of event handling is, it is now considered harmful