1)enum is a keyword
2)enum contains a collection of name and value pairs
3)enum is a user defined datatype
Syntax 1 to declare enum:
enum GTB
{
Name-1=value1,
Name-2=value2,
Name-3=value3
}
These values must be in the range of int datatype
Syntax 2 to declare enum:
enum GTB:byte
{
Name-1=value1,
Name-2=value2
}
as per syntax2, The values must be in the range of byte datatype. Enums must be declared in General declaration(GD) area only.
Example on Enum:
Open console application project:
Start->programs->Microsoft visual studio 2010->Microsoft Visual studio 2010->file menu->new->
project->select visual c# from installed templates->select console application
code for main method
class program
{
enum GTB
{
Sa = 14,
Ja = 18,
Ca = 22
}
class Enums
{
static void Main(string[] args)
{
int a = (int)GTB.Sa;
int b = (int)GTB.Ja;
int c = (int)GTB.Ca;
Console.WriteLine(a + " " + b + " " + c);
Console.ReadKey();
}
}
}
Press F5 to see the output
Observations on above program :
1)if enum number is not initialized, Then it will be initialized automatically with the incremented value of previous member.
2)in case if previous number is not existing then initillization begins from 0 onwards.
Working with object oriented programming concepts
Based on the style of programming, The programming languages are divided into 3 types
1)procedural languages
2)structured(structural) languages
3)object oriented programming languages
object oriented programming languages
whenever a languages supports the following oops concepts, Then that language is called as object oriented programming languages.
Oops concepts:
1)abstraction
2)encapsulation
3)polymorphism
4)inheritance
Abstraction:
Abstraction is a concept of writing data members and member function together. Which is also called as data hiding. Here data will be hidden with the help of private variables.
Encapsulation:
Encapsulation is a concept of providing complete functionality for an entity with the help of data members and member functions. Encapsulation leads to data binding.
Polymorphism:
Polymorphism is a concept of writing more than one method with same name and with different types of args.
Inheritance:
Inheritance is a concept of deriving features from parent (base) class into child(derived) class.
To work with oops concepts, We required classes & objects
Class: class is a logical representation as per C#.Net, A class is a collection of fields, methods, properties and events.
Object: object is a physical representation of an instance of the class
Syntax to write A class:
class Test
{
private int x, y; //fields
string s; //field…by default private
public void print() //method
{
//logic
}
}
class, private, public are keywords.
private, public are called as access specifiers
Syntax to create an object:
Test t=new Test();
Test-> is class name
t-> is object name
new-> is memory allocation operator
Test()-> is constructor
Example on class, objects along with private public access specifiers
Open console application project:
Start->programs->Microsoft visual studio 2010->Microsoft Visual studio 2010->file menu->new->
project->select visual c# from installed templates->select console application
class Program
{
class Test
{
private int a, b; //fields, Reference types
public void Read(int x, int y) //mehods
{
a = x;
b = y;
}
public void print() //method
{
int c = a + b;
Console.WriteLine("the sum is :" + c);
}
}
static void Main(string[] args)
{
Test t1 = new Test();
t1.Read(10, 20);
t1.print();
Test t2 = new Test();
t2.print();
Test t3 = t1;
t3.print();
Console.ReadKey();
}
}
About This key word:
1)when ever instance variable names & local variable names are same, then by default priority(importance) will be given to local variables.
2)in above case, in order to access instance variables we required “this” keyword.
3)”this“ works like an object for current class
4)this keyword always points to the current class
Example on this keyword along with instance & local variables
Open console application project:
Start->programs->Microsoft visual studio 2010->Microsoft Visual studio 2010->file menu->new->
project->select visual c# from installed templates->select console application
code in general declaration
class program
{
class Emp
{
private int sal = 5000; //instance variable
public void Increment(int sal)
{
this.sal = this.sal + sal;
}
public void print()
{
Console.WriteLine("Total:" + this.sal);
Console.WriteLine("Total:" + sal);
}
}
static void Main(string[] args)
{
Emp e1 = new Emp();
e1.Increment(3000);
e1.print();
Console.ReadKey();
}
}
Problems with console application projects:
1)console application does not supports to work with colors, fonts etc…-
2)does not support GUI based applications.
3)in console application, Once if the data is entered then it is not editable.
4)To overcome this problems .Net introduced windows forms application project template.