Tag Archives: c#

(C#) Factorial prime numbers factorization

Find how many factors there is in N! for a prime P Ex: 5! = (2^3)*(3^1)*(5^1) mult(5,2) = 3 mult(5,3) = 1 mult(5,5) = 1 all else = 0

Posted in C# | Also tagged , , , , , | Leave a comment

(C#) Prime Number Generator

 

Posted in C# | Also tagged , , , , , , | Leave a comment

(C#) Key-Value Pair System.Collections.Generic

Simple example of a Key Value Pair using System.Collections.Generic.

Posted in C# | Also tagged , , , , , , , , , | Leave a comment

Minimax algorithm with alpha-beta pruning in C#

Minimax is an algorithm where two players (min and max) play against each other for minimizing loss and maximizing gain. The following picture illustrates minimax with a game of tic-tac-toe. The algorithm uses a tree for the moves and scores of each player. In some cases, it is unnecessary for the algorithm to check certain subtrees. [...]

Posted in C# | Also tagged , , , , , , , , , , , , , , , , | 1 Comment

(C#) Windows Forms Keyboard Shortcut on Control

Easily create Windows Forms Shortcuts with this class. For example: A shortcut to catch pasting via Ctrl + V. Shortcut sc = new Shortcut(this, new Action(() =>¬† MessageBox.Show(“Data pasted!”))); sc.Keys.Add(Keys.ControlKey); sc.Keys.Add(Keys.V);

Posted in C# | Also tagged , , , , , , , | Leave a comment

(C#) LazyLoader – a simple, thread-safe class that can be used to load data on demand

A simple, thread-safe wrapper class for lazy-loading data into an instance on-demand (i.e. when the instance is first accessed.) Usage: LazyLoader l = new LazyLoader(() => Foo.LoadFromDataSource(“DB Connection String”)); // For access to the lazy-loaded instance: var x = l.Instance.Bar; // Need to ensure that the data is loaded deterministically? Use this: l.EnsureLoad();

Posted in C# | Also tagged , , , , , , , , | Leave a comment

Binary Search Trees (BSTs) in C#

A Binary Search Tree (also known as a BST) is a data structure that contains ordered nodes. Each node contains an element, a left node and a right node. The nodes on the left are all nodes that contain values lower than the element and the nodes on the right contain values higher than the [...]

Posted in C# | Also tagged , , , , , , , , , , , , , | 3 Comments

Operator overloading in C#

C# allows you to overload certain operators. The figure below shows a table of overloadable operators. Suppose there is a BankAccount class which contains a Number property and a Balance property. The + (addition) operator can be overloaded to combine two BankAccount instances and to return a new BankAccount instance with the combined balances. This [...]

Posted in C# | Also tagged , , , , , , , , | Leave a comment

State design pattern in C#

The state pattern is a software design pattern to represent the state of an object. It allows an object to alter its behavior when its state changes. It shows a similarity to the strategy pattern.

Posted in C# | Also tagged , , , , , , , , , , , , | Leave a comment

Detecting keyboard input in XNA

Keyboard input is essential in most games. The user might use it for moving a character, mario style enemy killing and many other things. XNA supports so called keyboard states to detect keyboard input.

Posted in C# | Also tagged , , , , , , , , , | Leave a comment