C# – Conversions

Conversions

A conversion always creates a new value from an existing one. Conversions can be either implicit or explicit.
Implicit conversions happen automatically, and explicit conversions require a cast.
For Implicit the compiler guarantee that it will succeed and no information is lost whereas explicit it is the opposite.

C# – Instance and Static

Instance and Static

Instance members

  • Data and function members that operate on the instance eg ints ToString() is a instance member of Integer class
  • By default members in a type/class are instance members

Static Members

  • Don’t operate on the instance of the type
  • Operate on the type itself

Example

using System;

namespace Rextester
{
    public class Program
    {
        public static void Main(string[] args)
        {
            Panda p1 = new Panda ("Pan Dee");
            Panda p2 = new Panda ("Pan Dah");
            
            Console.WriteLine(p1.Name);
            Console.WriteLine(p2.Name);
            Console.WriteLine(Panda.Population);
            

        }
    }
    
    public class Panda
    {
        //instance field
        public string Name;
        
        //static field
        //set to all Panda instances
        public static int Population;
        
        //Constructor
        public Panda (string n)
        {
            //Assign the instance field
            Name = n;
            
            //Increment the static Population field
            Population = Population + 1;
        }
    }
}

Static Class
– all its members must be static

C# – Constructors and Instantiation

Constructors and Instantiation

There are basically two types: Predefined types and Custom Type
Predefined types
It is the blueprint. It can be instantiated simply by using a literal such as 12 or “Hello world”. They are the types which are supported by the compiler. Examples such as int – represent a set of integers and perform arithmetic functions.

Custom Type
New operator creates instances of custom type. After instantiation the constructor is called to perform initialization.

Constructor
Defined like method but method name is the name of the class.

public class UnitConverter
{
	public UnitConverter (int unitRatio) { ratio = unitRatio;}
}

C# – Overview

Summary

  • Is an object oriented paradigm consists of encapsulation, inheritance and polymorphism
  • Unified Type system: all types share a common base type.
  • Classes and Interfaces: comprised of Classes and Interfaces
  • Properties, methods and events: properties – function members that encapsulate a piece of an objects state eg color, label text.
  • Functons can be treated as values: using delegates, functions can be passed as values
  • Supports pattern of purity: lambda expression, query expression and auto-properties for writing immutable types.

Type Safety

  • Supports static typing : enforces type safety at compile time rather than run time
  • Strongly typed language

Memory Management

  • Performs automatic memory management during run time
  • CLR has a garbage collector for reclaiming memory not being used

Platform Support

  • Allows cross platform support for Mac OS, iOS and Android.
  • ASP.NET 5 is the new framework which run either .NET Framework or .NET Core

The CLR and .NET Framework

  • Consists of CLR and set of libraries
  • CLR is the runtime for executign managed code
    Managed Code
    Packaged into assembly as .exe or .dll along metadata and type information
    Represented in intermediate language which laters gets converted into machine language.