Pages
    Calender
    <<  March 2010  >>
    MoTuWeThFrSaSu
    22232425262728
    1234567
    891011121314
    15161718192021
    22232425262728
    2930311234
    Ajander Singh , Created On 7. March 2009, 05:29

    As you know, a variable of a value type can never be null; it always contains the value type's value itself. In fact, this is why they call value types value types. Unfortunately, there are some scenarios in which this is a problem.

    Examples:

    Case 1: When designing a database, it's possible to define a column's data type to be a 32-bit integer that would map to the FCL's Int32 data type. But a column in a database can indicate that the value is nullable. That is, it is OK to have no value in the row's column. Working with database data by using the Microsoft .NET Framework can be quite difficult because in the common language runtime (CLR), thereis no way to represent an Int32 value as null.

    Case 2:  In Java, the java.util.Date class is a reference type, and therefore, a variable of this type can be set to null. However, in the CLR, a System.DateTime is a value type, and a DateTime variable can never be null. If an application written in Java wants  to communicate a date/time to a Web service running the CLR, there is a problem if the Java application sends null because the CLR has no way to represent this and operate on it.

    To improve this situation, Microsoft added the concept of nullable value types to the CLR. To understand how they work, we first need to look at the System.Nullable<T> class, which is defined in the FCL. Here is the logical representation of how the System.Nullable<T> type is defined:

     using System;
    namespace System
    {
        using System.Globalization;
        using System.Reflection;
        using System.Collections.Generic;
        using System.Runtime.CompilerServices;
        using System.Security;

        // Warning, don't put System.Runtime.Serialization.On*Serializ*Attribute
        // on this class without first fixing ObjectClone::InvokeVtsCallbacks
        // Also, because we have special type system support that says a a boxed Nullable<T>
        // can be used where a boxed<T> is use, Nullable<T> can not implement any intefaces
        // at all (since T may not). Do NOT add any interfaces to Nullable!
        //
        [TypeDependencyAttribute("System.Collections.Generic.NullableComparer`1")]
        [TypeDependencyAttribute("System.Collections.Generic.NullableEqualityComparer`1")]
        [Serializable()]
        public struct Nullable<T> where T : struct
        {
            private bool hasValue;
            internal T value;

            public Nullable(T value)
            {
                this.value = value;
                this.hasValue = true;
            }

            public bool HasValue
            {
                get { return hasValue; }
            }

            public T Value
            {
                get
                {
                    if (!HasValue)
                    {
                        ThrowHelper.ThrowInvalidOperationException(ExceptionResource.InvalidOperation_NoValue);
                    }
                    return value;
                }
            }

            public T GetValueOrDefault()
            {
                return value;
            }

            public T GetValueOrDefault(T defaultValue)
            {
                return HasValue ? value : defaultValue;
            }

            public override bool Equals(object other)
            {
                if (!HasValue)
                    return other == null;
                if (other == null)
                    return false;
                return value.Equals(other);
            }

            public override int GetHashCode()
            {
                return HasValue ? value.GetHashCode() : 0;
            }

            public override string ToString()
            {
                return HasValue ? value.ToString() : "";
            }

            public static implicit operator Nullable<T>(T value)
            {
                return new Nullable<T>(value);
            }

            public static explicit operator T(Nullable<T> value)
            {
                return value.Value;
            }

            // The following already obsoleted methods were removed:
            // public int CompareTo(object other)
            // public int CompareTo(Nullable<T> other)
            // public bool Equals(Nullable<T> other)
            // public static Nullable<T> FromObject(object value)
            // public object ToObject()
            // public string ToString(string format)
            // public string ToString(IFormatProvider provider)
            // public string ToString(string format, IFormatProvider provider)

            // The following newly obsoleted methods were removed:
            // string IFormattable.ToString(string format, IFormatProvider provider)
            // int IComparable.CompareTo(object other)
            // int IComparable<Nullable<T>>.CompareTo(Nullable<T> other)
            // bool IEquatable<Nullable<T>>.Equals(Nullable<T> other)
        }
    }

    As you can see, this class encapsulates the notion of a value type than can also be null. Since Nullable<T>  is itself a value type, instances of it are still fairly lightweight. That is, instances can still be on the stack, and an instance is the same size as the original value type plus the size of a Boolean field. Notice that Nullable's type parameter, T, is constrained to struct. This was done because reference type variables can already be null.

    So now, if you want to use a nullable Int32 in your code, you can write something like this:

    Nullable<Int32> x = 5;
    Nullable<Int32> y = null;
    Console.WriteLine("x: HasValue={0}, Value={1}",
    x.HasValue, x.Value);
    Console.WriteLine("y: HasValue={0} , Value={1}",
    y.HasValue, y.GetValueOrDefault());


    When I compile and run this code, I get the following output:

    x: HasValue=True, Value=5
    y: HasValue=False, Value=0

     

    C#'s Null-Coalescing Operator

    C# has an operator called the null-coalescing operator (??), which takes two operands. If the operand on the left is not null,  the operand's value is returned. If the operand on the left is null,  the value of the right operand is returned. The null-coalescing operator offers a very convenient way to set a variable's default value.

    A cool feature of the null-coalescing operator is that it can be used with reference types as well as nullable value types. Here is some code that demonstrates the use of the null-coalescing operator:

    private static void NullCoalescingOperator() {
        Int32? b = null;
        // The line below is equivalent to:
        // x = (b.HasValue) ? b.Value : 123
        Int32 x = b ?? 123;
        Console.WriteLine(x); // "123"
    }

     



    Be the first to rate this post

    • Currently 0/5 Stars.
    • 1
    • 2
    • 3
    • 4
    • 5

    The CLR has built-in support for nullable value types. This special support is provided for boxing, unboxing, calling GetType, and calling interface methods, and it is given to nullable types to make them fit more seamlessly into the CLR. This also makes them behave more naturally and as most developers would expect. Let's take a closer look at the CLR's special support for nullable types.

    Be the first to rate this post

    • Currently 0/5 Stars.
    • 1
    • 2
    • 3
    • 4
    • 5
    Ajander Singh , Created On 12. November 2008, 20:07

    The tradition of using Constants haven't changed much through the times of C++ and now C#, the implication is also the same. Looking deep into the working of Constatnts in C#, we can summarize that A constant is a symbol that has a never-changing value and when declaring a constant symbol. The value of constant as known as compile time and do not change, the compiler then saves the constant's value in the assembly's metadata. This means that you can define a constant only for t primitive types. In C#, the following types are primitives and can be used to define constants: Boolean, Char, Byte, SByte, Int16, UInt16, Int32, UInt32, Int64, UInt64, Single, Double, Decimal, and String.

    Constants are declared  as a field, using the const keyword before the type of the field. Constant must be initilized as they are declared because a constant are always considered to be part of defining type. Constant can be marked as public, private, protected, internal or protected internal. these access modifiers defines how users of the class can access constant.

    Note: Constant are always considerd to be static memberes not instance members. Definig a constant causes the creation of metadata. 

    Example:

    class Calendar

    {

        const int months = 12;

        const int weeks = 52;

        const int days = 365;

     

        const double daysPerWeek = days / weeks;

        const double daysPerMonth = days / months;

    Constants are accessed as if they were static fields, although they cannot use the static keyword. Expressions that are not contained within the class defining the constant must use the class name, a period, and the name of the constant to access the constant. For example:

    int birthstones = Calendar.months; 

    When code refer to const keyword or constant sysmbol, compiler loop up the sysmbol in the metadata of the assembly that defines the constant, extract the constant's value, and ambed value in the emitted IL code. Because a constant's value is embedded directly in code, constants don't require any memory to be allocated for them at run time. In addition, you can't get the address of a constant and you can't pass a constant by reference. These constraints also mean that constants don't have a good cross-assembly versioning story, so you should use them only when you know that the value of a symbol will never change.



    Currently rated 1.0 by 1 people

    • Currently 1/5 Stars.
    • 1
    • 2
    • 3
    • 4
    • 5

    As developers knows that many arithmetic operations on primitives types could result in an overflow:

    Byte b=100;
    b=(Byte) (b+200);       //b now contains 44 (2c in hex)

    In some cases silent overflow can give undesirable results, and if not detected causes the application to behave in strange and unusual ways. Many languages handles overflows in different ways. Like C and C++ allows overflow and is not considered as an error and allow the value to wrap; allows to application continues running. Microsoft visual basic always considers overflows to be an errors and throws an exception when it detects.

    The common language runtime (CLR) offers IL (Intermediate language) instructions that allow the compiler to choose the desired behavior. as you know the C# compiler generated IL has an add instruction as it the default behavior for C# to generate silent overflow. C# compiler use the /checked+ compiler switch to control the overflow, which tell the C# compiler to uses the safe version of the add with overflow check , add.ovf which will prevent this kind of silent overflow and throw OverflowException if overflow occur.

    C# allows the programmer to decide how overflow should be handled. as you know, By default overflow checking is turned off. this means that the compiler generates IL code by using the versions of the add, subtract, multiply, and conversion instructions that don't include overflow checking. as a result, the code runs faster but developers must be assured that overflows won't occur or that their code is designed to anticipate these overflows.

    If overflow occur, the CLR throws an OverflowException. You should design your application's code to handle this exception. Rather than have overflow checking turned off on or off globally, C# allows this flexibility by offering checked and unchecked operators. Simply checked operator tells the C# compiler to use the safe IL instruction for this operation , and the unchecked use the normal IL instructions (which is the normal behavior in C#).

    Here is an example that use checked operator:

    Byte b=100;
    b=checked((Byte)(b+200));                  //OverflowException is thrown

    If the Byte cast outside the checked operator, the exception wouldn't be occurred.

    b=(Byte)checked((b+200));                 //no OverflowException

    In addition to the checked and unchecked operators, C# also offers checked and unchecked statements. the statements cause all expressions within a block to be checked or unchecked:

    checked{
     Byte b=100;
     b=(Byte((b=200);
    }

    Now, try to turn on the compiler's switch /checked+ switch for debug build. Your application will run slowly because the system will be checking for overflow on any code that you didn't explicitly mark as checked and unchecked. If an exception occurs, you will be able to easily detect and be able to fix the bug in you application.



    Be the first to rate this post

    • Currently 0/5 Stars.
    • 1
    • 2
    • 3
    • 4
    • 5
    Ajander Singh , Created On 5. October 2008, 20:17

    A String represents an immutable ordered set of characters. The String type is derived from Object, making it a reference type, and therefore, String objects (its array of characters) always live in the heap, never on a thread's stack. The String  type also implements several interface (IComparable/ IComparable<String>, ICloneable, IConvertible, IEnumerable/ IEnumerable<Char>, and IEquatable<String>). The String class is sealed no inheritance allowed and string is an alias for System.String in the .NET Framework.

    Once created, a string can never get longer, get shorter, or have any of its characters changed. It allows you  to perform operations on a string without actually changing the string. If you perform a lot of string manipulations, you end up creating a lot of String objects on the heap, which causes more frequent garbage collections, thus hurting your application's performance. To perform a lot of string manipulations efficiently, use the StringBuilder class.


    You can concatenate several strings to form a single string by using the C# + (plus) operator: 

    String  sObj=”Hi” + “  “ + “Gentleman”; 

    In this example all strings are literal strings so C# compiler concatenates them at compile time and end up just one string “Hi Gentleman” in the module's metadata. Using the + (plus)  operator on nonliteral strings causes the concatenation to be performed at run time. To concatenate several strings together at run time, avoid using the + operator as it creates multiple string objects on the garbage-collected heap. Instead, use the System.Text.StringBuilder type.


    Verbatim Strings (“@”)

    C# also offers a special way to declare a string in which all characters between quotes are considered part of the string. These special declarations are called verbatim strings and are typically used when specifying the path of a file or directory or when working with regular expressions. 

    // Specifying the pathname of an application 
    String file = "C:\\Windows\\System32\\pbrush.exe"; 

    // Specifying the pathname of an application by using a verbatim string 
    String file = @"C:\Windows\System32\pbrush.exe";

    The @ symbol before the string tells the compiler that the string is a verbatim string. In effect, this  tells the compiler to treat backslash characters as backslash characters instead of escape characters.



    Currently rated 4.0 by 1 people

    • Currently 4/5 Stars.
    • 1
    • 2
    • 3
    • 4
    • 5