Posts

Showing posts with the label C#

Adding an Obsolete Attribute to Class Methods in C#

As a developer we normally maintain our own framework, our own abstraction of things, be it database wrappers, our own implementations of persistence managers, in some cases we figure out a better way or new way of doing things so we decide to include it in the framework, how do we now tell other developers to use this instead of the old one? The answer is the Obsolete Attribute. 1 2 3 4 5 6 7 8 9 [Obsolete("ExpandoObject.CreateObject is deprecated, please use NameValueCollection.CreateObject instead.", true)] public static void CreateObject ( this ExpandoObject that, NameValueCollection querystring) { querystring.AllKeys.ToList().ForEach(x => { if (!that.ToDictionary(y => y.Key).ContainsKey(x)) ((IDictionary< string , object >)that)[x] = querystring[x]; }); } We simply add this attribute on top of the method or class and specify our message. BTW, the Boolean parameter if set to True, will throw and exceptio...

Automatically Discover and Assign Parameter with Values to a Stored Procedure Call in C#

I'm sure most programmers have run across a situation wherein they have to pass  a lot of parameters to a stored procedure call in C#. Instead of manually typing each parameter in and assigning a value, why not develop a way to automatically discover the parameter, add it to the parameter list of the command object and assign the corresponding value from an entity or model. Here's my implementation using extension method. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 /// <summary> /// Automatically assigns parameters to Command.Parameter Collection from List of parameter names /// </summary> /// <param name="that">Command object being extended</param> /// <param name="model">Expando model</param> /// <param name="conn">sQLConnection used to g...

Var vs Dynamic what's the difference?

Here's a quick look: var dynamic Introduced in  C# 3.0 Introduced in  C# 4.0 Statically typed  – This means the type of variable declared is decided by the compiler at compile time. Dynamically typed  - This means the type of variable declared is decided by the compiler at runtime time. Need  to initialize at the time of declaration. e.g.,  var str=”I am a string”; Looking at the value assigned to the variable  str , the compiler will treat the variable  str  as string. No   need  to initialize at the time of declaration. e.g.,  dynamic str; str=”I am a string”;  //Works fine and compiles str=2;  //Works fine and compiles Errors are caught at compile time. Since the compiler knows about the type and the methods and properties of the type at the compile time itself Errors are caught at runtime Since the compiler comes to about the type and the methods and properties of the type at the run time. Visual ...

Concordance Algorithm

Image
I was given this test before in one of my Job Interviews will not name the company. I'll show you how I tackled the problem. "Given an arbitrary text document written in English, write a program that will generate a concordance, i.e. an alphabetical list of all word occurrences, labeled with word frequencies. Bonus: label each word with the sentence numbers in which each occurrence appeared." For example, this is a concordance of the above text: a. a {2:1,1} b. all {1:1} c. alphabetical {1:1} d. an {2:1,1} e. appeared {1:2} f. arbitrary {1:1} g. bonus {1:2} h. concordance {1:1} i. document {1:1} j. each {1:2} k. english {1:1} l. frequencies {1:1} m. generate {1:1} n. given {1:1} o. i.e. {1:1} p. in {2:1,2} q. it {1:2} r. label ...

.Net Generics

Let's talk about .Net Generics, what exactly is Generics in .Net. Generics was first introduced in .Net Framework 2.0. What is Generics? "Generics allows you to to create type safe class without committing to the actual class" say that again? It only means that you may create a class that can be used with different types.

A quick look at the LINQ Architecture

Image
LINQ is a new feature in Visual Studio 2008, extending the capabilities of C# and VB. We can use Language Integrated Query in querying collections, objects, xml and entities. For as long as an object derives from IEnumerable it can be queried using LINQ, LINQ can be used in the following ways. LINQ to SQL LINQ to XML LINQ to Object LINQ to Entities What does LINQ do? In LINQ to SQL it converts objects queries to SQL Command and when it receives the resultset from the database it then converts it back to objects. Below is an illustration of the LINQ Architecture.

Jump Starting LINQ

I'll be dedicating two weeks to study LINQ using C# and VS 2008, I'll be using the book titled "LINQ Quickly, A Practical Guide to Programming Language Integrated Query with C#" by N. Satheesh Kumar. I'll be discussing each chapter of the book and cite some of my own examples. Just to give you an overview of what I'm currently engaged in, I'm doing this web development project using ASP.Net and C#, it's an Inventory and Sales System for an importer and assembler of MAN buses here in the Philippines. Our team is composed of three developers including myself and a PM, the duration of the project is within six months period.