Posts

Implicitly typed keyword - var in C#

What is var var is a keyword in C#. It is used to declare the implicitly typed local variables. You can assign value of any type to var, and the compiler will decide the data type of value at the compile time.  Example var age=20; var name="ABCD"; var number=23.3 To get the data type of value that has been assigned to the var, you can use GetType() method. Console.WriteLine(age.GetType());     //output - System.Int32 Console.WriteLine(name.GetType());     //output - System.String Console.WriteLine(number.GetType());    //output - System.Double Initialization The var must be initialized at the time of declaration. Otherwise the compiler gives the compile time error. var age;       error - implicit typed variables must be initialized Multiple Declarations Multiple var variables cannot be declared on the same line. var age=23,Id=32; error- Implicitly typed variables cannot have multiple declarators var as a...

Classes and objects in C#

 What is a class A class is like a container which contains methods, fields, constructors events, and delegates etc. Just like every other object oriented programming language, everything in C# is associated with a class and object. A class depicts real world entity. For example, in real world, a car has several characteristics like colour, speed, etc. Also a car has wheels, steering, seats and many other parts. you can create 'Car' class in C# containing properties like Wheel, Steering. Car class will have methods to show the functionality of the Car like Start,Stop,DoorOpen , DoorClosed, etc. What is an object An object is an instance of a class. Using object, we can access the properties and methods of a class. We can create multiple objects of a single class. Declaring a class To create a class in C#, use the keyword class followed by name of the class. class Employee { } You can create properties, methods and variables in class. class Employee {     string Color="R...

Data types in C#

What is a Data type? A data type specifies the type of value that can be stored in a variable. C# is a strongly typed language. It means every variable in c# must have a data type specified. Type of Data Types. In C#, there are basically 2 types of data types 1) Value type - These are simple types like int,bool,char, float etc 2) Reference type - These are complex types which includes class,arrays, interface and delegates etc For example  int age=12;  float number=12.3; bool IsPresent=false; char type='A';  string msg="Hello"; Data types and it's range. This table lists some predefined data types in c# and the range of values it can store. Signed Data types Data Type Description Size sbyte 8-bit signed integer -128 to 127 short 16 bit signed -32768 to 32767 int 32 bit signed -2,147,483,648 to 2,147,483,647 long 64 bit signed -9,223,372,036,854,775,808 to 9,223...

Difference between Abstract class and Interface

Abstract class. An abstract class in C# is a class which cannot be instantiated.   It is declared with abstract modifier. Generally abstract class is used to provide common definition or behavior that the derived classes can implement. For example public abstract class Employee { } Interface An interface in C# is just like a contract that other classes have to follow. It is declared with interface modifier. Interface is used to provide loose coupling between classes and its dependancies. For example interface IEmployee { } The difference between abstract class and the interface is as follows. Abstract Class Interface Abstract class is declared with abstract modifier An interface is declared with interface modifier It contains abstract as well as non-abstract methods It contains only abstract methods You can declare and define the methods in abstract class You can only d...

How to declare and initialize a variable in C#

 A concept of a variable A variable is something which we can change the value of . In C#, a variable is a storage area in memory which holds the particular value in it. A variable name is prefixed with the data type. The data type of the variable decides what type of value and what range of values can be stored in that variable. Declaring and initializing  a variable. The below code declares the variable of Integer data type  int age=22;    ' age ' is a name of a variable. The compiler will allocate some memory to this variable and store the value 22 in it. The ' int ' is a data type of a variable. It means we can store only numbers in this variable. You will learn about the Data types in the next post. Print the value of the variable on console using below code. Console.WriteLine(age); Declaring multiple variables You can declare multiple variables on the same line by separating them with comma(,). These variables must be of same data type int age,id,salary A...

Interaction with Console

Image
 What is a Console application? A Console application in C# a command line application. It takes the input from the user and writes the output to the command line console window. The C# console application interacts with the three standard data stream i.e. standard input, standard output and standard error. What is Console class and it's functionality in C#? A Console class is defined in System namespace. It contains several properties and methods that are used to interact with standard input, output and error stream. It is a static class and it cannot be inherited. Create a Console application in C#. We will now create a console application. Before that we need to install .NET framework SDK. Some of the .NET framework SDKs comes preinstalled in windows. You can also download them separately from Microsofts website   You can check the .NET framework version installed on your machine by following below steps.'= Press Win + R. Type 'cmd' without quotes and press enter. on...

Introduction to C#

What is C# and why to use C#. C# (csharp) is a general purpose, object oriented, powerful programming language developed by Microsoft. A Team of developers led by Anders Hejlsberg at Microsoft developed this language and it is approved by European Computer Manufacturers Association (ECMA) and International Standards Organization (ISO). Using C#, developers can build many types of secured and robust applications with .NET framework C# has it's roots in C and C++. The developers who have worked on C or C++ shall find the C# quite familiar. Since it's creation , C# has added many features which plays important role in developing quality softwares and applications. With each version of the C#, new features were added to support the modern software development approach. A brief about .Net framework. A .Net framework is a virtual machine known as Common Language Runtime with the set of libraries. .Net framework compiles and executes programs written in language like C# and Visual bas...