Wednesday, March 20, 2013

The C# IS operator

will return true for tests of Interface implementation and class parentage


    interface IFuddy
    {
       string  test();
    }

    public class testBase: IFuddy
    {
        public string test()
        {
            return "hody";
        }
    }


    public class testA : testBase
    {
        public string atest()
        {
            return "hodydoe";
        }
    }

    public class testB : testBase
    {
        public string btest()
        {
            return "awaywego";
        }
    }


   private void retu(IFuddy f)
        {
            if (f is testA)
            {
                Response.Write((f as testA).atest());
            }

            if (f is testB)
            {
                Response.Write((f as testB).btest());
            }
            if (f is testBase)
            {
                Response.Write("f is testBase");
            }
            if (f is IFuddy)
            {
                Response.Write("f is IFuddy");
            }


        }

      protected void Button12_Click(object sender, EventArgs e)
        {
            testA t1 = new testA();
            testB t2 = new testB();
            retu(t1);
            retu(t2);
        }

Will write:

hodydoe
f is testBase
f is IFuddy
awaywego
f is testBase
f is IFuddy

Wednesday, March 13, 2013

ref with reference types


a vivid example:       

 private void changeRef(ref object o)
        {
            o = new GCNotificationStatus();
        }
        private void change(object o)
        {
            o = new GCNotificationStatus();
        }

        protected void Button14_Click(object sender, EventArgs e)
        {
            TaiwanCalendar t = new TaiwanCalendar();
            object test = t;
            change(test);
            Response.Write(test.GetType().FullName);   
            changeRef(ref test);
            Response.Write(test.GetType().FullName);
        }

cannot convert from 'ref blah' to 'ref object'



this doesnt compile:
private void changeRef(ref object o)
        {
            o = new GCNotificationStatus();

        }
        private void change(object o)
        {
            o = new GCNotificationStatus();

        }

        protected void Button14_Click(object sender, EventArgs e)
        {
            TaiwanCalendar t = new TaiwanCalendar();
            changeRef(ref t);
            change(t);
        }

the reason

Unable to load one or more of the requested types error

In your global.asax add a reference

using System.Reflection;

and handle ReflectionTypeLoadException

loop through the LoaderExceptions collection to find all missing references.


code for example


string appNamespace = null;
            try
            {
                // determine namespace of main assembly
                foreach (Assembly ass in AppDomain.CurrentDomain.GetAssemblies())
                {
                    foreach (Type type in ass.GetTypes())
                    {
                        if (type.IsSubclassOf(typeof(HttpApplication)))
                        {
                            appNamespace = type.Namespace;
                            break;
                        }
                    }
                    if (appNamespace != null)
                        break;
                }
            }
            catch(Exception ex)
            {
                StringBuilder sb = new StringBuilder();
                if (ex is ReflectionTypeLoadException)
                {
                    foreach (Exception exSub in ((ReflectionTypeLoadException)ex).LoaderExceptions)
                    {

                        sb.AppendLine(exSub.Message);
                        if (exSub is FileNotFoundException)
                        {
                            FileNotFoundException exFileNotFound = exSub as FileNotFoundException;
                            if (!string.IsNullOrEmpty(exFileNotFound.FusionLog))
                            {
                                sb.AppendLine(exFileNotFound.FusionLog);
                            }
                        }

                    }
                    try
                    {
                        using (StreamWriter outfile = new StreamWriter(Config.GetValue("Logger.ReferenceErrorLog", "referenceerror.log"), true))
                        {
                            outfile.Write(sb.ToString());
                        }
                    }
                    catch{}
                }
                else
                {
                   try
                   {
                    using (StreamWriter outfile = new StreamWriter(Config.GetValue("Logger.ReferenceErrorLog","referenceerror.log"),true))
                    {
                        outfile.Write(ex.Message);
                    }
                   }
                   catch { }
                    throw;
                }

            }


must declare a body because it is not marked abstract, extern, or partial

This error is not always giving the correct message

consider:

class Animal
    {
         Animal();
    }
  
this will raise the above error

but this :
     partial class Animal
    {
        partial Animal();
    }
    partial class Animal
    {
        Animal() { }
    }
wont help 


Multiple classes in css and jquery



.blah1.blah2 are treated differently
in css this is "or" in jquery this is "and"

CSS precedence

what will happen here?

<head runat="server">
    <title>Untitled Page</title>
    <style type ="text/css"  >
        .testClass{color:red ; font-family:Calibri;background-color:Purple; font-size:xx-small  }
        #testID{color:blue ; font-size:x-large  }
        p {color:Green  ; background-color:Gray; font-size:small }
        html { font-size :xx-large  }  
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <p  class ="testClass"  id = "testID"   >
    dsfsdfdsf<br/>
    sdfsdf<br/>
    </p>
    </div>
    </form>
</body>
</html>

Style overrides ID that  overrides class that overrides element

c# interview questions (intermediate)

  • is the LINQ Any operator a linear search?
  • what patterns have you used in your code?
  • if you needed to implement a process that had sub process A,B,C,D,E and B and D changed in detail in 5 situations - how would you model this?
  • what is the Big O notation of a binary search?
  • Can you describe how a hash table works?
  • how can you add a compiler warning?
    •        #warning
  • Can you implement an interface that is using a base object with an implementation that is using a descendant class?
  • how can you get the members of a class progromatically?
    •         Type t = typeof(blah); MemberInfo[] m =  t.GetMembers();
  • how can you alert a user of your class that a procedure is obsolete?
    •          [Obsolete("hody doe")]
  • can a private member ever be available to a different class without reflection?
    •        Nested classes
  • can you change the name of a rerefenced class to something else?
    •        alias  - using msg = System.Windows.Forms.MessageBox; 
  • var test = new { herby = "hello", age = 67f } what type is this?
    • anonymous - system generated name
  • what is the meaning of checked and uchecked keyword in c#?
  • What type of objects are able to be queried by linq?
  • when you have a readonly variable pointing to an object, can you change the value of the object?
  • How would you reference a custom attribute added to a object?
    • GetCustomAttributes, reflection
  • Why are strings immutable in c#
  • Which Interface would you implement for deep copying?
    • ICloneable
  • How can you add functionality to a sealed class?
    • extension method
  • How would encapsulate a reference type like a list as a member of a class to be read only?
  • how to have a generic procedure make sure whatever is being passed implements an interface properly  
    • void SwapIfGreater<T>(ref T lhs, ref T rhs) where T : System.IComparable<T>
  • How to Explicitly Implement Members of Two Interfaces 
    • interface IA
          {
               void hody();
          }

          interface IB
          {
               void hody();
          }

          class HodyDoe : IA, IB
          {
              void IA.hody()
              {
                  Console.WriteLine("IA.hody"); 
              }

              void IB.hody()
              {
                  Console.WriteLine("IB.hody"); 
              }
          }
         
          class Program
          {
              static void Main(string[] args)
              {

                  HodyDoe h = new HodyDoe();
                  (h as IA).hody();
                  (h as IB).hody();
                  Console.ReadLine(); 

              }
          }



c# Interview Questions (Junior Developer)


  • What is the difference between a value type and a reference type?
  • When checking true false evaluations what is the difference between "&" and "&&"?
  • Is  datetime a value type or a reference type?
  • Is there any way to assign null to an int?
  • How to get a distinct list from an array?
  • What is boxing?
  • What are generics and did you ever use them? What are the advantages of using generics?
  • What is the difference between a hashtable and a dictionary?
  • Can a dictionary have duplicate keys? Which structure can you use that allows duplicate keys?
  • What does sealed mean and give an example of a c# system class that is sealed
  • Can an abstract class have a constructor?
  • Can a private member be virtual?
  • can an abstract method be declared virtual?
  • If you had to refactor code – what steps would you take?
  • What are some advantages of a singleton over a static class?
  • Can you inherit a static class?
  • Can a static constructor have a parameter?
  • Please explain garbage collection in .net
  • Which interface do you need to implement to use
    •                 “using” keyword
      • IDisposable
    •                 Foreach loops
      • IEnumerable or IEnumerable<T>
    •                 Custom sorting
      • IComparable<T>
  • Is there any way to find out the value of a private member of a class?
  • Can a static procedure be recursive?
  • An abstract class can have a public constructor? A private contructor?
  • Whats the order of contructors with an inherited class? Static constructors?
  • What is the difference between finalize and dispose?
  • Are constructors inherited from base classes? 
  • in c# can you change the access level of a derived overwritten procedure protected to public?
  • if you have a derived class what difference is there between declaring "new" or "override" in a procedure?
  • if a base class has a protected member, can the child class override to public?
    • in c# - cannot change access modifiers when overriding 'protected' inherited member
  • can you declare a protected class in a namespace
    • no -Elements defined in a namespace cannot be explicitly declared as private, protected, or protected internal
  • Can you declare a class to be private?
    • Only with nested classes
  • what is the default accessor for a class? for a class member?
  • var something = 100.50F; what is the type of something?
  • what is the difference between a const and a readonly value?
  • what is the difference between typeof, GetType(), and is?
  • what is the difference between ref and out?
  • what is the difference between as and casting?
  • what is the purpose of partial classes?
  • if your argument is a reference type , does it make a difference if you use the ref keyword?
  • what is the difference between Equals ,ReferenceEquals and "=="
  • what is the value of y here:        int y = x ?? -1;
  • How would you remove duplicate entries in an array?
  • what is the value of 5/2 in c#?
  • can you share namespaces over projects?
  • Does array.Clone() create a deep or shallow copy?
  • does this compile?
    •     {
    •                 string s = "gdfgd";
    •             }
    •             {
    •                 string s = "gdfgd";
    •             }
    •         }

Naming Issues in .net

If you name a class in a namespace the same name as a project , you are inviting trouble.

for e.g. , references will now be confused
if you have a project called test and you add a class called test and you have a service reference called document, for example,

this will now not compile


        private test.Document.blah[] blahs;

Advantages of Singleton over static class


  • no need to declare static in procedures
  • ability to use inheritance, implement interfaces
  • ability to pass class as an argument


Difference between hashtable and dictionary

besides the usual answer with the need to cast or boxing -


Sample Javascript Interview Questions


//1
//what value will show with the alert?
test = "herby";
{
    var test = "not herby";
}
alert(test);
//1

//2
//what value will show with the alerts?
//how would you write this differently
function triple(x)
{
i= 3;
return x * i;
}
var i =2;

alert(triple(i)) ;
alert(i);
//2

//3
//will this run? why or why not?
var o = new Object;
o.totriple = triple;
with(o)
{
alert(totriple(5));
}
//

//4
//what are the results and why
alert("1"==true);
alert("1"===true);
//

//5
//what value will show with the alert?
//this applies to other languages also
var x = 1;
var y = x++;
alert(y);
//5

//6
//will the alert run?
//bonus - what is this called?
if(x==0 && y ==0);
alert("can this be?");
//

//7
//what are the results here?
alert("1" + 2);
alert(1 + "2");
alert(1 + 2 + " hello");
alert(" hello" + 1 + 2);
alert("19" < "2");
//7

//8
//question - can you make javascript mimic c#'s startwith function for strings?
String.prototype.startsWith = function(s){ return (this.substr(0, s.length) == s)  }
var test ="test";
alert(test.startsWith("te"));
alert(test.startsWith("ete")); 
//8

//9
//what value is num?  
var num = 34/0;
alert(num);
//9

//10
//will the value be true and why?
//is there any way of doing this?
var num2 = 0/0;
var num3 = 0/0;
alert(num2==num3);
//10

//11
//what will show?
alert("n\n\\n");
//11

//12
//is the if statement true
var num4;
if(num4===null)
  alert("here");
//12

//13
var t   = 0||67;alert(t); 
//13


Monday, March 4, 2013

Science- Fiction & scrum

I was thinking , lets adapt a famous science-fiction trope (cliche?) and see how it would fit scrum.

tell the scrum master that scrum is an impediment...
and watch him explode.