RSS

Tag Archives: c#

C# Bit Shorthand

1 << 0; // Result is 1
1 << 1; // Result is 2
1 << 2; // Result is 4
1 << 3; // Result is 8

The way this works is, the binary value of first operand (the left) is shifted by number of bits of the second operand. Something like this:

1 is 00001 in binary which is 1 << 0
2 is 00010 in binary which is 1 << 1
3 is 00100 in binary which is 1 << 2
So on, so forth.

Another way to look at it is "adding X zero in the first operand's binary, where X is second operand.".

The kicker is that, second operand is only the first 5 bits (low-order five bits), means 00000 to 11111 (which is 31)

Reference: MSDN << Operator

Advertisements
 
Leave a comment

Posted by on October 28, 2017 in General

 

Tags:

Indexer-Supported Collection in C#

Indexer is a faster way to search a list / collection in C#, no looping is necessary.

Classes that support indexer:

  • Array []
  • List<T>
  • Dictionary<Key, Value>
  • ILookup<Key, Value>

Classes that don’t support indexer:

  • IEnumerable<T>
  • HashSet<T>
  • ICollection<T>

For classes that don’t support indexer, there’s ElementAt LINQ extension method.

 
Leave a comment

Posted by on September 28, 2017 in General

 

Tags: , ,

Why Must Extension Methods Be Declared in a Static Class?

Some possible answers:

  • The purpose of Extension Methods is to make LINQ work and LINQ only needs extension methods to be in a static, non-generic, non-nested class.
  • Easier for compiler to work with. Compiler and VS’s Intellisense can easily locate the methods.
  • Grouping the helper methods together.

Sources:
https://stackoverflow.com/questions/3930335/why-are-extension-methods-only-allowed-in-non-nested-non-generic-static-class

https://stackoverflow.com/questions/7163823/why-must-c-sharp-extension-methods-be-defined-in-static-classes

 
Leave a comment

Posted by on September 20, 2017 in General

 

Tags: ,

JavaScript Bundle Doesn’t Work on ASP.Net MVC 5

Developers often make this very common mistake. You start an ASP.Net MVC app, add few JavaScript files and libraries, run. Everything works fine but the JavaScript.

ASP.Net MVC Bundling

public static void RegisterBundles(BundleCollection bundles)
{
    bundles.Add(new ScriptBundle("~/bundles/jquery").Include(
                "~/Scripts/jquery-1.*"));

    bundles.Add(new ScriptBundle("~/bundles/startstop").Include(
                "~/Scripts/startstop*"));
}

ASP.Net MVC bundle has default ignore list and one of ignore list is ‘*.min.js’ files. Yes, any minified JavaScript file.

You can add the following code RegisterBundles to clear the ignore list before adding anything to the bundle.

bundles.IgnoreList.Clear();

Once you clear the list, you are also able to build your own ignore list.

bundles.IgnoreList.Ignore("");
 
Leave a comment

Posted by on June 12, 2015 in General

 

Tags: , ,

Bigger Rock, But Still!!!

OK, after the smaller rock, I got a slightly bigger rock now… but I still think it’s a stupid mistake. I hope you guys don’t make the same one.

Instead of a internal setter, I removed the setter at all. Well, it didn’t serialize with my data at all! Everything else came through correctly but the properties without setter.

[DataContract]
public class Employee
{
    [DataMember]
    public int ID { get; }
    [DataMember]
    public string Firstname { get; set; }
}

What basically happens here is WCF creates XML schema for classes it needs to return as a response. The XML schema it creates is similar to C# class, but without C#-specific syntax like access modifier. The purpose of this is so that WCF can serialize the data and serve it as SOAP response, which is XML-based. The creation of the schema, ostensibly, ignores properties without setter.

Slightly bigger rock, but I still got tripped. Oh well..

 
Leave a comment

Posted by on June 10, 2015 in General

 

Tags: , ,

Boxing x Unboxing

Boxing and unboxing is not to be confused with downcast / upcast.

  • Boxing is the process of converting a value type to the type object or to any interface type implemented by this value type.
  • When the CLR boxes a value type, it wraps the value inside a System.Object and stores it on the managed heap.
  • Unboxing extracts the value type from the object.
  • Boxing is implicit
  • Unboxing is explicit.
  • The concept of boxing and unboxing underlies the C# view that a value of any type can be treated as an object.

Example:

Boxing

int i = 123;
// The following line boxes i. 
object o = i;

Unboxing

o = 123;
i = (int)o;  // unboxing

Reference: MSDN

 
Leave a comment

Posted by on June 5, 2015 in General

 

Tags: , , , ,

It’s The Small Rock, Agggh!!

I have this stupid mistake that I hope none of you would ever make.

I am building a WCF service and one of the DTOs is something like this:

[DataContract]
public class Employee
{
    [DataMember]
    public int ID { get; internal set; }
    [DataMember]
    public string Firstname { get; set; }
}

On the ‘other’ side (client), I can’t set my Employee’s ID… Hours of debugging turned to “dooohhhh” moment when I realized the setter was in fact internal. Internal is internal, access modifier that can’t be accessed from outside library. Easily miss the basic when you deal with higher level stuff.

It’s the small, tiny rock that trips you. Aggh!

 
Leave a comment

Posted by on June 4, 2015 in General

 

Tags: ,

 
%d bloggers like this: