UNIT 01 (.
NET Framework using C#)
1. Declaring implicit and explicit variables:
Implicitly Typed Variable:
In implicitly typed variables, the type of the variable is automatically deduced at compile time by the compiler
from the value used to initialize the variable.
C# 3.0 introduced the implicit typed local variable "var". var can only be defined in a method as a local
variable.
Example:
class Program
{
static void Main(string[ ] args)
{
var i = 10;
Console.WriteLine("Type of i is {0}", i.GetType( ).ToString( ));
var str = "Hello World!!";
Console.WriteLine("Type of str is {0}", str.GetType( ).ToString( ));
var d = 100.50d;
Console.WriteLine("Type of d is {0}", d.GetType( ).ToString( ));
var b = true;
Console.WriteLine("Type of b is {0}", b.GetType( ).ToString( ));
String s;
s = Console.ReadLine( );
}
}
Output:
Kushal Johari, Faculty of Computer Applications Page 1
Implicitly-typed variables must initialize at the time of declaration, otherwise C# compiler would give an error:
Implicitly-typed variables must be initialized.
var i = 100; // Valid
var j; // Compile-time error: Implicitly-typed variables must be initialized
Multiple declarations of var variables in a single statement is not allowed.
var i = 100, j = 200, k = 300; // Compile-time error
var i = 100; var j = 200; var k = 300; // Valid
var cannot be used for function parameters.
void Display(var param) //Compile-time error
{
Console.Write(param);
}
Explicitly Typed Variable:
An explicit conversion involves casting from one type to another.
Example:
class Program
{
static void Main(string[ ] args)
{
int En = new int( );
En = 100;
Console.WriteLine("Explicitly Type of En is {0}", En.GetType( ).ToString( ));
string Estr = "Hello World!!";
Console.WriteLine("Explicitly Type of Estr is {0}", Estr.GetType( ).ToString( ));
double Ed = 100.50d;
Console.WriteLine("Explicitly Type of Ed is {0}", Ed.GetType( ).ToString( ));
Boolean Eb = true;
Console.WriteLine("Explicitly Type of Eb is {0}", Eb.GetType( ).ToString( ));
String s;
s = Console.ReadLine( );
}
Kushal Johari, Faculty of Computer Applications Page 2
}
Output:
2. Creating Object and Classes:
Class and Object are the basic concepts of Object-Oriented Programming which revolve around the real-life entities..
A class is a user-defined blueprint or prototype from which objects are created.
Basically, a class combines the fields and methods (member function which defines actions) into a single unit.
In C#, classes support polymorphism, inheritance and also provide the concept of derived classes and base classes.
Declaration of class
Generally, a class declaration contains only keyword class, followed by an identifier (name) of the class. But there are
some optional attributes that can be used with class declaration according to the application requirement.
In general, class declarations can include these components, in order:
Modifiers: A class can be public or internal etc. By default modifier of class is internal.
Keyword class: A class keyword is used to declare the type class.
Class Identifier: The variable of type class is provided. The identifier (or name of class) should begin with a
initial letter which should be capitalized by convention.
Base class or Super class: The name of the class’s parent (super class), if any, preceded by the : (colon). This
is optional.
Interfaces: A comma-separated list of interfaces implemented by the class, if any, preceded by the : (colon). A
class can implement more than one interface. This is optional.
Body: The class body is surrounded by { } (curly braces)
Example:
public class mca // declaring public class
{
public int a, b; // field variable
Kushal Johari, Faculty of Computer Applications Page 3
public void display( ) // member function or method
{
Console.WriteLine(“Class & Objects in C#”);
}
}
Objects:
It is a basic unit of Object-Oriented Programming and represents the real-life entities. A typical C# program creates
many objects, which as you know, interact by invoking methods.
An object consists of:
State: It is represented by attributes of an object. It also reflects the properties of an object.
Behavior: It is represented by methods of an object. It also reflects the response of an object with other
objects.
Identity: It gives a unique name to an object and enables one object to interact with other objects.
Consider Dog as an object and see the below diagram for its identity, state, and behavior.
Declaring Objects (Also called instantiating a class)
When an object of a class is created, the class is said to be instantiated. All the instances share the attributes and the
behavior of the class. But the values of those attributes, i.e. the state are unique for each object. A single class may
have any number of instances.
Kushal Johari, Faculty of Computer Applications Page 4
Example:
namespace ConsoleApplication1
{
class Program
{
static void Main(string[ ] args)
{
// Creating an object using 'new'
// Calling the parameterized constructor
// With parameters 10 and 12
Rectangle rect1 = new Rectangle(10, 12);
int area = rect1.Area( ); // To display are of the Rectangle
Console.WriteLine("The area of the" + "Rectangle is " + area);
String s;
s = Console.ReadLine( );
}
}
class Rectangle
{
public int length, breadth;
public Rectangle(int l, int b) // Parameterized Constructor User defined
{
length = l;
breadth = b;
}
public int Area( ) // Method to Calculate Area of the rectangle
{
return length * breadth;
}
}
}
Kushal Johari, Faculty of Computer Applications Page 5
Output:
3. The Main method specification:
C# applications have an entry point called Main Method. It is the first method which gets invoked whenever an
application started and it is present in every C# executable file.
The application may be Console Application or Windows Application. The most common entry point of a C# program
is static void Main( ) or static void Main(String [ ]args).
Example:
using System;
class GFG
{
// Main Method
static public void Main(String[ ] args)
{
Console.WriteLine("Main Method");
}
}
Meaning of the Main Syntax:
a. static: It means Main Method can be called without an object.
b. public: It is access modifiers which means the compiler can execute this from anywhere.
c. void: The Main method doesn’t return anything.
d. Main( ): It is the configured name of the Main method.
e. String [ ]args: For accepting the zero-indexed command line arguments. args is the user-defined name. So you
can change it by a valid identifer. [ ] must come before the args otherwise compiler will give errors.
Kushal Johari, Faculty of Computer Applications Page 6