Skip to main content

Posts

Showing posts with the label C# Brainteasers

C# Brainteasers on Using Block

What will be the output of following code using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace JUSTFORFUN {     class Program     {         static void Main(string[] args)         {             using (Student s = new Student())             {                 Console.WriteLine("Insed Using Bock");             }             Console.Read();         }     }     class Student     {              } } Output : Build Error 1 'JUSTFORFUN.Student': type used in a using statement must be implicitly convertible to 'System.IDisposable What it means is, if we need to use any type in using block that t...

using Statement in C#

The following example shows how to use the using statement. C# using (Font font1 = new Font( "Arial" , 10.0f)) { byte charset = font1.GdiCharSet; } Remarks File  and  Font  are examples of managed types that access unmanaged resources (in this case file handles and device contexts). There are many other kinds of unmanaged resources and class library types that encapsulate them. All such types must implement the  IDisposable  interface. As a rule, when you use an  IDisposable  object, you should declare and instantiate it in a  using  statement. The  using  statement calls the  Dispose  method on the object in the correct way, and (when you use it as shown earlier) it also causes the object itself to go out of scope as soon as  Dispose  is called. Within the  using  block, the object is read-only and cannot be modified or reassigned. The  using  sta...

C# Brainteaser (Multiple Catch Blocks For Single Try Block)

What will be output of following code using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace JUSTFORFUN {     class Program     {         static void Main(string[] args)         {             try             {                 int a = 10;                 int b = 0;                 int c = a / b;             }             catch (NullReferenceException ex2)             {                 Console.WriteLine("generic exception with msg :" + ex2.Message);             }             catch (DivideByZeroException...

In C#, what is the difference between String and string?

In C#, what is the difference between String and string? Example: string s = "Hello, World"; String S = "Hello, World"; string is an alias for System.String. So technically, there is no difference. It's like int vs. System.Int32. As far as guidelines, I think it's generally recommended to use string any time you're referring to an object. Example: string place = "world"; Likewise, I think it's generally recommended to use String if you need to refer specifically to the class. Example: string greet = String.Format("Hello {0}!", place); This is the style that Microsoft tends to use in their examples.

A Deep Dive into C# Abstract Class

Abstract class is a special type of class which cannot be instantiated and acts as a base class for other classes. Abstract class members marked as abstract must be implemented by derived classes. The purpose of an abstract class is to provide basic or default functionality as well as common functionality that multiple derived classes can share and override. For example , a class library may define an abstract class that is used as a parameter to many of its functions, and require programmers using that library to provide their own implementation of the class by creating a derived class. In C#, System.IO.FileStream is an implementation of the System.IO.Stream abstract class. abstract class ShapesClass { abstract public int Area (); } class Square : ShapesClass { int side = 0 ;   public Square ( int n ) { side = n ; } // Override Area method public override int Area () { return side * side ; } }   class Rectangle : Shape...

C# Brainteasers

Please try to find the output of following code without running it. using System; using System.Collections.Generic; class Program {     static void Main(string[] args)     {         DoSomething("a", "b");         Console.ReadLine();     }     public static void DoSomething<T>(IList<T> set)     {         Console.WriteLine(set.Count);     }     public static void DoSomething<T>(params T[] items)     {         List<T> set = new List<T>();         foreach (T t in items)         {             if (t == null)                 continue;             set.Add(t);         }         DoSomething(set);   ...

C# Brainteasers

I first saw this on  Ayende's blog  (in a rather more obscure form, admittedly). Once again, work out what will be printed, and why. using  System; class  Test {      static   void  Main()     {         Foo( "Hello" );     }          static   void  Foo( object  x)     {         Console.WriteLine( "object" );     }          static   void  Foo<T>( params  T[] x)     {         Console.WriteLine( "params T[]" );     } } Answer:   params T[]  is printed. Now why would the compiler choose to create an array when it doesn't have to? Well... there are two stages to this. Firstly, ...

C# Brainteasers

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# Brainteasers

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<Printer> printers =  new  List<Printer>();          for  ( int  i=0; i < 10; i++)         {             printers.Add( delegate  { Console.WriteLine(i); });         }                  foreach  (Printer printer  in  printers)        ...

C# Brainteasers

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# Brainteasers

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 an...

C# Brainteasers

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...