GithubHelp home page GithubHelp logo

csharp-interview's Introduction

csharp-interview

This repo contains questions used in my C# interviews. Some of them are problems from my daily work.

When we should not use double?

Most of the time we use double or float when we need a non-integer number. However, sometimes using double or float can go wrong. Consider this example from "effective java", I have 1 dollar, there are candies for 10cent, 20cent, 30cent and so on. You buy one of each candy till you cannot afford it. How many candies do you buy? How many change do you get?

var funds = 1.00;
var itemsBought = 0;
for (var price = 0.1; funds >= price; price += 0.1)
{
    funds -= price;
    itemsBought++;
}
Console.WriteLine(itemsBought); // 3

The output shows you can only buy 3. However, we know we can buy 4: 0.1$, 0.2$, 0.3$ and 0.4$. This is because double/float cannot exactly represent values like 0.1, 0.01, and it turns out that you have 0.39999999999999991 left, but you cannot afford a 40-cent-candy.

The correct way to do it is to use decimal

var funds = 1.00m; //funds and price are decimal
var itemsBought = 0;
for (var price = .1m; funds >= price; price += .1m)
{
    funds -= price;
    itemsBought++;
}
Console.WriteLine(itemsBought);

Sorting problem

Q: Design a method to sort an array of integers

A: Aha, an easy one, use bubble sort.

Q: What if you need to support sorting an array of doubles and floats

A: Use generics

Q: What if you need to support sorting an array of any objects

A: Um, object implements IComparable

public void sort<T>(T[] array) where T:IComparable
{
    if (array == null || array.Length < 1) return;
    var ordered = false;
    for (var i = 0; i < array.Length-1 && !ordered; i++)
    {
        ordered = true;
        for (var j = 0; j < array.Length - i - 1; j++)
        {
            if (array[j].CompareTo(array[j+1]) > 0)
            {
                ordered = false;
                var temp = array[j];
                array[j] = array[j+1];
                array[j+1] = temp;
            }
        }
    }
}

When do you use reflection?

Normally, interface is preferred than reflection. Using reflection, you lose the compile-time type checking, have verbose coding statement and performance suffers.

In some situations, reflection is inevitable, like remote procedure call, class browser, code analysis tool, registration service, etc.

What's delegate? Any difference among Func, Action and Predicate

Delegate is like function pointer in C++, and this pointer specifies the function signature. Func, Action and Predicate are all delegates. Func allows from 0 to 8 input parameters, and one return type. If your delegate returns nothing, you'll use Action. Predicate has one input parameter, and bool as return type. You can view it as a special Func.

public delegate TResult Func<in T1, in T2, out TResult>(T1 arg1,T2 arg2)

public delegate bool Predicate<in T>(T obj)

public delegate void Action<in T>(T obj)
//return a string without any parameter
Func<string> func = () => "hello"; 

//one string parameter, no return value
Action<String> action = Console.WriteLine; 

Predicate<int> predicate = (x) => { return x > 0; };

What's Event? EventHandler?

Event is a wrapper of delegate, and you can see it as a syntatic sugar of delegate. EventHandler is a delegate, and EventHandler provide a way to customize EventArgs.

public delegate void EventHandler(object sender, EventArgs e)

csharp-interview's People

Contributors

bargitta avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.