Critical Developers

Programmers Knowledge Base

C# OOPS Interview Questions and Answers

Can you allow a class to be inherited, but prevent a method from being overridden in C#?
Yes. By Declaring the class public and making the method sealed.

Can you declare an overridden method to be static if the original method is not static?

No. Two virtual methods must have the same signature.

Can you inherit private members of a class?

No, we cannot inherit private members of a class because private members are accessible only to that class and not outside that class.

Is it possible to execute two catch blocks?

No. Whenever, an exception occurs in your program, the correct catch block is executed and the control goes to the finally block.

Can you prevent a class from overriding?
Yes. You can prevent a class from overriding in C# by using the sealed keyword; and in VB by using NotInheritable keyword.

What is 'this' pointer?
'this' pointer refers to the current object of a class.

Can 'this' be used within a static method?
No.

Is it possible for a class to inherit the constructor of its base class?
No. A class cannot inherit the constructor of its base class.

Can you inherit private members of a class?
No. You cannot inherit private member of a class because private members are accessible only to that class and not outside that class.

What's the difference between the 'ref' and 'out' keywords?
An argument passed as "ref" must be initialized before passing to the method whereas "out" parameter needs not to be initialized before passing to a method, and it will be initialized inside the method.

Difference Between Const, ReadOnly and Static ReadOnly in C# ?

Constant variables are declared and initialized at compile time. The value can't be changed after words.
Readonly variables will be initialized only from the non-static constructor of the same class.
Static Readonly variables will be initialized only from the static constructor of the same class.

What is the difference between “dispose” and “finalize” variables in C#?
 Dispose() Method

     -  Dispose method will be used to free unmanaged resources like files, database connection etc.
     -  To clear unmanaged resources we need to write code manually to raise dispose() method.
     -  This Dispose() method belongs to IDisposable interface.
     -  If we need to implement this method for any custom classes we need to inherit the class from IDisposable interface.

 Finalize() Method

     -  Finalize method also free unmanaged resources like database connections, files etc.
     -  It is automatically raised by garbage collection mechanism whenever the object goes out of scope.
     -  This method belongs to object class.
     -  We need to implement this method whenever we have unmanaged resources in our code and make sure these resources will be freed when garbage collection process done.

Why to use "using" statement in C#?
The using block is used to obtain a resource and use it and then automatically dispose off when the execution of block completed.


floatheader.js - Simple jQuery Plugin for Fixed Table Headers

As there are lots of plugins to fix/float the table header and they are also included with bunch of options and features. But sometime we do not need all those features, so here in I have written a simple and a light weight plugin which floats the table header. And but obvious we all know why we need floating headers, because our user hate to scroll up and down when there are numbers numbers of records on a page.

FloatHeader is a very light-weight jQuery plugin used to fix the table header. The logic is very simple so that you can tweak as per you requirement. Only a few lines of code, thats it; what else you need.

Lets learn how to use it:-

Very first we need a table markups as shown below:-

 <table class="tblList">
      <thead>
        <tr>
          <th>Header#1</th>
          <th>Header#2</th>
          <th>Header#3</th>
        </tr>
      </thead>
      <tbody>
        <tr>
          <td>Column-1</td>
          <td>Column-2</td>
          <td>Column-3</td>
        </tr>
        <tr>
          <td>Column-4</td>
          <td>Column-5</td>
          <td>Column-6</td>
        </tr>
        <tr>

. . . . . .


        <tr>
          <td>Column-22</td>
          <td>Column-23</td>
          <td>Column-24</td>
        </tr>
        <tr>
          <td>Column-25</td>
          <td>Column-26</td>
          <td>Column-27</td>
        </tr>
      </tbody>
    </table>

Add styles for table as in here:-

/* primary styles */

html, body{
  margin:0;
  padding:0;
  height:100%;
  color: #000000;
}

/* required styles for plugin */
.maincontainer
{
   width:100%;
   overflow-x: hidden;
   overflow-y: auto;
   margin: 0 auto;
   position:relative;
   height:200px;
}

.headerclone
{
    width:100%;
   z-index: 9999;
   display:none;
   position:absolute;
}
/* yours table styles */
.tblList
{
    width: 100%;
    border-collapse: collapse;
    border-style: None;
}

.tblList thead th {
    background-color: #f7f7f7 !important;
    border-color: #cbc8c8 #cbc8c8 #e0e0e0 #cbc8c8;
    border-style: solid;
    border-width: 1px;
    font-size: 15px;
    font-weight: bold;
    height: 25px;
    padding: 4px 10px 3px;
    text-align: left;
    vertical-align: top;
}
.tblList tbody td {
    border-color: #cbc8c8 #cbc8c8 #e0e0e0 #cbc8c8;
    border-style: solid;
    border-width: 1px;
    font-size: 14px;
    font-weight: normal;
    padding: 4px 10px 6px;
    vertical-align: top;
}
.tblList tr.alt td {
    background-color: #FFFFFF !important;
}
.tblList tbody tr:nth-child(2n+1) {
    background-color: #F7F7F7 !important;
}
.tblList tbody tr:nth-child(2n) {
    background-color: #FFFFFF !important;
}

Add a reference of jQuery.js either using local path or using cdn.

Then Add floatheader.js plugin which is written as below:-

    // FloatHeader v1.0 - jQuery floatheader plugin
    // (c) 2015 Ram Yadav - criticaldevelopers.com
    // License: http://www.opensource.org/licenses/mit-license.php

    (function ($) {
        $.fn.floatheader = function (options) {
            // Default Settings
            var settings = $.extend({

            }, options);
            // Apply Settings Variables
            //
            function fixheader(elem) {
               // Add supportive wrappers to scrollable table
               $(elem).wrap('<div class="maincontainer">');
               $("div.maincontainer").prepend('<div class="headerclone"></div>');
               $(elem).clone().appendTo(".headerclone")
               $(".headerclone").find("table > tbody").remove();

               // To get same width of header while cloning
               $(elem).find("thead th").each(function(i){
               $(".headerclone").find("table thead tr th").eq(i).css({"width":$(this).width()+"px"});           
               });

               // onscroll show/hide the cloned header
               $("div.maincontainer").bind("scroll", function () {
                   var offset = $(this).scrollTop();
                   var headerheight = $(elem).find("thead th").eq(0).outerHeight();
                   if (offset >= headerheight) {
                       $(".headerclone").show();
                       $(".headerclone").css({ "top": offset + "px" });
                   }
                   else {
                       $(".headerclone").hide();
                       $(".headerclone").css({ "top": offset + "px" });
                   }
               });
            }
            //
            return fixheader(this);
        };
    } (jQuery));

And Now its time to call the floatheader as here below:-

<script type="text/javascript">

    $(document).ready(function () {
        $("table.tblList").floatheader();
    });

</script>


To view a demo click here