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

Monday, July 8, 2013

.Net Interview

Interviewing candidate for a mid level requires lot of scrutiny to get right candidate.  I had always had list of areas I would like to cover the depth in the area is depends on the candidates interest and experience, but I would like always make sure to know his breadth.  Here is my notes (Crude copy paste from One Note)

  1. OOPs
    1. Delegates / Extended Methods
    2. Threading (Pool / TPL / Native) 
    3. Could not differentiate Static and Dynamic poly
    4. Abstract and Interface
    5. Abstract Factory
    6. Virtual, Override
    7. Partial class
  2. .Net basics
    1. Reference type, value type
    2. Call by value and call by reference
    3. Why Reflections / Why Garbage Collection & How
  3. Collections
    1. Stack, Queue
    2. Array, Array List
    3. Hash table, Dictionary
  4. Build & Deployment

Debugging

  1. When ASP.Net page is slow how do you respond?

Technology Area

  1. ASP.Net (Single sign-on)
      • Cache, Paging
      • Session
      • Application - Global.asax
      • Tracing
      • Error handling
      • Profiling
      • AJAX
      • Configuration
      • HTTPModule & HTTPHandler
      • ASP.Net Pipeline
      • User control, Customer control
      • Authentication, Authorization
      • Member provider
      • Data Binding
  2. MVC / MVVM
  3. Entity Framework / ORM / LINQ
  4. WCF (RIA, OData) / WPF / WF / SL (MEF, Prism)
  5. Design Patterns
  6. SQL (SSRS, SSIS, SSAS, TSQL)
  7. SharePoint (Customization, Performance Point, Power View, Power Pivot)

Designing Skills

  • Consider you are building simple Reporting solution to where you need to show data from DB to an UI page.  What are you primary question to estimate & design this app?
    • Number of Reports
    • Size of data
    • Number of users
    • Authentication
    • Performance needs, Frequency of data change and Caching
    • SP vs Dynamic SQL
    • AJAX & Pagination

Technical Leadership

  • Code Review
    • Readability
    • Each method one action
    • Input validation
    • Error Handling
    • Performance & Caching
    • View State
  • Estimation
  • Task allocation and tracking

Tools

  • Fiddler
  • FxCop
  • Beyond Compare

Logical Thinking

  • Designing lift logic
  • Designing Railway platform logic
  • Representing hierarchy using Array
  • Thread Synchronization
  • Tic-Tac -To who to create next move based on current state
    • How to you store the state
    • Compare two sets
  • Process communication

Wednesday, September 26, 2012

C# Virtual & Abstract Properties

Recently a new learner of C# asked me a question on C# Virtual & Abstract properties.  It turned out to a deep dive.  Detail follows.

Difference between the Abstract and Virtual property

* Abstract properties forces you to make the class abstract,
  no such constraint for virtual properties.  Virtual provides
  an offer to subclass to override.
* Abstract properties defer the implementation to subclass. 
  In Virtual property a default implementation can be provided..

Let us deep dive on write only virtual property
-------------------------------------------------------------

To make a property as write only

Option 1: You can mark the getter as private
          public virtual string Prop1 { set; private get; }

Option 2: You can just have setter

   > in case of explicit implemented properties,
     it is legal to just have Setter

        /////////////////////////////////////////////////////
        string Field;
        public virtual string Prop1 { set { Field = value; } }

        /////////////////////////////////////////////////////

   > in case of Automatically implemented properties, you must have getter
      because if you have just the setter, there is no way you can
      access the value. (Check compiler error below)

        /////////////////////////////////////////////////////
        public virtual string Prop1 { set; }
        /////////////////////////////////////////////////////

Error:
'ConsoleApplication1.test.MyVirtaulWriteOnlyProperty1.set' must declare a body
because it is not marked abstract or extern. Automatically implemented properties
must define both get and set accessors.

Detail:
So for virtual write only property, you must explicitly have setter implemented.
if you are using Automatically implemented property it must have both get & set

In contrary write only abstract property allows setter only property.
It is because, in abstract the implementation is deferred, in the subclass
the compiler will force you to implement the setter explicitly in subclass.

        /////////////////////////////////////////////////////
    abstract class Animal
    {
            public abstract string Prop1 { set; }
    }

        /////////////////////////////////////////////////////

Attempt - 1

        /////////////////////////////////////////////////////
    public class Dog : Animal
    {
            public override string Prop1 {set;}
    }

        /////////////////////////////////////////////////////

Errror
'ConsoleApplication1.Dog.Prop1.set' must declare a body
because it is not marked abstract or extern.
Automatically implemented properties must define both get and set accessors.   

Detail:
The reason is there is no way you can read the value,
so compiler forces us to add both get, set.

Attempt - 2
        /////////////////////////////////////////////////////
    public class Dog : Animal
    {
            public override string Prop1 {get;set;}
    }

        /////////////////////////////////////////////////////

Error:
'ConsoleApplication1.Dog.Prop1.get': cannot override
because 'ConsoleApplication1.Animal.Prop1'
does not have an overridable get accessor
   
Detail:
There is no getter defined in suberclass, so we cannot define it in subclass. 
We cannot change the write only property as read/write property in subclass

Attempt - 3: (Correct implementation)
        /////////////////////////////////////////////////////
    public class Dog : Animal
    {
           string field;
           public override string Prop1
           {
              set { field = value; }
           }
    }

        /////////////////////////////////////////////////////

Error: None
Detail:
So the only way out is implement the Write only property as write only explicitly.

I hope this helps to understand the write only property behavior in reference to Abstract & Virtual.

Wednesday, May 9, 2012

Run-time assembly creation

There are times where you may need to generate Assembly dynamically at run-time.  The blog post shares some code for the same.

public CompilerResults Compile()
{
//Set the compiler options
var compilerOptions = new Dictionary<string, string>();
compilerOptions.Add("CompilerVersion", "v4.0");

CodeDomProvider compiler = new CSharpCodeProvider(compilerOptions);
CompilerParameters parameters = new CompilerParameters();
parameters.WarningLevel = 4;
parameters.TreatWarningsAsErrors = false;
parameters.GenerateExecutable = false;
parameters.GenerateInMemory = false;
parameters.OutputAssembly = GetFileName();
parameters.IncludeDebugInformation = false;
if (RoleEnvironment.IsAvailable)
parameters.TempFiles = new TempFileCollection(RoleEnvironment.GetLocalResource("CompilerTempFiles").RootPath, false);

string baseDir = Path.GetDirectoryName(
Assembly.GetExecutingAssembly().Location);
baseDir = baseDir.Substring(0,
baseDir.LastIndexOf(Path.DirectorySeparatorChar));
baseDir = baseDir.Substring(0,
baseDir.LastIndexOf(Path.DirectorySeparatorChar));

//Add required assembly references of your code
string assemblyName;
foreach (string assembly in GetReferences())
{
string suffix = assembly.EndsWith(".exe") ? "" : ".dll";

assemblyName = assembly;
if (!assembly.StartsWith("System"))
{
if (assembly.IndexOf(".dll") > -1)
{
assemblyName = FindAssembly(baseDir, assembly);
suffix = "";
}
parameters.ReferencedAssemblies.Add(assemblyName + suffix);
}
else
{
var assemblyPath = (from assm
in AppDomain.CurrentDomain.GetAssemblies()
where assm.FullName.IndexOf(assemblyName + ",") > -1
select assm).FirstOrDefault();

if (assemblyPath == null)
{
throw new Exception("Unable to locate assembly: "
+ assemblyName + suffix);
}

parameters.ReferencedAssemblies.Add(assemblyPath.Location);
}
}

//Get your dynamic code
string Code = GetCodeText();

CompilerResults results = compiler.CompileAssemblyFromSource(
parameters, Code);

return results;
}





With dynamic code you must have some known interface, so that you can create the dynamic object of given interface to invoke the code.

public IKnownInterface GetTableServiceContext()
{
string assemblyPath = GetFileName();
if (!File.Exists(assemblyPath))
{
throw new FileNotFoundException(assemblyPath + " not found");
}

System.Reflection.Assembly assem = AppDomain.CurrentDomain.Load(
File.ReadAllBytes(assemblyPath));

Type queryType = assem.GetType("DynamicClassName");

return Activator.CreateInstance(queryType) as IKnownInterface;
}





We should be avoiding dynamic code generation and look for solutions like dependency injection, MEF frameworks.

AI Learning resouces

 Collection of resources aka.ms/genai-beginners  -  github- generative-ai-for-beginners rasbt/LLMs-from-scratch: Implement a ChatGPT-like LL...