Contents tagged with C#

  • How to send email in C# windows forms application

    Hello everybody,

    sometime people ask me how to send email from C# windows forms application or even ask is it possible at all to accomplish in C#?

    I can reassure you that yes, it's possible. 

    For this purpose you can use following code sample:

    private void btnSendMessage_Click(object sender, EventArgs e)


    var from = new MailAddress(txtFrom.Text, "From Name");

    var toEmail = new MailAddress(txtTo.Text, "To Name");

    string passwordFromEmailOfSender = txtPassword.Text;

    string subject = "Subject";

    string body = txtMessage.Text;

    var smtp = new SmtpClient


    Host = "", … more

  • How to avoid many if during programming for cAlgo

    Hello everybody,

    today I want to write few words about how to avoid nested ifs if you program for cAlgo.

    Recently I've got following request ( code is modified in order to protect privacy of customers idea ):

    buy in following case:

    price is above moving average 20

    price is above moving average 30

    stochastic is above 40

    each of indciators from 1 - 3 can be turned on/off

    As example consider following case:

    if( movingAverage30On && MarketSeries.Low[1] > longMa.Result[1])


    if(movingAverage20On && MarketSeries.Low[1] > shortMa.Result[1])


    if(stochasticOn && _stochastic.PercentD[1] > 40)






    if(movingAverage20 … more

  • How to parse google search results

    Hello everybody,

    today I want to describe how to use C# for scrapping results. 

    For me it was surprising to see, that there is a stereotype that for scrapping the best tool or tools only to use are R, Python. And majority of people who needed functionality of parsing wanted that functionality in some of those languages. While I do agree that those are great languages, which really enhance toolbox of developer, I want to show also that in C# you can achieve similar results. 

    Below goes screenshot of C# program which sends request to google and receives result:

    Application workflow is pretty simple.

    You enter search request. At screenshot you can see that search request was i7 … more

  • How closures are implemented by .Net

    Hello everybody,

    today I want to share piece of wisdom which is interesting, but hard to explain why somebody may need it. That is how closures are implemented. 

    Consider the following code:


    class Program


            static void Main(string[] args)


                int a = 54;

                Task t = new Task(

                    () =>



                        Console.WriteLine("Inside task");






    Now you may wonder, how a will be passed to closure? How Console.WriteLine will be executed?

    For my surprise .Net for closures generates behind the … more

  • C# Task, ContinueWith

    Hello everybody,

    today I want to write few words about Task Paralel library, and in particular two methods: Task.Start, and Task.ContinueWith.


    System.Threading.Tasks.Task t = new System.Threading.Tasks.Task(

                    () => {

                        //some heavy code like intensive calculations, reading from db, etc.




                System.Threading.Tasks.Task t2 = t.ContinueWith(

                    (a) => {

                    //Code to update UI




    Following template has the following structure. You need to split your code … more

  • Generate TypeScript from Enum

    Hello everybody,

    today I want to share with the world code, which generates typescript from enum declaration. I suppose everything should be understandable from code itself:

    using System;

    using System.Collections.Generic;

    using System.ComponentModel;

    using System.Linq;

    using System.Reflection;

    using System.Text;

    using System.Threading.Tasks;

    namespace System


    public static class AttributesSimplification





    public enum EnumSample


    [Description("Choice One")]


    [Description("Choice Two")]



       gives following ts:

    module Enumerations {

    export module EnumSample {

    export enum Enum {

    ChoiceOne = 0, … more

  • One more summary of Moq library

    Examples of Mock usages

    Mock - is a simple and lightweight isolation framework, which is built on the basis of anonymous methods and expression trees. To create them it uses code generation, thus allowing to mock interfaces, virtual methods (and even protected methods) and does not allow to mock non-virtual and static methods.


    At market exists only two frameworks that allow to mock anything. These are TypeMockIsolator and Microsoft Fakes, available in Visual Studio 2012 and higher. These frameworks, unlike Mock ( which uses code generation) use CLR Profiling API, allowing to mock any method even for static, virtual and private methods. IMHO they are good for testing legacy code … more

  • C# and continue operator in cycles

    Hello everybody,

    today I want to write a notice about word continue in C#. 

    So, how it look like:

    for( int i = 0; i < 10; i++ )


         if(i == 5)




        //do some other staff


    In this code, //do some other staff will be executed only 9 times, because for fifth it is written as continue. Continue can be compared to situation, when you bust some boxes, but then you see, that some box doesn't feet to your needs. Then you'll say to C# compiler, skip the box, and continue to bust another boxes. more

  • CQRS in in C# most trivial possible example

    Hello everybody,

    today I want to make compendium of CQRS for very simple case.

    So, at one of the projects I seen following way of implementing code:

    public class WriteModel


    //common properties

    public int SomeEntity {get;set; } // ID of some entity


    public class ReadModel : WriteModel


    public string AdditionalProperty {get;set; }


    and that way of implementing it was proudly named CQRS. To be honest I wasn't very agreeable on that part, so I decided to hear opinions of other people, not only mine understanding of what I read in book.

    And I'd like to save one of the comments not only at stackoverflow, but also at my web site.

    So, what's wrong with … more

  • Trivial implementation of File uploader

    Hello everybody,

    today I want to share with you fragment of code of html extension.

    Imagine that you edit your cshtml file, and see following code:

    @Html.EditorFor(model => model.SomeField

    Then you need to add some file upload and notice, that there is not file upload! 

    How can you fix this situation?

    I propose to use HtmlExtension code. For example like this:

    public static MvcHtmlString FileUpload(this HtmlHelper htmlHelper, string cssClass, object htmlAttrs = null) {

    var tb = new TagBuilder("input");

    tb.MergeAttribute("type", "file");


    if (!string.IsNullOrWhiteSpace(cssClass)) {

    tb. … more

  • Usage of Factor in R

    Hello everybody,

    today I want to make post about usage of factor in R.

    So, first of all, what is factor at all? 

    I was surprised to see that factor is very overloaded term and can have a lot of meanings in many areas.  So, for what factors can be used in R?

    First of all they can be used for storing categorical or nominal values. For example it can be convenient for saving gender, blood group, car type, etc. 

    So, let's say you wrote something like this: 

    cars.classes <- c("Coupe", "Cabriolet", "Sport", "Hatchback")

    For now cars.classes is not a factor, but just character vector. 

    If you execute this code in RStudio, and then execute in RStudio following line:

    cars.classes … more

  • Coercion in programming

    Hello everybody,

    just one short notice. Sometime in programming you can read word coercion. 

    You may be wondering what it means. 

    If to put simply, it is converting of one type into the other. 

    In C# coercion can be of two types: 

    implicit and explicit. 

    double a,b;

    a = 25; // implicit coercion

    b = (double) 7; //explicit coercion ( casting)


  • The Adapter Pattern

    Hello everybody,

    today I want to make post about one of design patterns which is called Adapter. There are many articles over the web about this topic, but I hope to bring some clarity in this area.

    Before I proceed any further, let me describe situation, when you can need it. One of the cases for consideration can be option, if you have your own class, let's name it A. And then let's say that there is some other class B, which has some needed function for you. For example like this:

    How you can deal with this? One of the common ways which many people do, is to use class be directly in your code. For example like this:

    Aggregate class B from class A in some kind of function, and do … more

  • Moq and unit test

    Hello everybody,

    today I want to write few words about Moq and unit testing. 

    First of all you can have a question, why do I need Moq at all?

    Moq is especially useful for cases if we don't have desire to code interface, then code implementation of interface, and then code fake implementation. As usually those simple implementation are relatively simple so somebody ( as Moq team ) can organize code generation for your simple interface implementations.

    So, unit test is something that should follow 3 A:




    General schema of Unit test implementation can be like this:


    public void SomeTest()



    var … more

  • Override Equals method of value types

    Hello everybody.

    Today I want to give some demo.

    using System;

    using System.Diagnostics;

    namespace StackOverflowQuest


    class Program


    struct StructTest


    public string TestString { get; set; }

    //public override bool Equals(object obj)


    // var ct = (StructTest)obj;

    // return ct.TestString == this.TestString;



    class ClassTest


    public string TestString { get; set; }

    public override bool Equals(object obj)


    var ct = obj as ClassTest;

    if (ct == null) … more

  • Search for duplicates

    Hello everybody,

    Here is just shorp version of application which searches for duplicates in folder.

    In  downloadable archive you can find my implementation of searcher with Task, ContinueWith and some other new features of C# 4.5 more