Pages

Friday, April 23, 2010

C# Questions

struct vs Class
  • structs are value types that can contain data and functions
  • structs are value types and do not require heap allocation.
  • structs directly store their data in the struct, classes store a reference to a dynamically allocated object.
  • structs are useful for small data structures
  • structs can affect performance
  • Constructors are invoked with the new operator, but that does not allocate memory on the heap
  • A struct constructor simply returns the struct value itself (typically in a temporary location on the stack), and this value is then copied as necessary
  • With classes, multiple variables may have a reference to the same object
  • It is possible for operations on one variable to affect the object referenced by the other variable.
  • With structs, the variables each have their own copy of the data, and it is not possible for operations on one to affect the other.
  • structs do not support user-specified inheritance, and they implicitly inherit from type object
Public struct PointStruct
{
 public int x;
 public int y;

 public PointStruct(int x, int y)
 {
  this.x = x;
  this.y = y;
 }
}

static void Main(string[] args)
{
 PointStruct ps = new PointStruct();
 ps.x = 10;
 ps.y = 10;
 Console.WriteLine(“Initial struct values are “ + ps.x +”, “+ ps.y);
 ModifyStructPoint(ps);
 Console.WriteLine(“After ModifyStructPoint, struct values are “ + ps.x +”, “+ ps.y);
}

void ModifyStructPoint(PointStruct newStruct)
{
 newStruct.x = 20;
 newStruct.y = 20;
 Console.WriteLine(“Inside ModifyStructPoint()”);
 Console.WriteLine(“Modified values are “ + newStruct.x +”, “+ newStruct.y);
}

Initial struct values are 10,10
Inside ModifyStructPoint()
Modified values are 20,20
After ModifyStructPoint, struct values are 10,10
class PointClass
{
 public int x;
 public int y;

 public PointClass(int x, int y)
 {
 this.x = x;
 this.y = y;
 }
}

static void Main(string[] args)
{
 PointClass pc = new PointClass(10,10);
 Console.WriteLine(“Initial class values are “ + pc.x +”, “+ pc.y);
 ModifyClassPoint(pc);
 Console.WriteLine(“After ModifyClassPoint, class are “ + ps.x +”, “+ ps.y);
}

void ModifyClassPoint(PointStruct newPoint)
{
 newPoint.x = 20;
 newPoint y = 20;
 Console.WriteLine(“Inside ModifyClassPoint()”);
 Console.WriteLine(“Modified values are “ + newPoint.x +”, “+ newPoint.y);
}

Initial class values are 10,10
Inside ModifyClassPoint()
Modified values are 20,20
After ModifyClassPoint, struct values are 20,20


Parse XML Files
  • Reference System.Xml 
  • Use XmlReader to parse the text string 
  • XmlReader methods to read the XML data
String xmlString = @”<bookstore><book genre=“autobiography” publications=“…” isbn=“…”><title>this is test</title><author><first>…</first><last>…</last></author></book></bookstroe>”

StringBuilder output = StringBuilder();
using (XmlReader reader = XmlReader.Create(new StringReader(xmlString))
{
 reader.ReadToFollowing(“book”); //read the first element
 reader.MovetoFirstAttribute();
 string genre = reader.value();
 reader.ReadToFollowing(“tittle”) //autobiography
 string titlle = reader.ReadElementContentAsString() //this is test
}

Difference between String and string
  • The string type is a sealed class type that inherits directly from object.
  • Values of the string type can be written as string literals.
  • The keyword string is simply an alias for the predefined class System.String
  • Use string for variable names
  • Use String for class methods and reference
  • Coding conventions state that upper case String should be used for ‘Class methods’
  •  typeof(string)  or typeof(String) //System.String


Calling base Constructor in C#

RegEx
RegEx – is powerful string manipulation tool, super fast and efficient
string blog_content = ".net.net test .net .nettest.net .net"'
RegEx.Matches(blog_content, ".net") //6

Difference Between abstract and virtual functions
  • Abstract methods have no implementation and MUST be overridden.
  • Virtual methods MAY have an implementation but are not required to and then CAN be overridden.
  • You cannot call base.method() for abstract but you can for virtual methods

Difference Between ref and out keyword
They're pretty much the same - the only difference is that a variable you pass as an out parameter doesn't need to be initialized
public void RefOut()
{
 //string outstring = "this is old out string";
 string outstring; // it won't throw error
 outmethod(out outstring);
 //string refstring = "this is old ref string";
 string refstring; // it will throw error, because not initialized
 refmethod(ref refstring);
}

void outmethod(out string outstring)
{
 outstring = "this is new out string";
}

void refmethod(ref string refstring)
{
 refstring = "this is new out string";
}

Enum
public enum AuthMethod
{
 FORMS =1,
 WINDOW =2,
 SINGLE =3
}

AuthMethod amf = AuthMethoD.FORMS;
string str = Enum.GetName(trpeof(AuthMethod),amf);
str = AuthMethod.FORMS.toString();

No comments:

Post a Comment