This project is read-only.

What's new

Dec 15, 2014 at 3:49 PM
Edited Dec 15, 2014 at 4:23 PM
I have added class List and Dictionary to CoreLib but have not updated the code yet, if anyone need it I will update it

Test example:
using System;
using System.Collections.Generic;

class X {
    public static int Main (string [] args)
    {
        var list = new List<int>();
        list.Add(1);
        list.Add(2);
        list.Add(3);
        list.Add(4);

        foreach (var item in list)
        {
            Console.WriteLine(item);
        }

        Console.WriteLine(list.Contains(2) ? "yes" : "no");

        return 0;
    }
}
1
2
3
4
yes
using System;
using System.Collections.Generic;

class X {
    public static int Main (string [] args)
    {
        var dict = new Dictionary<int, string>();
        dict[1] = "string 1";
        dict[2] = "string 2";
        dict[3] = "string 3";
        dict[4] = "string 4";

        foreach (var item in dict.Keys)
        {
            Console.WriteLine(item);
        }

        foreach (var item in dict.Values)
        {
            Console.WriteLine(item);
        }

        Console.WriteLine(dict[3]);

        return 0;
    }
}
1
2
3
4
string 1
string 2
string 3
string 4
string 3
Dec 22, 2014 at 1:54 PM
in version 22122014 I have added Multicast Delegate support.

Here is the test for it:
using System;

public delegate long MyDelegate(int i);

public class Y
{
    public event MyDelegate TestEvent;

    public int Test ()
    {
        TestEvent += new MyDelegate (callback1);
        TestEvent += new MyDelegate (callback2);
        if (TestEvent (2) != 4)
            return 3;

        return 0;
    }

    private static long callback1 (int b)
    {
        return b;
    }

    private static long callback2 (int b)
    {
        return b * b;
    }
}

public class Z
{
    public static int Main ()
    {
        var z = new Y ();

        int result = z.Test ();

        if (result != 0)
            return result;

        return 0;
    }
}
Dec 24, 2014 at 8:24 PM
Edited Dec 24, 2014 at 8:26 PM
I have added FileStream (in next update it will be available)
using System;
using System.IO;
using System.Text;

class X
{
    public static int Main()
    {
        using (FileStream fs = new FileStream("1.txt", FileMode.Open))
        {
            byte[] b = new byte[1024];
            while (fs.Read(b, 0, b.Length) > 0)
            {
                Console.WriteLine(new string(Encoding.ASCII.GetChars(b)));
            }
        }

        Console.WriteLine("OK");
        return 0;
    }
}


Dec 27, 2014 at 7:37 PM
I have added UnicodeEncoding, Utf8Encoding, AsciiEncoding and BinaryReader and BinaryWriter

Test case
using System;
using System.IO;

class ConsoleApplication
{
    const string fileName = "AppSettings.dat";

    static void Main()
    {
        WriteDefaultValues();
        DisplayValues();
    }

    public static void WriteDefaultValues()
    {
        using (BinaryWriter writer = new BinaryWriter(new FileStream(fileName, FileMode.Create)))
        {
            writer.Write(1.250F);
            writer.Write(@"c:\Temp");
            writer.Write(10);
            writer.Write(true);
        }
    }

    public static void DisplayValues()
    {
        float aspectRatio;
        string tempDirectory;
        int autoSaveTime;
        bool showStatusBar;

        using (BinaryReader reader = new BinaryReader(new FileStream(fileName, FileMode.Open)))
        {
            aspectRatio = reader.ReadSingle();
            tempDirectory = reader.ReadString();
            autoSaveTime = reader.ReadInt32();
            showStatusBar = reader.ReadBoolean();
        }

        Console.WriteLine("Aspect ratio set to: " + aspectRatio);
        Console.WriteLine("Temp directory is: " + tempDirectory);
        Console.WriteLine("Auto save time set to: " + autoSaveTime);
        Console.WriteLine("Show status bar: " + showStatusBar);
    }
}
Output
Aspect ratio set to: 1.250000
Temp directory is: c:\Temp
Auto save time set to: 10
Show status bar: True
Dec 27, 2014 at 11:21 PM
Additionally added StreamReader and StreamWriter

Test case
using System;
using System.IO;

class Test 
{
    public static void Main() 
    {
        try 
        {
            // Create an instance of StreamReader to read from a file. 
            // The using statement also closes the StreamReader. 
            using (StreamReader sr = new StreamReader("TestFile.txt")) 
            {
                string line;
                // Read and display lines from the file until the end of  
                // the file is reached. 
                while ((line = sr.ReadLine()) != null) 
                {
                    Console.WriteLine(line);
                }
            }
        }
        catch (Exception e) 
        {
            // Let the user know what went wrong.
            Console.WriteLine("The file could not be read:");
            Console.WriteLine(e.Message);
        }
    }
}
Jan 7, 2015 at 11:46 AM
I have added formatting for Int16, Int32, Int64, Single, Double
using System;

class X
{
    public static int Main(string[] args)
    {
        int value = -16325;
        string specifier;

        // Use standard numeric format specifier.
        specifier = "G";
        Console.WriteLine("{0}: {1}", specifier, value.ToString(specifier));
        // Displays:    G: -16325
        specifier = "C";
        Console.WriteLine("{0}: {1}", specifier, value.ToString(specifier));
        // Displays:    C: ($16,325.00)
        specifier = "D8";
        Console.WriteLine("{0}: {1}", specifier, value.ToString(specifier));
        // Displays:    D8: -00016325
        specifier = "E4";
        Console.WriteLine("{0}: {1}", specifier, value.ToString(specifier));
        // Displays:    E4: -1.6325E+004
        specifier = "e3";
        Console.WriteLine("{0}: {1}", specifier, value.ToString(specifier));
        // Displays:    e3: -1.633e+004
        specifier = "F";
        Console.WriteLine("{0}: {1}", specifier, value.ToString(specifier));
        // Displays:    F: -16325.00
        specifier = "N";
        Console.WriteLine("{0}: {1}", specifier, value.ToString(specifier));
        // Displays:    N: -16,325.00
        specifier = "P";
        Console.WriteLine("{0}: {1}", specifier, (value / 100000).ToString(specifier));
        // Displays:    P: -16.33 %
        specifier = "X";
        Console.WriteLine("{0}: {1}", specifier, value.ToString(specifier));
        // Displays:    X: FFFFC03B  

        // Use custom numeric format specifiers.
        specifier = "0,0.000";
        Console.WriteLine("{0}: {1}", specifier, value.ToString(specifier));
        // Displays:    0,0.000: -16,325.000
        specifier = "#,#.00#;(#,#.00#)";
        Console.WriteLine("{0}: {1}", specifier, (value * -1).ToString(specifier));
        // Displays:    #,#.00#;(#,#.00#): 16,325.00
        return 0;
    }
}
Output
C: ($16,325.00)
D8: -00016325
E4: -1.6325E+004
e3: -1.633e+004
F: -16325.00
N: -16,325.00
P: 0.00 %
X: FFFFC03B
0,0.000: -16,325.000
#,#.00#;(#,#.00#): 16,325.00
This will be available in the next update
Feb 6, 2015 at 1:36 PM
Edited Feb 6, 2015 at 2:37 PM
Added Multidimensional Arrays
using System;

class X
{
    public static int Main(string[] args)
    {
        // Two-dimensional array. 
        int[,] array2D = new int[,] { { 1, 2 }, { 3, 4 }, { 5, 6 }, { 7, 8 } };
        // The same array with dimensions specified. 
        int[,] array2Da = new int[4, 2] { { 1, 2 }, { 3, 4 }, { 5, 6 }, { 7, 8 } };
        // A similar array with string elements. 
        string[,] array2Db = new string[3, 2] { { "one", "two" }, { "three", "four" },
                                        { "five", "six" } };

        // Three-dimensional array. 
        int[, ,] array3D = new int[,,] { { { 1, 2, 3 }, { 4, 5, 6 } }, 
                                 { { 7, 8, 9 }, { 10, 11, 12 } } };
        // The same array with dimensions specified. 
        int[, ,] array3Da = new int[2, 2, 3] { { { 1, 2, 3 }, { 4, 5, 6 } }, 
                                       { { 7, 8, 9 }, { 10, 11, 12 } } };

        // Accessing array elements.
        System.Console.WriteLine(array2D[0, 0]);
        System.Console.WriteLine(array2D[0, 1]);
        System.Console.WriteLine(array2D[1, 0]);
        System.Console.WriteLine(array2D[1, 1]);
        System.Console.WriteLine(array2D[3, 0]);
        System.Console.WriteLine(array2Db[1, 0]);
        System.Console.WriteLine(array3Da[1, 0, 1]);
        System.Console.WriteLine(array3D[1, 1, 2]);

        return 0;
    }
}
Output
1 
2 
3 
4 
7 
three 
8 
12 
PS. There is a small bug but I will fix it :)
PS2. Fixed :)
Feb 6, 2015 at 1:38 PM
Added Enum.ToString
using System;

public class EnumSample {
    enum Colors {Red = 1, Blue = 2};

    public static void Main() {
        Enum myColors = Colors.Red;
        Console.WriteLine("The value of this instance is '{0}'",
           myColors.ToString());
    }
}
Output
The value of this instance is 'Red'
Jun 18, 2015 at 11:37 AM
I have added System.Linq enumerables so now everyone can enjoy some LINQ functionality

Test example:


using System;
using System.Linq;
using System.Collections.Generic;

public class C
{
    static void Test<T, R> (Func<T, R> d)
    {
    }
    
    public static int Main ()
    {
        Test ((int x) => { return x + 1; });
        
        int[] source = new int[] { 2, 1, 0 };
        IEnumerable<int> e = source.Where((i) => i == 0).Select((i) => i + 1);

        if (e.ToList ()[0] != 1)
            return 1;

        e = source.Where((int i) => i == 0).Select((int i) => i + 1);

        if (e.ToList ()[0] != 1)
            return 2;
        
        e = source.Where(delegate (int i) { return i == 0; }).Select(delegate (int i) { return i + 1; });
        
        if (e.ToList ()[0] != 1)
            return 3;
            
        return 0;
    }
}

It will be available in the next update
Jun 18, 2015 at 1:31 PM
one more nice example
using System;
using System.Linq;

// LINQ and lambdas mix tests

public class C
{
    static bool Test (Func<int, bool> f)
    {
        return false;
    }
    
    static bool Test2 (Func<int, int> f)
    {
        return false;
    }

    public static int Main ()
    {
        var x = new int [] { 'a', 'b', 'c' };
        
        var e = from ck in x
            let xy = Test(c => c == ck)
            where ck == 'v'
            select Test(c => c == ck);

        var e2 = from ck in x
            where Test(c => c == ck)
            select Test(c => c == ck);
    
        int[] int_array = new int [] { 0, 1, 2, 3, 4 };
        var e3 = from int i in int_array group Test2 (gg => i + 2) by Test2 (g => i % 2);

        var e4 = from i in x
            let l = i + 4
            let g = l - 2
            where Test(c => c == l)
            where l > 0
            select l;
            
        var e5 = from a in x
            join b in x on Test (a2 => a2 == a) equals Test (b2 => b2 == b)
            select a;
            
        var e6 = from a in x
            join b in x on Test (a2 => a2 == a) equals Test (b2 => b2 == b) into re6
            select a;
    
        return 0;
    }
}

Jul 8, 2015 at 5:00 PM
Threads are going to be supported in the next update.

Test case:
using System;
using System.Threading;
using System.Collections;

namespace MonitorCS1
{
    class MonitorSample
    {
        const int MAX_LOOP_TIME = 1000;
        Queue m_smplQueue;

        public MonitorSample()
        {
            m_smplQueue = new Queue(); 
        }
        public void FirstThread()
        {
            int counter = 0;

            lock(m_smplQueue)
            {
                while(counter < MAX_LOOP_TIME)
                {
                    //Wait, if the queue is busy.
                    Monitor.Wait(m_smplQueue);

                    //Push one element.
                    m_smplQueue.Enqueue(counter);

                    //Release the waiting thread.
                    Monitor.Pulse(m_smplQueue); 

                    counter++;
                }
            }                   
        }
        public void SecondThread()
        {
            lock(m_smplQueue)
            {
                //Release the waiting thread.
                Monitor.Pulse(m_smplQueue);

                //Wait in the loop, while the queue is busy.
                //Exit on the time-out when the first thread stops. 

                while(Monitor.Wait(m_smplQueue, 1000))
                {
                    //Pop the first element.
                    int counter = (int)m_smplQueue.Dequeue();

                    //Print the first element.
                    Console.WriteLine(counter.ToString());

                    //Release the waiting thread.
                    Monitor.Pulse(m_smplQueue);
                }
            }
        }

        //Return the number of queue elements.
        public int GetQueueCount()
        {
            return m_smplQueue.Count;
        }

        static void Main(string[] args)
        {
            //Create the MonitorSample object.
            MonitorSample test = new MonitorSample();           
            //Create the first thread.
            Thread tFirst = new Thread(new ThreadStart(test.FirstThread));
            //Create the second thread.
            Thread tSecond = new Thread(new ThreadStart(test.SecondThread));
            //Start threads.
            tFirst.Start();
            tSecond.Start();
            //wait to the end of the two threads
            tFirst.Join();
            tSecond.Join();         
            //Print the number of queue elements.
            Console.WriteLine("Queue Count = " + test.GetQueueCount().ToString());
        }
    }
}
Jul 14, 2015 at 4:11 PM
Happy to say that async\await will work in next update but without ThreadPool support (so all task will be LongRunning tasks)

Test case:
using System;
using System.Threading;
using System.Threading.Tasks;

public class Example
{
   public static void Main()
   {
      CancellationTokenSource source = new CancellationTokenSource();

      var t = Task.Run(async delegate
              {
                 await Task.Delay(TimeSpan.FromSeconds(1.5), source.Token);
                 return 42;
              });
      source.Cancel();
      try {
         t.Wait();
      }
      catch (AggregateException ae) {
         foreach (var e in ae.InnerExceptions)
            Console.WriteLine("{0}: {1}", e.GetType().Name, e.Message);
      }
      Console.Write("Task t Status: {0}", t.Status);
      if (t.Status == TaskStatus.RanToCompletion)
         Console.Write(", Result: {0}", t.Result);
      source.Dispose();
   }
}
PS. this already works in my dev brunch.
Jul 16, 2015 at 1:05 PM
ok. async/await partially works
Aug 10, 2015 at 7:52 PM
Finally I have issued new version where I have implemented Interfaces and Structs.

Key changes:
  1. Interfaces reside on a Type instance not on an Instance of the type which significantly reduces the size of a class (as opposed to C++ implementation)
  2. Structs do not use virtual table anymore (as opposed to C++ implementation). So now Structs are POCO
Nov 4, 2015 at 10:42 AM
I have updated source code which can compile and use mscorlib. Later I will write instructions how to compile mscorlib library
Jan 8, 2016 at 4:45 PM
Edited Jan 8, 2016 at 4:46 PM
Now Parallel.For/ForEach works in CoreLib

Test
using System;
using System.Threading;
using System.Threading.Tasks;

public class Test
{
    static int N = 3;

    public static void TestMethod()
    {
        Console.WriteLine("Parallel.For 1");

        // Using a named method.
        Parallel.For(0, N, Method2);

        Console.WriteLine("Parallel.For 2");

        // Using an anonymous method.
        Parallel.For(0, N, delegate (int i)
        {
            // Do Work.
            Console.WriteLine("delegate {0}", i);
        });

        Console.WriteLine("Parallel.For 3");

        // Using a lambda expression.
        Parallel.For(0, N, i =>
        {
            // Do Work.
            Console.WriteLine("=> {0}", i);
        });

        Console.WriteLine("End");
    }

    static void Method2(int i)
    {
        // Do work.
        Console.WriteLine("Method2: {0}", i);
    }
}

class X
{
    public static int Main(string[] args)
    {
        Test.TestMethod();
        Console.WriteLine("Exit");
        return 0;
    }
}