Create identity matrix in C#

static double[][] IdentityMatrix(int n) { double[][] result = CreateMatrix(n, n); for (int i = 0; i < n; ++i) result[i][i] = 1.0; return result; } static double[][] CreateMatrix(int rows, int cols) { double[][] result = new double[rows][]; for (int i = 0; i < rows; ++i) result[i] = new double[cols]; return result; }
Continue reading…

Enjoyed this post? Share it!

 
 

Parallel matrix multiplication in C# using task parallel library

static double[][] MatrixMultiplication(double[][] matrixOne, double[][] matrixTwo) { //aRows = rows in matrixOne //bCols = columns in matrixTwo double[][] result = CreateMatrix(aRows, bCols); Parallel.For(0, aRows, i => { for (int j = 0; j < bCols; ++j) for (int k = 0; k < aCols; ++k) result[i][j] += matrixOne[i][k] * matrixTwo[k][j]; } ); return result; } […]
Continue reading…

Enjoyed this post? Share it!

 
 

Matrix multiplication in C#

static double[][] MatrixMultiplication(double[][] matrixOne, double[][] matrixTwo) { int aRows = matrixOne.Length; int aCols = matrixOne[0].Length; int bRows = matrixTwo.Length; int bCols = matrixTwo[0].Length; if (aCols != bRows) throw new Exception("Out of shape matrices"); double[][] result = CreateMatrix(aRows, bCols); for (int i = 0; i < aRows; ++i) // each row of MatrixOne for (int j […]
Continue reading…

Enjoyed this post? Share it!

 
 

What are c# Tuples?

In C#, a tuple represents a set of data in a single data structure. Tuples can: 1. Provide easy access and manipulation of a set of data. 2. Return multiple values from a method. 3. Pass multiple values to a method in a single parameter. Example: var myTuple = Tuple.Create("w3mentor", new DateTime(2009, 12, 8)); Console.WriteLine("{0} […]
Continue reading…

Enjoyed this post? Share it!

 
 

Finalizers in C#

Finalizers are class methods that are executed before the garbage collector frees the memory of a referenced object no longer in use. The syntax for a finalizer is the name of the class to which the tilde sign ~ preceeds: class TestClass1 { ~ TestClass1 () {… } } The above code is converted by […]
Continue reading…

Enjoyed this post? Share it!

 
 

Embedded types in C#

An embedded type in C# is declared within the scope of another type. public class FirstLevel { public class NestedClass {} // embedded Class public enum Color {Red, Blue, Tan} // Embedded Enum } An embedded type has the following features: 1. The embedded type can access the private members of enclosing type and all […]
Continue reading…

Enjoyed this post? Share it!

 
 

Nullable types in c#

Reference types in c# can have non-existing value with a zero reference. However, For value types this is usually not possible: string s = null; // OK, reference-type int i = 0; // compile-time, value-type can not be null To represent zero in a value type, you need a special construct called Nullable Type . […]
Continue reading…

Enjoyed this post? Share it!

 
 

Display the call stack method names in C#

using System.Diagnostics;   [STAThread] public static void Main() { //obtain call stack StackTrace stackTrace = new StackTrace(); // obtain method calls (frames) StackFrame[] stackFrames = stackTrace.GetFrames();   // display method names foreach (StackFrame stackFrame in stackFrames) { Console.WriteLine(stackFrame.GetMethod().Name); // output method name } }
Continue reading…

Enjoyed this post? Share it!

 
 

Using TrimStart and TrimEnd in C# strings

The TrimStart function in C# removes all occurances of a string/character from the beginning of an input string. string testInput="001"; Int val=Conver.ToInt32(testInput.TrimStart(’0’)); Console.WriteLine(val); Ouput: 1 We can use the TrimEnd function if we need to remove the character at the end of the string. The TrimEnd function in C# removes all occurances of a string/character […]
Continue reading…

Enjoyed this post? Share it!

 
 

Getting all installed printers in system using C# .NET

If a printer is installed on the machine, the following code will list the installed printer(s) in the system. Add a listbox named printersList to a windows form and use the following in the form_load. using System.Drawing.Printing; private void Form1_Load(object sender, EventArgs e) { foreach (String printer in PrinterSettings.InstalledPrinters) { printersList.Items.Add(printer.ToString()); } }
Continue reading…

Enjoyed this post? Share it!