Skip to main content

Posts

Showing posts from July, 2013

Covariance and Contravariance in C#

Today: what do we mean by “covariance” and “contravariance”? The first thing to understand is that for any two types T and U, exactly one of the following statements is true: T is bigger than U. T is smaller than U. T is equal to U. T is not related to U. For example, consider a type hierarchy consisting of Animal, Mammal, Reptile, Giraffe, Tiger, Snake and Turtle, with the obvious relationships. ( Mammal is a subclass of Animal, etc.) Mammal is a bigger type than Giraffe and smaller thanAnimal, and obviously equal to Mammal. But Mammal is neither bigger than, smaller than, nor equal to Reptile, it’s just different. Why is this relevant? Suppose you have a variable, that is, a storage location. Storage locations in C# all have a type associated with them.  At runtime you can store an object which is an instance of an equal or smaller type in that storage location.  That is, a variable of type Mammal can have an instance of Giraffe stored in it, but no

Cluster Index - Always logical ordering sometimes physical ordering

IF Object_id('T1') is NOT NULL DROP TABLE T1 Go CREATE TABLE T1(Name varchar(20)) Go INSERT T1 (Name) values ('Apple'),('Boy'), ('Dog'),('Elephant') GO SELECT * From T1 CREATE CLUSTERED INDEX CINDX_T1 ON T1(NAME) /* 1 – data page 2 – index page 3 and 4 – text pages 8 – GAM page 9 – SGAM page 10 – IAM page 11 – PFS page */ DBCC IND('SQLCommitted',T1,-1) /* dbcc page ( {‘dbname’ | dbid}, filenum, pagenum [, printopt={0|1|2|3} ]) */ DBCC TRACEON (3604); DBCC PAGE ('SQLCommitted', 1,668, 1) INSERT T1 (Name) values ('Cat') DBCC PAGE ('SQLCommitted', 1,664, 1) DBCC PAGE ('SQLCommitted', 1,664, 2) ALTER TABLE T1 REBUILD DBCC IND('SQLCommitted',T1,-1) DBCC PAGE ('SQLCommitted', 1,664, 1) DBCC PAGE ('SQLCommitted', 1,664, 2)