Sunday, December 1, 2013

How object is created using Type/Class? [ClassName] [ObjectName] = new [ClassName()];

From the Book CLR via C#



  1. Calculates the number of bytes required by all instance fields defined in the type and all of its base types up to and including System.Object (which defines no instance fields of its own). Every object on the heap requires some additional members - called the type object pointer and the sync block index - used by the CLR to manage the object. The bytes for these additional members are added to the size of the object.

  2. Allocates memory for the object by allocating the number of bytes required for the specified type from the managed heap; all of these are bytes are then set to Zero (0).

  3. Initializes the object's type object pointer and sync block index members.

  4. The type's instance constructor is called, passing it any arguments specified in the call to new. Most compilers automatically emit code in a constructor to call the base class's constructor. Eventually, System.Object's constructor is called.


After new operator has performed all these operations, it returns a reference to the newly created object.


Hope this answers all of your questions.


difference between regex.match and regex.ismatch

Hi,


What is difference between regex.match and regex.ismatch ?


difference between regex.match and regex.ismatch

Regex.Match will return you a Match class instance that can be analyzed deeply about what is to be matched. But Regex.isMatch only tells you whether the fixed string matches the Regex expression or not.


See the reflected codes of Regex class:


【Regex's Match】



public static Match Match(string input, string pattern)
{
return (new Regex(pattern, RegexOptions.None, true)).Match(input);
}

public static Match Match(string input, string pattern, RegexOptions options)
{
return (new Regex(pattern, options, true)).Match(input);
}

public Match Match(string input)
{
if (input == null)
{
throw new ArgumentNullException("input");
}
return this.Run(false, -1, input, 0, input.Length, (this.UseOptionR() ? input.Length : 0));
}

public Match Match(string input, int startat)
{
if (input == null)
{
throw new ArgumentNullException("input");
}
return this.Run(false, -1, input, 0, input.Length, startat);
}

public Match Match(string input, int beginning, int length)
{
if (input == null)
{
throw new ArgumentNullException("input");
}
return this.Run(false, -1, input, beginning, length, (this.UseOptionR() ? beginning + length : beginning));
}

【Regex's IsMatch】



public static bool IsMatch(string input, string pattern)
{
return (new Regex(pattern, RegexOptions.None, true)).IsMatch(input);
}

public static bool IsMatch(string input, string pattern, RegexOptions options)
{
return (new Regex(pattern, options, true)).IsMatch(input);
}

public bool IsMatch(string input)
{
if (input == null)
{
throw new ArgumentNullException("input");
}
return null == this.Run(true, -1, input, 0, input.Length, (this.UseOptionR() ? input.Length : 0));
}

public bool IsMatch(string input, int startat)
{
if (input == null)
{
throw new ArgumentNullException("input");
}
return null == this.Run(true, -1, input, 0, input.Length, startat);
}



ASP.NET Questions

Other Discussions

FreeRice Donate

Issues to report


How object is created using Type/Class? [ClassName] [ObjectName] = new [ClassName()];


  1. That code will create an object in memory with all the methods and properties defined by the class. The CLR and compiler (as is the nature with C# and object-oriented languages) abstract those complexities in to this simple line so that you can just create an object and get and set properties without having to worry about pointers, memory addresses and the size of various members.

  2. Instantiating the object allocates the memory and calls the constructor.

  3. That is just the notation that equates to allocating memory and calling the constructor.


I think this answers your questions. If you have any more questions, I'd be happy to answer them.




Wasabi Fan



How object is created using Type/Class? [ClassName] [ObjectName] = new [ClassName()];

Here is a simple example.



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
MyClass myclass1 = new MyClass(); // calls the constror with no parameters
string value = "abc";
MyClass myclass2 = new MyClass(value); // calls the constror with one parameters

}
public class MyClass()
{
public MyClass()
{
//this is the consructor with no paraameters
//it has the same name as the class an no return or void
}
public MyClass(string value)
{
//this is the consructor with one paraameters
//it has the same name as the class an no return or void
}
}

}
}





jdweng


difference between regex.match and regex.ismatch

Hi,


What is difference between regex.match and regex.ismatch ?


difference between regex.match and regex.ismatch

Regex.Match will return you a Match class instance that can be analyzed deeply about what is to be matched. But Regex.isMatch only tells you whether the fixed string matches the Regex expression or not.


See the reflected codeso of Regex class:


【Regex's Match】



public static Match Match(string input, string pattern)
{
return (new Regex(pattern, RegexOptions.None, true)).Match(input);
}

public static Match Match(string input, string pattern, RegexOptions options)
{
return (new Regex(pattern, options, true)).Match(input);
}

public Match Match(string input)
{
if (input == null)
{
throw new ArgumentNullException("input");
}
return this.Run(false, -1, input, 0, input.Length, (this.UseOptionR() ? input.Length : 0));
}

public Match Match(string input, int startat)
{
if (input == null)
{
throw new ArgumentNullException("input");
}
return this.Run(false, -1, input, 0, input.Length, startat);
}

public Match Match(string input, int beginning, int length)
{
if (input == null)
{
throw new ArgumentNullException("input");
}
return this.Run(false, -1, input, beginning, length, (this.UseOptionR() ? beginning + length : beginning));
}

【Regex's IsMatch】



public static bool IsMatch(string input, string pattern)
{
return (new Regex(pattern, RegexOptions.None, true)).IsMatch(input);
}

public static bool IsMatch(string input, string pattern, RegexOptions options)
{
return (new Regex(pattern, options, true)).IsMatch(input);
}

public bool IsMatch(string input)
{
if (input == null)
{
throw new ArgumentNullException("input");
}
return null == this.Run(true, -1, input, 0, input.Length, (this.UseOptionR() ? input.Length : 0));
}

public bool IsMatch(string input, int startat)
{
if (input == null)
{
throw new ArgumentNullException("input");
}
return null == this.Run(true, -1, input, 0, input.Length, startat);
}



ASP.NET Questions

Other Discussions

FreeRice Donate

Issues to report