Thursday 22 December 2016

Distributed Cache Service in SharePoint 2013

The Distributed Cache service, which is built on Windows Server AppFabric Cache, is set to run in a collocated mode on all SharePoint 2013 Servers by default, using up to 10% of the server's total physical memory.

Why Distributed Cache ?

In SharePoint Server 2013, Microsoft managed to dramatically improve performance by storing objects and data across servers to enable the accelerated execution of many different types of requests and operations.



Typically a bottleneck in performance occur while retrieving and writing application data that resides in database and application data in session states. We can use the various levels of caching in SharePoint 2010 to increase the performance of SharePoint applications as written in the previous article. In-memory distributed caching is a form of caching that allows the cache to span multiple servers so that it can grow in size and in transactional capacity.



It's essential for maintaining the large amounts of information on your SharePoint Server, ensuring that the information is fresh and readily available for the end user.
Improving SharePoint 2013 performance relies on the provision of caching functionalities to the following features:
  • Claims-based authentication
  • Newsfeeds, micro blogging and conversations
  • OneNote client access
  • Security trimming
  • Page load performance
  • Caching functionalities, provided by the Distributed Cache service, enable the SharePoint features listed above to quickly retrieve data without any dependency on databases stored in SQL Server, as everything is stored in memory.

    Wednesday 21 December 2016

    DataTypes in C#

    C# is strongly typed language so every variable and object must have a type.
    There are two types of data type in C#
      1.   primitive types (or) predefined
    Ex:  byte, short, int, float, double, long ,char, bool, DateTime, string, object etc..
     2.   non-primitive types (or) User Defined
    Ex: class , struct , enum , interface,  delegate, array.
    In C#, based on what a variable contains there is two types of built-in data type
    Value types
    A variable holds actual values then that type of data types are value types. These value types are stored in “stack” memory and these value types are fixed in size. If you assign a value of a variable to another variable it will create two copies.
    Ex: byte, short, int, float, double, long ,char, bool, DateTime.
    • Primitive data types are value types except string, object.
    • Object type is superior to all types. It can store any type or any size of data. It helps in inheritance process.
    • Struct, enum are value types.
    Reference types
    A variable holds a reference to the value, then that type of data types are reference types. These reference types are stored in “heap” memory and these types are not fixed in size.  They are maintained in system managed heap but it also uses stack to store reference of the heap. Two primitive types (string and object) and non-primitive data types (class, interface & delegate) are examples of reference type.
    Ex:   class, interface, delegate, string, object and array
    Let us learn couple of data types and its uses with example

    Date and Time

    Date time is one of the most commonly used data type in C#, here i am going to explain some of properties about it also.
    Ex:
    DateTime currenttime = DateTime.Now;//display’s current date time. 
    Output:

    int days = DateTime.DaysInMonth(2011, 7);// it displays “31”.  
    Output:

    Common Date Time Properties
    Property          Description
    Date        Gives the date component of the current instance
    Day Gives the day of the month represented by the current instance
    DayOfWeek  Gives the day of the week represented by the current instance
    Hour  Gives the hour component of the date represented by the current instance
    Minute  Gives the minute component of the date represented by the current instance
    Month    Gives the month component of the date represented by the current instance   
    Now  Gives a DateTime object that is set to the current date and time, in the local time zone
    TimeOfDay Gives the time of day for the current instance
    Today Gives the current date
    DaysInMonth  Gives no of Days in that month
    Examples
    DateTime now = DateTime.Now;
    Console.WriteLine("Time:" + now.TimeOfDay);//it display only current time of that day.
    Output:

    Console.WriteLine("Current month: "+now.Month);//it display what is current month.
    Console.WriteLine("To Day is: "+now.DayOfWeek);// it gives current day name.
    Output:

    Common DateTime Arithmetic Methods
    Method          Description
    AddDays    Adds or subtracts the specified number of days
    AddHours Adds or subtracts the specified number of hours
    AddMinutes  Adds or subtracts the specified number of minutes
    AddMonths Adds or subtracts the specified number of months
    AddYears Adds or subtracts the specified number of years
    Examples
    DateTime myDateTime = DateTime.Parse("7/28/2011 10:17:30");
    TimeSpan TimeSpan = new TimeSpan(3, 4, 3, 12);
    DateTime newDateTime = myDateTime + TimeSpan;
    DateTime subtracttime = myDateTime - TimeSpan;
    Console.WriteLine("myDateTime + TimeSpan = " + newDateTime);
    Console.WriteLine("myDateTime - TimeSpan = " + subtracttime);
    Output:

    DateTime now = DateTime.Now;
    DateTime addtwodays = DateTime.Now.AddDays(2);//it adds two days to current date and time.
    DateTime addminutes = DateTime.Now.AddMinutes(50);
    Console.WriteLine("Current DateTime:" + now);
    Console.WriteLine("Addition of two days, DateTime:" + addtwodays);
    Console.WriteLine("adition of minutes, DateTime:" + addminutes);
    Output:

    Common TimeSpan Members
    Name  Description
    Add Adds the specified TimeSpan to the current instance
    Days Gets the days component of the time interval represented by the current TimeSpan
    FromDays Returns a TimeSpan that represents a specified number of days
    FromHours Returns a TimeSpan that represents a specified number of hours
    FromMilliseconds Returns a TimeSpan that represents a specified number of milliseconds
    FromMinutes Returns a TimeSpan that represents a specified number of minutes
    FromSeconds Returns a TimeSpan that represents a specified number of seconds
    Hours  Gets the hours component of the time interval represented by the current TimeSpan
    Milliseconds Gets the milliseconds component of the time interval represented by the current TimeSpan
    Minutes Gets the minutes component of the time interval represented by the current TimeSpan
    Seconds Gets the seconds component of the time interval represented by the current TimeSpan
    Subtract   Subtracts the specified TimeSpan from the current instance
    TotalDays Gets the value of the current TimeSpan expressed as whole and fractional days
    TotalHours Gets the value of the current TimeSpan expressed as whole and fractional hours
    TotalMilliseconds Gets the value of the current TimeSpan expressed as whole and fractional milliseconds
    TotalMinutes Gets the value of the current TimeSpan expressed as whole and fractional minutes
    TotalSeconds Gets the value of the current TimeSpan expressed as whole and fractional seconds
    Example on TimeSpan:
    using System; class timespan {
    public static void Main()
    {
    DateTime time = DateTime.Now;
    TimeSpan TimeSpan = new TimeSpan(24, 00, 00);
    DateTime time24 = time.Subtract(TimeSpan);
    Console.WriteLine("myTimeSpan = " + TimeSpan);
    Console.WriteLine("time before 24 hours = " + time24);
    Console.ReadLine();
    }
    }
    Output

    Integer

    byte ,sbyte, short, ushort, int ,unit, long, ulong
    Name Range
    Sbyte -128 to 127 
    short -32768 to 32767
    int -2147483648 to  2147483647
    long -9223372036854775808 to 9223372036854775807
    byte 0 to 255
    ushort 0 to 65535
    uint 0 to 4294967295
    ulong 0 to 18446744073709551615

    Long Example:

    using System;
    
    class longexample
    
    {
    public static void Main()
    {
    long inches;
    long miles;
    inches = 45789124565654567;
    miles = inches / (5280 * 12); // 12 inches= 1 feet, 5280 feet’s= 1 mile;
    Console.WriteLine(" In miles = " + miles);
    }
    }
    Output:
    In miles= 722681890240

    Byte Example:

    using System;
    
    class byteexample
    
    {
    public static void Main()
    {
    byte t = 1;
    int total = 0;
    for (t = 1; t < 200; t++)
    {
    total = total + t;
    }
    Console.WriteLine(" Total = " + total);
    }
    }
    Output:
    Total= 19900
    The smallest ineger types are byte and sbyte. For small unsigned value use byte , for small signed values use sbyte type to hold value. Whenever if you need integer larger than byte 0r sbyte but smaller than int or uint use short or ushort.

    Floating point:


    The flating point values are initialized as mentioned below, For single precession value use “float “
    float   a=1.1f;
    For more than one precession value use “double”  
    double d=2.12345;
    Name  Range
    float plus & minus 1.5 X 10-45 to ±3.4 X 1038
    double  plus & minus 5.0 X 10-3245 to ±3.4 X 10308
    floating points Example:
    using System;
    
    class floatexample
    
    {
    public static void Main()
    {
    double radius;
    float area = 12.6f;
    radius = Math.Sqrt(area / 3.1416);
    Console.WriteLine("circle radius = " + radius);
    }
    }
    Output :
    circle radius= 2.002672042194

    Boolean type:

    The bool type gives values as true or false. And there is no conversion between integer and bool, we can’t convert ‘1’ to ‘true’ or “0” to “false”.
    Boolean Example:
    using System;
    
    class integerexample
    
    {
    public static void Main()
    {
    bool a;
    a = true;
    if (a)
    {
    Console.WriteLine("a=" + a);
    }
    else
    {
    Console.WriteLine("a is false");
    }
    }
    } 
    Out put :
    a= true;

    String:

    A string is a set of characters enclosed by double quotes, the fact that a string is immutable. If we once created sequence of characters to string then cannot be altered. This feature allows strings to be implemented more efficiently. And unused string objects are collected automatically by  Garbage collector.
    using System;
    
    class stringexample
    
    {
    public static void Main()
    {
    string str1 = "C# makes strings easy.";
    string str2 = str1.Substring(2, 12);
    Console.WriteLine("sub string is " + str2);
    }
    }
    Output:

    String Methods

    The string type,  includes several methods. There are methods, for example, for formatting, concatenating, and comparing strings.
    String Format() Example:
    Format string means arranging the data in the human readable form and easily understandable. For example {0} indicates arg0,{1} indicates arg1 and so on . During the execution when format is encountered in the format of string the corresponding argument is substituted and displayed.
    using System;
    
    class timespan
    
    {
    public static void Main()
    {
    string text;
    string firstName = "fname";
    string lastName = "lname";
    text = string.Format("Your firstname ={0}, LastName {1}.", firstName, lastName);
    System.Console.WriteLine(text);
    }
    }
    Output:


    Concatenating strings
    There are two ways to concatenate(join together) two or more strings. First using the + operator and various concatenation methods defined by String. The method that performs concatenation is called Concat(). One of its simplest form is showing below
    public static void string.Concat( string str0, string str1)
    this method  returns a string that contains str1 concated to the end of str0.
    Concat example:
    using System;
    
    class timespan
    
    {
    public static void Main()
    {
    string text;
    string firstName = "Laxmi";
    string lastName = "Narayana";

    text = string.Concat(firstName, lastName);
    System.Console.WriteLine("Full Name:" + text);
    }
    }
    Output:

    Compare Statement
    static int   string.Compare( string strA, string strB)
    If strA is less than strB then Compare() method  returns zero, if strA is greater than strB returns greater than zero, if  the strings are equal returns zero.
    Compare method example:
    using System;
    
    class timespan
    
    {
    public static void Main()
    {
    string str1 = "Maruthi";
    string str2 = "Narayana";
    // String comparison in which case matters.
    int result = string.Compare(str1, str2);
    if (result < 0)
    Console.WriteLine(str1 + " is less than " + str2);
    else if (result > 0)
    Console.WriteLine(str1 + " is greater than " + str2);
    else
    Console.WriteLine(str1 + " equals " + str2);

    }
    }
    Output:

    Escape characters

    Escape character is used to store some special character in the variable or print on the screen. In C#, escape character is “\” (backslash)
    • \’ is used for Single quotation mark 
    •  \” is used for Double quotation mark 
    •  \\ is used for a single Backslash 
    •  \0 is used for Null 
    •  \b is used for Backspace 
    •  \f is used for Form feed
    •  \n is used for Newline 
    •  \r is used for Carriage return 
    •  \t is used for Tab character

    Wednesday 14 December 2016

    Differences between views and temp tables

    In a relational database like SQL Server, views provide a way of giving users a way to work with specific portions of a larger schema. Temporary tables are another means of providing end users with a subset or collation of data from base tables, and at first glance it seems that these are similar methods of achieving the same end. So it is important to understand what the difference is between a view and a temp table.

    A temp table is a base table that is not stored in the database. Instead it only exists while the database session remains active, and it must be populated each session with data using SQL INSERT commands. Similarly, a view is not stored with data but with a query that will retrieve data. However, views exist only for a single query, and each time you generate a view it is recreated from current data. In contrast, a temp table exists for the entire database session, and once populated it retains those records until the session ends.

    Therefore, the data in a view is always current because it is generated dynamically, whereas the data in a temp table reflects the state of the database at the time it was populated. Because temp tables are actually base tables, they can be updated in the same way as a base table, but only views that meet certain criteria can be used for DML operations.

    The key determining factor in choosing between a view or a temp table is performance. If you are going to use data only once, then a view will perform better than a temp table because you don't need to create and populate the base structure, plus a view will guarantee that the results returned are always current. But if you are going to use data repeatedly during a database session then a temp table provides better performance because it only needs to be created once. As always, a trade-off is involved, and you need to be aware of the risk of outdated data with a temp table, and the restrictions on views for manipulating data.

    What are extension methods in C#

    What are extension methods?

    Extension methods are a new feature in C# 3.0. An extension method enables us to add methods to existing types without creating a new derived type, recompiling, or modify the original types. We can say that it extends the functionality of an existing type in .NET. An extension method is a static method to the existing static class. We call an extension method in the same general way; there is no difference in calling.

    Extension methods enable you to add methods to existing types without creating a new derived type, recompiling, or otherwise modifying the original type.

    An extension method is a special kind of static method, but they are called as if they were instance methods on the extended type.

    How to use extension methods?

    An extension method is a static method of a static class, where the "this" modifier is applied to the first parameter. The type of the first parameter will be the type that is extended.

    Extension methods are only in scope when you explicitly import the namespace into your source code with a using directive.


    The following list contains basic features and properties of extension methods:
    1. It is a static method.
    2. It must be located in a static class.
    3. It uses the "this" keyword as the first parameter with a type in .NET and this method will be called by a given type instance on the client side.
    4. It also shown by VS intellisense. When we press the dot (.) after a type instance, then it comes in VS intellisense.
    5. An extension method should be in the same namespace as it is used or you need to import the namespace of the class by a using statement.
    6. You can give any name for the class that has an extension method but the class should be static.
    7. If you want to add new methods to a type and you don't have the source code for it, then the solution is to use and implement extension methods of that type.
    8. If you create extension methods that have the same signature methods as the type you are extending, then the extension methods will never be called.




    Benefits of extension methods
    • Extension methods allow existing classes to be extended without relying on inheritance or having to change the class's source code.
    • If the class is sealed than there in no concept of extending its functionality. For this a new concept is introduced, in other words extension methods.
    • This feature is important for all developers, especially if you would like to use the dynamism of the C# enhancements in your class's design.
    More code snippets of extension expansion methods
    using
    System;
    using
    System.Text;
    namespace
    ExtensionMethod2
    {
        public static class ExtMetClass
        {
            public static int IntegerExtension(this string str)
            {
                return Int32.Parse(str);
            }
        }

        class Program
        {
            static void Main(string[] args)
            {
                string str = "123456";
                int num = str.IntegerExtension();
                Console.WriteLine("The output using extension method: {0}", num);
                Console.ReadLine();
            }
        }
    }


    In the preceding program we have used an extension method IntegerExtension() to convert a string to a numeric type.

    Important points for the use of extension methods:

    • An extension method must be defined in a top-level static class.
    • An extension method with the same name and signature as an instance method will not be called.
    • Extension methods cannot be used to override existing methods.
    • The concept of extension methods cannot be applied to fields, properties or events.
    • Overuse of extension methods is not a good style of programming.

    Difference between data encapsulation vs abstraction

    Encapsulation and abstraction both solve same problem: Complexity; but in different dimensions.

    Abstraction is virtual class design.
    Before actually defining class, developer will think about what all properties,methods and event will be there in my class.At the time of class definition ,developer will think about which should display to end user and which should not
    Whereas,encapsulation is data hiding.


    Encapsulation is a mechanism by which you restrict the access to some of the object's components, as well as binding the data and methods operating on the data.

    Now if we consider a laptop, as an end user I have access only to some features of the  system. So I could use the mouse to move the cursor, or the keyboard for typing text, but I would not have access to the internal components of the laptop. Again the keyboard in turn is bound internally to a set of methods that operate in response to a user action or an event.

    Abstraction is the ability to define an object that can represent abstract entities which can work, change state and communicate with other entities.

    Let us take the example of our laptop Keyboard itself, here we have a number of Keys, each performing some function dependent on the value given. Now all keys have a certain value, that is accepted by the CPU when you press it. So we create a common object called Key with following methods.
    1. class Key
    2. {
    3. String keyValue;
    4. }
    5. class KeyBoard
    6. {
    7. public void pressKeyDown(Key k)
    8. {
    9. if(k.keyValue==X)
    10. // Do something
    11. }
    12. }


    Now we could create Objects of class Key and pass the definition as shown.

    1. Key enter=new Key();
    2. enter.keyValue="Enter";
    3. Key shift=new Key();
    4. shift.keyValue="Shift";
    5. KeyBoard.pressKeyDown(enter);
    6. KeyBoard.pressKeyDown(shift);


    Here the classes Key and Keyboard are abstractions used in place of an actual Key and Keyboard.

    Friday 9 December 2016

    Static in C#

    You are relaxing on a tropical island. No one else is there. A bird flies by in the sky. The island is static in memory—only one instance exists.

    Many locations exist in computer memory. A static thing stands alone. It occupies just one location. Static is used on methods, classes and constructors.

    Static denotes things that are singular. They are part of no instance. Static often improves performance, but makes programs less flexible.


    Static methods. These are called with the type name. No instance is required—this makes them slightly faster. Static methods can be public or private.

    Static methods use the static keyword, usually as the first keyword or the second keyword after public.
    Warning:A static method cannot access non-static class level members. It has no "this" pointer.
    Instance:An instance method can access those members, but must be called through an instantiated object. This adds indirection.













    Output :