Showing posts with label Programming C#. Show all posts
Showing posts with label Programming C#. Show all posts

Listview Control (Custom)


In my search for a usable Listview control i have tested and tried many and many available user controls. The ones i liked are to expensive for me (janus, infragistics). So i decided to create my own on.

C# Brainteaser compiler

Should this code compile? Does it? What does it mean?


using System;

class Test
{
enum Foo { Bar, Baz };

static void Main()
{
Foo f = 0.0;
Console.WriteLine(f);
}
}




Answer: This shouldn't compile, but it does under the MS compilers for both C# 2 and 3 (and probably 1 as well - I haven't checked). It shouldn't compile because only the literal 0 should be implicitly convertible to the default value of any enum. Here the decimal is 0.0. Just a little compiler bug. The result is to print Bar as that's the 0 value of the Foo.

C# Brainteaser print

Here's some code using the anonymous method feature of C# 2. What does it do?

using System;
using System.Collections.Generic;

class Test
{
delegate void Printer();

static void Main()
{
List printers = new List();
for (int i=0; i < 10; i++)
{
printers.Add(delegate { Console.WriteLine(i); });
}

foreach (Printer printer in printers)
{
printer();
}
}
}




Answer: Ah, the joys of captured variables. There's only one i variable here, and its value changes on each iteration of the loop. The anonymous methods capture the variable itself rather than its value at the point of creation - so the result is 10 printed ten times!

C# Brainteaser arithmetic

Computers are meant to be good at arithmetic, aren't they? Why does this print "False"?


double d1 = 1.000001;
double d2 = 0.000001;
Console.WriteLine((d1-d2)==1.0);






Answer: All the values here are stored as binary floating point. While 1.0 can be stored exactly, 1.000001 is actually stored as 1.0000009999999999177333620536956004798412322998046875, and 0.000001 is actually stored as 0.000000999999999999999954748111825886258685613938723690807819366455078125. The difference between them isn't exactly 1.0, and in fact the difference can't be stored exactly either

C# Brainteaser Order it

What will be displayed, why, and how confident are you?

using System;

class Foo
{
static Foo()
{
Console.WriteLine ("Foo");
}
}

class Bar
{
static int i = Init();

static int Init()
{
Console.WriteLine("Bar");
return 0;
}
}

class Test
{
static void Main()
{
Foo f = new Foo();
Bar b = new Bar();
}
}




Answer: On my box, Bar is printed and then Foo. This is because Foo has a static constructor, which cannot be run until the exact point at which the class first has to be initialized. Bar doesn't have a static constructor though, so the CLR is allowed to initialize it earlier. However, there's nothing to guarantee that Bar will be printed at all. No static fields have been referenced, so in theory the CLR doesn't have to initialize it at all in our example.

C# Brainteaser Overloading

What is displayed, and why?

using System;

class Base
{
public virtual void Foo(int x)
{
Console.WriteLine ("Base.Foo(int)");
}
}

class Derived : Base
{
public override void Foo(int x)
{
Console.WriteLine ("Derived.Foo(int)");
}

public void Foo(object o)
{
Console.WriteLine ("Derived.Foo(object)");
}
}
class Test
{
static void Main()
{
Derived d = new Derived();
int i = 10;
d.Foo(i);
}
}


Answer: Derived.Foo(object) is printed - when choosing an overload, if there are any compatible methods declared in a derived class, all signatures declared in the base class are ignored - even if they're overridden in the same derived class!

The name of the component(s) is used within the Windows Form Designer

While opening the windows form designer the following error pops up

"The name '' is already used by another object "

Cause:
The designer probably needs prozac . General error :-)

Solution:
Microsoft self writes on there website:

"please write down the exact message and contact microsoft supper " Yeah right, like that will bring a solution.


I say. go back one backup of your sourcefile and bringin adjusted again. This is the fastest solution. Another tip from experience " You probably have one line of code duplicate in the designer. Open the .CS and the generated code file and check it. 99% sure that this will solve your issue"


A way of life

// "In the end it's a little boy expressing himself." Marcel


while (I_am_alive)


{


cout<<"I love to do more than just programming.";


}


A way of life

// "In the end it's a little boy expressing himself." Marcel

while (I_am_alive)

{

cout<<"I love to do more than just programming.";

}