System Helloworldapplication: Using Namespace
System Helloworldapplication: Using Namespace
using System;
namespace HelloWorldApplication
{
class HelloWorld
{
static void Main(string[] args)
{
/* my first program in C# */
Console.WriteLine("Hello World");
Console.ReadKey();
}
}
}
The first line of the program using System; - the using keyword is used to include
the System namespace in the program. A program generally has
multiple using statements.
The next line has the namespace declaration. A namespace is a collection of
classes. The HelloWorldApplication namespace contains the class HelloWorld.
using System;
namespace RectangleApplication
{
class Rectangle
{
// member variables
double length;
double width;
public void Acceptdetails()
{
length = 4.5;
width = 3.5;
}
class ExecuteRectangle
{
static void Main(string[] args)
{
Rectangle r = new Rectangle();
r.Acceptdetails();
r.Display();
Console.ReadLine();
}
}
}
Member Variables
Variables are attributes or data members of a class, used for storing data. In the
preceding
program,
has
two
member
variables
/* decimal */
/* hexadecimal */
/* int */
/* unsigned int */
/* long */
/* unsigned long */
Private access specifier allows a class to hide its member variables and member
functions from other functions and objects. Only functions of the same class can access
its private members. Even an instance of a class cannot access its private members.
Protected access specifier allows a child class to access the member variables and
member functions of its base class. This way it helps in implementing inheritance.
nizovi
We can define class members as static using the static keyword. When we declare a
member of a class as static, it means no matter how many objects of the class are
created, there is only one copy of the static member.
Nasledjivanje
C# does not support multiple inheritance. However, you can use interfaces to
implement multiple inheritance.
polymorphism
The following example shows using function print() to print different data types:
Function overloading
class Printdata
{
void print(int i)
{
Console.WriteLine("Printing int: {0}", i );
}
void print(double f)
{
Console.WriteLine("Printing float: {0}" , f);
}
void print(string s)
{
Console.WriteLine("Printing string: {0}", s);
}
Dynamic Polymorphism
Abstract classes contain abstract methods, which are implemented by the derived
class. The derived classes have more specialized functionality.
When you have a function defined in a class that you want to be implemented in an
inherited class(es), you use virtual functions. The virtual functions could be
implemented differently in different inherited class and the call to these functions will
be decided at runtime.
class Shape
{
protected int width, height;
public Shape( int a=0, int b=0)
{
width = a;
height = b;
}
public virtual int area()
{
Console.WriteLine("Parent class area :");
return 0;
}
}
class Rectangle: Shape
{
public Rectangle( int a=0, int b=0): base(a, b)
{
}
public override int area ()
{
Console.WriteLine("Rectangle class area :");
}
public override int area()
{
Console.WriteLine("Triangle class area :");
return (width * height / 2);
}
}
Operator Overloading
public static Box operator+ (Box b, Box c)
{
Box box = new Box();
box.length = b.length + c.length;
box.breadth = b.breadth + c.breadth;
box.height = b.height + c.height;
return box;
}
interfejsi
public interface ITransactions
{
// interface members
void showTransaction();
double getAmount();
}
#define PI
MatchCollection mc = Regex.Matches(text, expr); //regularni izrazi
namespace FileIOApplication
{
class Program
{
static void Main(string[] args)
{
FileStream F = new FileStream("test.dat", FileMode.OpenOrCreate,
FileAccess.ReadWrite);
for (int i = 1; i <= 20; i++)
{
F.WriteByte((byte)i);
}
F.Position = 0;
for (int i = 0; i <= 20; i++)
{
Console.Write(F.ReadByte() + " ");
}
F.Close();
Console.ReadKey();
}
}
}
-----------------------------------Attributes
AttributeUsage
[AttributeUsage(AttributeTargets.Class |
AttributeTargets.Constructor |
AttributeTargets.Field |
AttributeTargets.Method |
AttributeTargets.Property,
AllowMultiple = true)]
Conditional
public class Myclass
{
[Conditional("DEBUG")]
public static void Message(string msg)
{
Console.WriteLine(msg);
}
}
The parameter iserror, is a Boolean value. If its value is true, the compiler should treat
the use of the item as an error. Default value is false (compiler generates a warning).
public class MyClass
{
[Obsolete("Don't use OldMethod, use NewMethod instead", true)]
static void OldMethod()
{
Console.WriteLine("It is the old method");
}
static void NewMethod()
{
Console.WriteLine("It is the new method");
}
public static void Main()
{
OldMethod();
}
}
properties
using System;
namespace tutorialspoint
{
class Student
{
private string code = "N.A";
private string name = "not known";
private int age = 0;
{
return code;
}
set
{
code = value;
}
}
}
set
{
code = value;
}
}
return "Code = " + Code +", Name = " + Name + ", Age = " + Age;
}
}
return ( tmp );
}
set
{
if( index >= 0 && index <= size-1 )
{
namelist[index] = value;
}
}
}
Console.ReadKey();
}
A delegate is a reference type variable that holds the reference to a method. The
reference can be changed at runtime.
using System;
delegate int NumberChanger(int n);
namespace DelegateAppl
{
class TestDelegate
{
static int num = 10;
public static int AddNum(int p)
{
num += p;
return num;
}
Multicasting of a Delegate
Delegate objects can be composed using the "+" operator. A composed delegate calls
the two delegates it was composed from. Only delegates of the same type can be
composed. The "-" operator can be used to remove a component delegate from a
composed delegate.
using System;
delegate int NumberChanger(int n);
namespace DelegateAppl
{
class TestDelegate
{
static int num = 10;
public static int AddNum(int p)
{
num += p;
return num;
}
//calling multicast
nc(5);
Console.WriteLine("Value of Num: {0}", getNum());
Console.ReadKey();
}
}
}
following
are
the
various
commonly
used
classes
of
Class
ArrayLis
t
It is basically an alternative to an array. However, unlike array you can add and remove items
from a list at a specified position using an index and the array resizes itself automatically. It
also allows dynamic memory allocation, adding, searching and sorting items in the list.
A hash table is used when you need to access elements by using key, and you can identify a
useful key value. Each item in the hash table has a key/value pair. The key is used to access
the items in the collection.
SortedLi
st
A sorted list is a combination of an array and a hash table. It contains a list of items that can be
accessed using a key or an index. If you access items using an index, it is an ArrayList, and if
you access items using a key , it is a Hashtable. The collection of items is always sorted by the
key value.
It is used when you need a last-in, first-out access of items. When you add an item in the list, it
is called pushing the item and when you remove it, it is called popping the item.
It is used when you need a first-in, first-out access of items. When you add an item in the list, it
is calledenqueue and when you remove an item, it is calleddeque.
It is used when you need to store the bits but do not know the number of bits in advance. You
can access items from the BitArray collection by using an integer index, which starts from
zero.
Generics allow you to delay the specification of the data type of programming
elements in a class or a method, until it is actually used in the program. In other
words, generics allow you to write a class or method that can work with any data type.
using System;
using System.Collections.Generic;
namespace GenericApplication
{
public class MyGenericArray<T>
{
private T[] array;
public MyGenericArray(int size)
{
array = new T[size + 1];
class Tester
{
static void Main(string[] args)
{
Anonymous methods
using System;
delegate void NumberChanger(int n);
namespace DelegateAppl
{
class TestDelegate
{