Showing posts with label C#. Show all posts
Showing posts with label C#. Show all posts

Inheritance - Daily Learning

Tuesday, June 8, 2010

I always wonder that most of the people don't realize the importance of virtual and override words in the function. Everytime I ask, many (8 out of 10) of them tend to answer that virtual function can be overridden by child class. When you don't define base class function as virtual and child class function as override then also it overrides. So here I am going to show the difference with the following example in Console App.

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

namespace Inheritance
{
class Program
{
static void Main(string[] args)
{
CA ca = new CA();
CB cb = new CB();
CA cab = new CB();

ca.fun1();
ca.fun2();
ca.fun3();
ca.fun4();

cb.fun1();
cb.fun2();
cb.fun3();
cb.fun4();

cab.fun1();
cab.fun2();
cab.fun3();
cab.fun4();
Console.ReadLine();
}
}

public class CA
{
public void fun1()
{
Type type = GetType();
Console.WriteLine("CA: fun1");
}
public virtual void fun2()
{
Type type = GetType();
Console.WriteLine("CA: fun2");
}
public virtual void fun3()
{
Type type = GetType();
Console.WriteLine("CA: fun3");
}
public virtual void fun4()
{
Type type = GetType();
Console.WriteLine("CA: fun4");
}
}
public class CB:CA
{
public void fun1()
{
Type type = GetType();
Console.WriteLine("CB: fun1");
}
public void fun2()
{
Type type = GetType();
Console.WriteLine("CB: fun2");
}
public override void fun3()
{
Type type = GetType();
Console.WriteLine("CB: fun3");
}
public new void fun4()
{
Type type = GetType();
Console.WriteLine("CB: fun4");
}
}
}

It's out put is as follow



Labels: , ,

C# -Daily Learning

Friday, June 4, 2010

Difference between ref and out parameter

a ref parameter should be assigned before they are called.
out are assigned by the called function itself.
out parameter is used to return more than one values from a function other than return object.
ref = Reference, when parsed to a method, the method references that original variable and doesnt create another instance. So if you make a change to a var parsed into that method when ref is used, it will affect the calling method

Labels: ,

Learning C# - Everyday learning

Thursday, June 3, 2010

Namespaces can be nested. So I can have one namespace within another.
System is most frequently used namespace
Inside System namespace is class called Console, which contains static methods.

The datatypes are defined as classes, they are equipped with methods. ToString() is a common method for all the datatypes.

We can use pointers in C# but we have to mark the functions that use pointers as 'unsafe'

unsafe static void Main()

To read numbers from console window use Parse method.
 Number = int.Parse(Console.ReadLine());

Formatting numbers
Character Used For
c C Currency values
d D Decimal numbers
e E Scientific numeric display such as 1.45e5
f F Fixed decimal numbers
g G General and most common type of numbers
n N Natural numbers
r R Roundtrip formatting
x X Hexadecimal formatting
p P Percentages
var Distance = 248.38782;
Distance.ToString("E") would result var into 2.483878E+002

var NewColor = 3478;
NewColor.ToString("X") would result var into D96

var HSalary = 22.74121
HSalary.ToString("c") would result var into $22.74

var HoursWorked = 35.5018473;
HoursWorked.ToString("F") would result var into 35.50

Labels: ,