const Vs readonly Keyword and params

const-:

  • A constant is a data member which is evaluated at Compile time.
  • const is implicitly static.

readonly-:

  • readonly data member is evaluated at run-time in its declaration or in constructor.
  • it is not implicitly “Static” but can be “Static” or as per Instance.
  • If we explicitly make “read-only” field static and know its value at compile time then that field works as const.

e.g.

// Same as public const double PI = 3.14;

public static readonly double PI = 3.14; 

params-:

  •  params is mainly used to declare variable number of arguments inside the method called the parameter array.
  • Also the argument with Prefix params must be the last argument inside the method.

Example-:

public int Sum(params int[] arr)

{

int sum=0;

foreach(int i in arr)

{

sum=sum+i;

}

return sum;

}

public static void main()

{

int sum = Sum(5,10,15);

Console.WriteLine(“Sum is {0}”,sum);

}

base Vs. this Keyword.

“base” and “this” keywords are conceptually quite close to each other and some time can create ambiguity. Yet they have different functionality.”

“base”-:

“base” keyword is used to access class members that are hidden by current class which inherits it.

Example-:

class Parent

{

string parent;

public virtual void BaseDemo()

{

Console.WriteLine(“I am from Parent Class”);

}

}

class Child: Parent

{

public override void BaseDemo()

{

base.BaseDemo();

Console.WriteLine(“I am from Child Class”);

}

}

O/P-: 

I am from Parent Class.

I am from Child Class.

“this”-:

  • It is accessible only from non-static methods.
  • It is used to differentiate between Data and Function Members.
  • Refers the current instance of the class.

Example-:

Class Person

{

string name;

public Person(string name)

{

this.name = name; //”this.name” here is data member of the class.

Console.WriteLine(this.name);

}

Class Program

{

public static void Main()

{

Person p = new Person(“DataMember”);

}

}

Class Vs. Struct

Classes are reference types whereas Struct are Value Types. All structs are implicitly sealed i.e. they can’t be inherited. struct doesn’t requires constructor where as a class does. strcut variables can’t specify initializers whereas class variable can.

Their differences are common, But there’s again some difference between C++ structs and C# strcuts.

  1. In C++ all members of a struct should be public, whereas in C# members can be public, private or internal.
  2. In C++ struct can be allocated on the Heap,Stack or as a member. It can be used both as a Value or Reference. whereas in C# it always be allocated on a stack because it is a value Type in C#.

Why Seal a Class??

  • Once a class is been Sealed it can’t be derived, thus prevent Unintended Derivation of the class.
  • Sealing a class also helps in Code Optimization.
Value Types and Reference Types in C-Sharp.

Value Types and Reference Types in C-Sharp.

Tags: Type C sharp

Basic C-Sharp cont…

More On Interfaces-:

  • An Interface can inherit from multiple Interfaces.

Example-:

interface IRect:IShape,IColor

{

……….

}

  • If Two Interfaces shares same methods then we need to explicitly define name of the interface before method name from which it come.

Example-:

interface IShape

{

void Create()

}

interface IColor

{

void Create()

}

interface IRectangle: IShape,IColor

{

void IShape:Create();

void IColor:Create();

}

Some Basic C-Sharp Stuff.

I was skimming basics of C-sharp. So just blogging hope it helps…

Objects-:

Object of a class is sometime also been referred as an Instance of a class and class is then said to be instantiated.

Every Object/Instance has its unique identity i.e. each time we create an object a new memory space is been allocated to it.

Object is a good example of “Abstraction”. By creating an object of a class(Non-Static) one can use all the functionality of the class. Thus without knowing the implementation details of the class, methods, data members of the class can be used.This can also be termed as Information Hiding.

Interfaces-:

  • An Interface is an Type(Reference Type like class). Class is an implementation of that Type.
  • Its a contract for the class.
  • Interface can consist of set of Function Members,Properties,Indexers,events etc.
  • They can’t contain Data Members.
  • Any Class that implements Interface must support/implements all of its members.
  • Interface are implicitly Public and abstract. Thus can’t be instantiated.
  • Interface provide Polymorphism. As more than one class implements an interface so more than one class can use the functionality of single contract which is an example of Polymorphism.

Example-:

public interface Shape

{

void Create();

}

class Rectangle : Shape

{

public void Create()

{

Console.WriteLine(“Inside Rectangle”);

}

}

class Circle : Shape

{

public void Create()

{

Console.WriteLine(“Inside Circle”);

}

}

class Program

{

static void Main(string[] args)

{

Rectangle rect = new Rectangle();

Circle circ = new Circle();

Shape s = rect; // Shape s = new Shape()— This Is Error.

s.Create();

s = circ;

s.Create();

}

}

StreamReader Vs StringReader & StringBuilder

Though its a common concept but important at the same time.

StreamReader and StringReader both implemts functionality of TextReader.Though StreamReader is a way to read streams i.e. whole file from a given path whereas StringReader read string by string from a file.

Example-:

TextReader ts = new TextReader();

ts = new StreamReader(fileName);

ts = new StringReader(string);

So from a directory to open a file StreamReader can be used and if File Exists at a given path StringReader can be used to read strings from the file.

Now What is StringBuilder() class. Well StringBuilder has methods like Append(),AppendLine(),Insert() to Modify and build the string. A string can easiy me modified and build again using StringBuilder().

Example-:

string[] sr = {“Item-1”,”item-2”,”item-3”};

StringBuilder sb = new StringBuilder();

foreach(string s in sr)

{

sb.Append(s); //sb.Append(s).AppendLine()  This will produce the same o/p. 

sb.AppendLine(); // appends newline Character..

}