Monday, October 5, 2015

Regular expressions to resolve different requirements

Issue 1>
I was having requirement that I want to find all words in two consecutive % characters. Below interesting regular expression I used for that.

%[\w\s\S]*?%

\w for alphabet and underscore
\s for space
\S for special chars
? returns shortest possible match

Edit the% my file % %% %kjghjhgkjg % % (**&*&*&*&*&*&*& % %9089898098% Expression

Highlighted are the matches.

http://regexr.com/3btui see for more details.

The c#.net code used as below to find all these matches.

            MatchCollection allMatchResults = null;
            var regexObj = new Regex(@"%[\w\s\S]*?%");  
            allMatchResults = regexObj.Matches(this.textBox1.Text);

            string output = "";
            foreach (var s in allMatchResults)
            {
                output += s + "\n";
            }

            label1.Text = output;

Friday, August 21, 2015

Getting "Must declare the scalar variable" error while writing SQL statements in script

I was writing bunch of operations in sql, was using SQL Server 2008 R2. I declared one INT variable and was trying to use inside select statement, I was getting error as "Must declare the scalar variable "@numberOfCards"."

SQL scripts looks like something similar to this: -


declare @numberOfCards int;
set @numberOfCards = 3; --some number
;
;
;
GO
select * from CardsRepository where CardsCount = @numberOfCards

Reason of issue:
Culprit was GO statement. The GO command signals the end of a batch of Transact-SQL statements. A local variable is only valid within the body of a batch or procedure.

Solution:

The first option is to remove the GO command between the 2 sets of scripts so that the local variable @numberOfCards is valid and accessible on both scripts. Use semi comma where ever the sql statement ends. 

Wednesday, April 1, 2015

Good serialization deserialization example in c# 4.0 (.Net 4.0)

Code of deserialization


           CarInfoCollection carInfoCollection = null;

            using (StringReader strReader = new StringReader(this.textBox.Text.Trim()))
            {
                using (XmlTextReader reader = new XmlTextReader(strReader))
                {
                    XmlSerializer serializer = new XmlSerializer(typeof(CarInfoCollection));
                    carInfoCollection = (CarInfoCollection)serializer.Deserialize(reader);
                    reader.Close();
                }
            }


            MessageBox.Show(carInfoCollection.CarInfoFields.Count().ToString());

----------------------------------------------------------------------------------------

Type classes

  [System.SerializableAttribute()]
    [XmlRootAttribute("CarInfoFields", Namespace = "urn:carinfo-schema:CarInfoCollection")]
    public class CarInfoCollection
    {
        [XmlElement("CarInfo")]
        public CarInfo[] CarInfoFields { get; set; }
    }

    [System.SerializableAttribute()]
    public class CarInfo
    {
        [XmlAttribute]
        public String Name { get; set; }

        [XmlAttribute]
        public CarUsage Usage { get; set; }

        [XmlArray(ElementName = "Features", IsNullable = true)]
        [XmlArrayItem(ElementName = "Feature", IsNullable = true)]
        public List<string> Features { get; set; }
    }

----------------------------------------------------------------------------------------

Xml string entered in textbox, examples




Serialization Example

When we do not want xml declaration / omit xml declaration

OmitXmlDeclaration should be set to True while initializing XmlWriter. Sample :-

  public string SerializeToXML(T typeToSerialize)
        {
            using (var sw = new StringWriter())
            {
                using (var xw = XmlWriter.Create(sw, new XmlWriterSettings { OmitXmlDeclaration = true }))
                {
                    var serializer = new XmlSerializer(typeof(T));
                    
                    // below code will omit the namespace in serialized xml
                    XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
                    ns.Add("", "");
                    serializer.Serialize(xw, typeToSerialize, ns, encodingStyle: string.Empty);
                }
                return sw.ToString();
            }
        } 

Saturday, December 15, 2012

.Net 4.5 Features, what is new?



  • Cross-Platform Development with the .NET Framework


  • Portable class libraries on multiple platforms, such as Windows 7, Windows 8, Silverlight, Windows Phone, and Xbox 360. When you create a Portable Class Library project, you can choose two or more platforms you want to target. Link: http://msdn.microsoft.com/en-us/library/gg597391.aspx


  • Asynchronous file operation

  • In the .NET Framework 4.5, new asynchronous features were added to the C# and Visual Basic languages. These features add a task-based model for performing asynchronous operations. To use this new model, use the asynchronous methods in the I/O classes. 


  • perform resource-intensive I/O operations without blocking the main thread.
  • async method contains Async in its name such asReadAsync, WriteAsync, CopyToAsync, FlushAsync, ReadLineAsync, and ReadToEndAsync. These async methods are implemented on stream classes, such as Stream,FileStream, and MemoryStream, and on classes that are used for reading from or writing to streams, such TextReader and TextWriter
  • In th .NET Framework 4 and earlier versions, you have to use methods such as BeginRead and EndRead to implement asynchronous I/O operations. These methods are still available in the .NET Framework 4.5 to support legacy code; however, the async methods help you implement asynchronous I/O operations more easily.
  • Keywords – async (mark method which contains async operaion) and await (applies to result of async operation)

  • Ref URL : http://msdn.microsoft.com/en-us/library/kztecsys.aspx



  • Web
  • - Support for reading and writing HTTP requests and responses asynchronously.
    - Support for new HTML5 form types
    - Support for asynchronous modules and handlers.
    Ref URL : http://msdn.microsoft.com/en-us/library/hh420390.aspx



  • CLR and Framework improvements

  • a.    Ability to reduce system restarts by detecting and closing .NET Framework 4 applications during deployment
    b.    Support for arrays that are larger than 2 gigabytes (GB) on 64-bit platforms. This feature can be enabled in the application configuration file. Ref: element
    c.    Better performance through background garbage collection for servers.
    d.    ProfileOptimization - Background just-in-time (JIT) compilation, which is optionally available on multi-core processors to improve application performance. (Profile optimization requires a multicore computer. The methods are ignored on other computers.) Each time you initiate profile optimization in an application domain, the profile that was created during the previous use is read. The information in the profile is used to guide background compilation by identifying the methods that are most likely to be executed during startup. On multicore computers, this increases the chances that a method is already compiled by the time it is needed so that the main application thread does not have to call the JIT compiler.
    e.    Regex.MatchTimeout property - The MatchTimeout property defines the approximate maximum time interval for a Regex instance to execute a single matching operation before the operation times out.
    f.    Ability to define the default culture for an application domain
    g.    Zip compression improvements to reduce the size of a compressed file. See the System.IO.Compression namespace.
    h.    Type reflection support split between Type and TypeInfo classes




  • Improved Garbage Collection


  • Background GC available to server application also in .Net 4.5
    Before version 4, the .NET Framework provided a concurrent GC mode that performed full GCs concurrently with user code (vs. blocking, which pauses all user threads), thus reducing pause time for full GCs.

    In the .NET Framework 4, Microsoft delivered an improved version called the background workstation garbage collection, which reduced latency but only benefited client apps.

    In the .NET Framework 4.5, Microsoft have delivered background server garbage collection, which is typically used for server apps. As a result, all apps now have background GC available to them, regardless of which GC they use.
    This results in much shorter pauses on server.

    Higher throughput
    For server GC, there’s one heap per logical processor.
    When one of the heaps runs out of space for allocations, it triggers a GC. Usually, server apps have a pool of worker threads that perform similar types of tasks, and roughly the same amount of memory is allocated per thread, which results in naturally balanced managed heaps. If this is not the case, and one thread allocates a lot more memory than other threads, the heap that corresponds to its CPU runs out of space to allocate quickly and triggers a GC. GC has a “heap balancing” mechanism to help these apps.

    In the .NET Framework 4 and earlier versions, the small object heap (SOH) was balanced, but the large object heap (LOH) was not, so imbalanced LOH allocations in server GC would trigger more full GCs . In the .NET Framework 4.5, the server GC allocator balances the allocations across the heaps when it finds that GC heaps, including both SOH and LOH, are not balanced. In apps that begin to develop imbalanced heaps, this mechanism eliminates unnecessary GCs. This reduces the total time spent in GC and improves the app throughput.

    GC support for latest hardware
    Starting with Windows 7 and Windows Server 2008 R2, Windows supports more than 64 processors on a single computer. For machines that have very large numbers of processors (more than 64), the operating system splits the processors into multiple processor groups. In the .NET Framework 4.5, the GC can view CPUs across processor groups and takes all the cores into account when creating and balancing heaps.

    You’ll need to enable the element in your app’s configuration file to turn this behavior on. GC automatically detects and enables NUMA support. (NUMA – Non uniform memory access)

    SustainedLowLatency
    Stock market kind of application cannot tolerate pauses.

    In the .NET Framework 4.5, Microsoft has provided the option by introducing SustainedLowLatency mode, which avoids full blocking GCs. This mode is also available for the workstation GC in the .NET Framework 4 via Update 4.0.3. While the SustainedLowLatency setting is in effect, generation 0, generation 1, and background generation 2 collections still occur and do not typically cause noticeable pause times. blocking generation 2 collection happens only if the machine is low in memory or if the app induces a GC by calling GC.Collect().

    In the .NET Framework 4.5, SustainedLowLatency mode is available for both workstation and server GC. To turn it on, set the GCSettings.LatencyMode property to GCLatencyMode.SustainedLowLatency. The .NET Framework 4 includes aLowLatency mode for workstation GC; however, this setting is only intended to be used for short periods of time, whereasSustainedLowLatency mode is intended to be used for much longer.

    Applications work with large datasets where user objects > 2 GB
    In earlier versions of the .NET Framework, the following lines of code throw OutOMemoryException exceptions due to the 2-GB limit on object sizes on the GC heap. In the .NET Framework 4.5, the code succeeds, assuming that there is enough physical memory available on the machine:

    new Object[1000000000]; //(approximate size of this array 8 GB)
    new int[50000,50000]; //(approximate size of this array is 10 GB)
    new Dictionary(1000000000); //(approximate underlying array size is 24 GB)


    Note that the array index is still a 32-bit number. By default, the .NET Framework does not support arrays that are greater than 2 GB, but in the .NET Framework 4.5, you can use the element in your application configuration file to enable arrays that are greater than this size.

    Sunday, June 10, 2012

    Playing with Constructors (c# 4.0) (Examples in VS 2010)

    Constructors are class methods which executes when class object is created. 
    Constructors can be marked as public, private, protected, internal, protected internal, static.
    Classes can have multiple constructors. If we do not specify constructor, c# will create default one.

    Scenario 1: Inheritance and default constructors

    When we inherit base class (Base1) in child class (Base2), and create an object of child class, it executes base class constructor first and child class constructor later. This helps that when child class constructor is executing, system makes sure that base class has already set properties or initializations and then child class can re-set those according to need. Refer below example for this:

    class Base1
    {
        public Base1()
        {   Console.WriteLine("Base 1 .ctor called");
        }
    }
    class Base2 : Base1
    {
        public Base2()
        {   Console.WriteLine("Base 2 .ctor called");
        }
    }

    Actual call is
    Base2 base2Obj = new Base2(); or
    Base1 base1Obj = new Base2();

    Output is

    Base 1 .ctor called
    Base 2 .ctor called

    Even if you write base2 constructor as below, output will be the same as mentioned above.

    public Base2() : base()
    {       
         Console.WriteLine("Base 2 .ctor called");
    }

    Conclusion:

    It always the base class costructor that  get called first. Derived class may not like the way that the base class has been initialized. It can change the initial value of data provided that it has access to do so.


    Sunday, December 4, 2011

    Terminologies used in "Full Text Search"

    Recently I and my Friend Sonali has completed a white paper on full text search engines with detail case study on SQL Server 2008 full text search & Lucene.net

    The important thing to understand in full text search is, being aware of various terminologies used in full text search process. In below paragraph I am going to explain in short about what is full text search and in later paragraphs various terminologies used in the process.
    This information is collected from various websites, from msdn, apache lucene.net formal website etc

    Full Text Search
    In text retrieval, full text search refers to a technique for searching a computer-stored document or database. In a full text search, the search engine examines all of the words in every stored document as it tries to match search words supplied by the user.
    Full text search is often divided into two tasks: indexing and searching.
    The indexing stage will scan the text of all the documents and build a list of search terms, often called an index.
    When a query arrives, either programmatically or as a result of a user request, the full-text engine accesses the sorted and optimized word index to identify which documents contain the requested term(s). The engine creates a list of documents that qualify, typically provided as a list of pointers into the main document index.

    General terms used in full text search engines...

    Saturday, June 18, 2011

    What is Scrum?

    I always hear 'Scrum' and 'Agile' words in my project since my project is following Scrum methodology. I started working in Scrum project, when I was very new to these terminologies. After 3 years, now I realize the importance for Scrum over other software development methodologies.

    Below information I collected from few websites which defines above both things.

    Agile Methodology

    Agile methodology is an approach to project management, typically used in software development. It helps teams respond to the unpredictability of building software through incremental, iterative work cadences, known as sprints.

    Agile development methodology attempts to provide many opportunities to assess the direction of a project throughout the development lifecycle. This is achieved through regular cadences of work, known as sprints or iterations, at the end of which teams must present a shippable increment of work. Thus by focusing on the repetition of abbreviated work cycles as well as the functional product they yield, agile methodology could be described as “iterative” and “incremental"

    Agile methodology emphasizes the communication ,collaboration ,rapid exchange of information , teamwork and what is very important the functioning software and flexibility.

    The results of “inspect-and-adapt” in this methodology to development greatly reduce both development costs and time to market. Because teams can gather requirements at the same time they’re gathering requirements, the phenomenon known as “analysis paralysis” can’t really impede a team from making progress. And because a team’s work cycle is limited to two weeks (or as defined), it gives stakeholders recurring opportunities to calibrate releases for success in the real world.