Monday, April 30, 2012

Overriding


Class inheritance causes the methods and properties present in the base class also to be
derived into the derived class. A situation may arise wherein you would like to change
the functionality of an inherited method or property. In such cases we can override the
method or property of the base class. This is another feature of polymorphism.


public abstract class shapes
{
public abstract void display()
{
Console.WriteLine("Shapes");
}
}
public class square: shapes
{
public override void display()
{
Console.WriteLine("This is a square");
}
}
public class rectangle:shapes
{
public override void display()
{
Console.WriteLine("This is a rectangle");
}
}

Overloading and Overriding of the Class


Overloading provides the ability to create multiple methods or properties with the same
name, but with different parameters lists. This is a feature of polymorphism. A simple
example would be an addition function, which will add the numbers if two integer
parameters are passed to it and concatenate the strings if two strings are passed to it.


using System;
public class test
{
public int Add(int x , int y)
{
return(x + y);
}
public string Add(String x, String y )
{
return (x + y);
}
public static void Main()
{
test a = new test ();
int b;
String c;
b = a.Add(1, 2);
c = a.Add("Reshmi", " Nair");
Console.WriteLine(b);
Console.WriteLine(c);
}
}
O/P:
3
Reshmi Nair

Language Fundamentals in C#



Constants & Variables
A variable is a named memory location. They are programming elements that can change
during program execution. Data that needs to be stored in memory & accessed at a later
time are stored in variables. Instead of referring to the memory location by the actual
memory address you refer to it with a variable name.
Variables are declared as follows
int a;
They can also be initialized at the time of declaration as follows:
int a = 10;
Constants are very similar to variables. The main difference is that the value contained in
memory cannot be changed once the constant is declared. When you declare a constant
its value is also specified and this value cannot be changed during program execution.
Constants are used in situations where we need to keep the value in some memory
location constant. If you use hard-coded values, and the value is changed then it has to be
changed in all the locations in the code where it has been used. Instead if we are using
constants, all we will need to do is to change the value of the constant. This would
propagate the changes to our entire application.


Constants are declared as follows
const int a;
Simple Types (Primitive Data types)
Simple or value type variables are those, which are assigned space in the stack instead of
the heap. All the primitive types such as int, double etc are value type variables. The
simple types basically consist of Boolean and Numeric types, where Numeric is further
divided into Integral and Floating Point.

The first rule of value types is that they cannot be null. Anytime you declare a variable of
value type, you have allocated the number of bytes associated with that type on the stack
and are working directly with that allocated array of bits. In addition, when you pass a
variable of value type, you are passing that variable’s value and not a reference to the
underlying object.

Object Type
Object type or reference type variables are those, which are allocated storage space in the
heap. Reference type objects can be null. When a reference type is allocated under the
covers a value is allocated on the heap and a reference to that value is returned. There are
basically four reference types: classes, interfaces, delegates and arrays.

Class Type
Custom data types are available in .NET framework in the form of classes or class type. It
is nothing but a set of data and related behavior that is defined by the developer.

Object type and class type are both reference type variables. The only difference comes
from the fact that object type consists of objects predefined and available with the .NET
framework such as string whereas class type consists of custom user defined data types
such as the class employee given below.

class employee
{
int empid;
string empname
public employee()
{
empid = 10;
empname = “Reshmi”;
}
}

History of C#


.NET framework offers a myriad of languages which puts us programmers into a deep
thought process about which programming language best suits our needs.
Which language is the "best" language choice? If you are a VB wizard, should you take
the time to learn C# or continue to use VB.NET? Are C# ASP.NET pages "faster" than
VB .NET ASP.NET pages? These are questions that you may find yourself asking,
especially when you're just starting to delve into .NET. Fortunately the answer is simple:
there is no "best" language. All .NET languages use, at their root, functionality from the
set of classes provided by the .NET Framework. Therefore, everything you can do in
VB.NET you can do in C#, and vice-a-versa.
The differences occur in three main areas: syntax, object-oriented principles, and the
Visual Studio .NET IDE. Syntax concerns the statements and language elements. Object
Oriented differences are less obvious, and concern differences in implementation and
feature sets between the two languages. IDE differences include things like compiler
settings or attributes. There is also a fourth area of difference: language features that are
present in one language but have no equivalent in the other.
If you are more familiar with Java, JScript, or C/C++, you may find C#'s syntax more
familiar than VB.NET's.
A good question that has to be answered in order to keep you interested in learning C# is
Why should you learn another programming language when you already doing enterprise
development in C++ or Java. The very answer at the first go will be C# is intended to be
the premier language for writing NGWS (Next Generation of Windows Services)
applications in the enterprise computing world.
The programming language C# derives from C and C++; however apart from being
entirely object oriented it is type safe and simple too. If you are a C/C++ programmer
your learning curve should be flat. Many C# statements including expressions and
operators have been taken directly taken from your favourite language
An important point about C# is that it simplifies and modernizes C++ in the areas of
classes, namespaces and exception handling. Much of complex features have not been
included or in fact hidden in C# to make it easer to use and more importantly less error
prone for e.g. no more macros, templates and no multiple inheritances (Few of you might
not like it.)
C# provides you with convenient features like garbage collection, versioning and lot
more.
The only expense that I can think of is that your code operates in safe mode, where no
pointers are allowed. However if you want to use pointers you are not restricted from
using it via unsafe code- and no marshalling is involved when calling the unsafe code.
So you will learn a great deal of this new language in the coming Sections and see for
yourself that how C# resembles or not resembles your favorite language

JIT (Just–in-Time Compiler) & Debugging


The .NET Runtime ships with a Just-In-Time (JIT or JITter) compiler, which will convert
the MSIL code in to the native code (CPU Specific executable code). So whatever code
we write will be complied in to MSIL format and the JIT takes over when you run it.
The .NET runtime/Common Language Runtime (CLR) ships three different classes of
JITters. The Main JIT compiler converts the MSIL code it to native code with out any
optimizations. The JIT compiler takes the MSIL code and optimizes it. So this compiler
requires lot of resources like, time to compile, larger memory footprint, etc. The PreJIT
is based on the Main JIT and it works like the traditional compilers (compiles MSIL to
native code during compilation time rather than runtime). This compiler is usually used at
the time of installation.
No matter whatever language we used to develop the HelloWorld program, it’s a known
fact that compiler’s are going to generate a MSIL format, once our code has been
converted in to MSIL format, from MSIL format all the code that we write will be
converted to native code in the same way whether if it is a VB.NET source or C# source.

First C#.NET Program


/* This is the famous helloworld program written using C#.NET */
/* Indicates that the code is referring System Namespace to access the
functionality’s of System.dll */
using System;
// Namespace name given for the class
namespace HelloWorldSample
{
//Definition of the class
public class HelloWorld
{
// Entry point method for the class
public static void Main()
{
//Displaying helloworld in the screen
System.Console.WriteLine("HelloWorld");
}
//end of the class declaration
}
//end of the namespace
}

Figure showing HelloWorld program written using C#.NET in Notepad




The lines in the program that starts with a // and /*….*/ (comment blocks) are comment
entries like in other programming languages which are excluded in the compilation
process. For C or C++ programmers the C# style of coding sounds great because it
almost follows the same style.

namespace HelloWorldSample - The keyword “namespace” is new to some
programmers who are not familiar with C++.

‘namespace’ – a keyword in .NET is used to avoid name collisions i.e. For example, you
develop a library which has a class named “File” and you use some other library which
also has a class named “File”, in those cases there are chances of name collision. To
avoid this you can give a namespace name for your class, which should be meaningful. It
is always better to follow the syntax (MS Recommended) given below while giving
names for your namespaces
CompanyName.TechnologyName
However the hierarchy can be extended based on the implementation of the classes in the
library.

public class HelloWorld - This is the class declaration in C#.NET; the interesting thing
for C++ or Java developers is that they can apply the OOPS concepts that are supported
by C#.NET . The class always ends with an “End Class”.

‘public’ - is the modifier to determine the scope of the class (for other modifiers refer
.NET framework SDK documentation or later parts of this tutorial). HelloWorld is the
class name given for the class. Consumers of the class will be accessing through this
name only.

public static void Main () - This is called as the entry point function because the runtime
after loading your applications searches for an entry point from which the actual
execution starts. C/C++ programmers will find this method very familiar (VB
Programmers remember Sub Main). All Applications (exe) must have a definition for the
Main Method. Try removing the Main method from your application and the compiler
will complain that "No Start Point Defined". This means that the Main Method is the
starting point of any application, in other words When you execute your Application
"Main" method is called first automatically.

'public' - This is the Access modifier for the Method. Since the Main method should be
accessible to everyone in order for the .NET Runtime to be able to call it automatically it
is always defined as public.

'static' - indicates that the method is a Class Method. Hence it can be called without
making an instance of the class first.

‘void’ – indicates the return type of the Main function, here in this case the Main function
returns nothing so it is mentioned as void, for functions that returns value should have
appropriate type such as long, string etc.,

Now its time to compile and execute this complex program. To compile the above piece
of code you can use C# compiler. To run the C# compiler make sure you set your path
variable to point to the place where your C# compiler is available. (To set a new value in
the path variable, go to control panel and double click System icon, then choose
advanced tab and click Environment Variables button to add or edit the environmental
variables)

Figure shows compilation of the HelloWorld program using C# compiler








The compiler used here is “csc”, which is a visual basic .net compiler accepts the source
file “HelloWorld.cs” compiles the same to produce a program that’s not true executable,
instead it generates something called assembly.
Assembly
An assembly is a grouping of files deployed as a single file. An assembly almost always
consists of at least two files: the executable and the manifest. The manifest is a list of all
the files that exist inside the assembly. The executable content inside the assembly is
referred to individually as a module. Conceptually, modules correspond to DLLs or
EXEs; each module contains metadata, in addition to the metadata of its parent assembly.
The assembly format is an enhanced version of the current Portable Executable (PE)
format (your normal Windows .EXE file format).
Manifest
Manifest is considered as the integral part of every assembly that renders the assembly
self-describing. The assembly manifest contains the assembly's metadata and it also
establishes the assembly identity, specifies the files that make up the assembly
implementation, specifies the types and resources that make up the assembly, itemizes the
compile-time dependencies on other assemblies, and specifies the set of permissions
required for the assembly to run properly.
Metadata
The standard PE header comes at the beginning of the file. Inside the file is the CLR
header, followed by the data required to load the code into its process space—referred to
as metadata. It describes to the execution engine how the module should be loaded, what
additional files it needs, how to load those additional files, and how to interact with COM
and the .NET runtime.
Metadata also describes the methods, interfaces, and classes contained in the module or
assembly. The information the metadata provides allows the JIT compiler to compile and
run the module. The metadata section exposes much of your application's internals and
eases the transition from disassembled IL to useful code.

First VB.NET Program


To start of with any language it’s always worth writing a simple program, which actually
does nothing but displays a “HelloWorld” string in the screen. Taking this simple
program we will try to figure out how .NET framework delivers a successful
HelloWorld.exe. Well to write such a complex program we will go for our favorite editor,
the choice is unanimous, it’s “Notepad”.



'This is the famous HelloWorld Program written using VB.NET
Namespace HelloWorldSample
'Definition of the Class
Public Class HelloWorld
'entry point method for the Class
Public Shared Sub Main()
System.Console.WriteLine("HelloWorld")
End Sub
'end of Class Declaration
End Class
'end of the Class Module
'end of namespace
End Namespace


Now let us spend sometime in examining the HelloWorld program to find out what’s new
in writing code through any .NET language.

The lines in the program that starts with a ‘(single quote) are comment entries like in
other programming languages which are excluded in the compilation process. Like VB
the way in which comment entries are represented remains the same in VB.NET.
Namespace HelloWorldSample - The keyword “Namespace” is new to some
programmers who are not familiar with C++.

‘Namespace’ – a keyword in .NET is used to avoid name collisions i.e. For example, you
develop a library which has a class named “File” and you use some other library which
also has a class named “File”, in those cases there are chances of name collision. To
avoid this you can give a namespace name for your class, which should be meaningful. It
is always better to follow the syntax (MS Recommended) given below while giving
names for your namespaces

CompanyName.TechnologyName

However the hierarchy can be extended based on the implementation of the classes in the
library.

Public Class HelloWorld - This is the class declaration in VB.NET; the interesting thing
for VB developers is that VB.NET is a fully object-oriented language (so everything is a
Class here) . The class always ends with an “End Class”.

‘Public’ - is the modifier to determine the scope of the class (for other modifiers refer
.NET framework SDK documentation or later parts of this tutorial). HelloWorld is the
class name given for the class. Consumers of the class will be accessing through this
name only

Public Shared Sub Main () - This is called as the entry point function because the
runtime after loading your applications searches for an entry point from which the actual
execution starts. C/C++ programmers will find this method very familiar (VB
Programmers remember Sub Main). All Applications (exe) must have a definition for the
Main Method. Try removing the Main method from your application and the compiler
will complain that "No Start Point Defined". This means that the Main Method is the
starting point of any application, in other words When you execute your Application
"Main" method is called first automatically.

'Public' - This is the Access modifier for the Method. Since the Main method should be
accessible to everyone in order for the .NET Runtime to be able to call it automatically it
is always defined as public.

'Shared' - indicates that the method is a Class Method. Hence it can be called without
making an instance of the class first.

Now its time to compile and execute this complex program. To compile the above piece
of code you can use VB.NET compiler. To run the VB.NET compiler make sure you set
your path variable to point to the place where your VB.NET compiler is available. (To
set a new value in the path variable, go to control panel and double click System icon,
then choose advanced tab and click Environment Variables button to add or edit the
environmental variables)

Figure shows compilation of the HelloWorld program for VB.NET


The compiler used here is “vbc”, which is a visual basic .net compiler accepts the source
file “HelloWorld.vb” compiles the same to produce a program that’s not true executable,
instead it generates something called assembly. Here the VB.NET compiler produces a
Managed Code/Intermediate Language (MSIL) format that uses instructions which are
CPU-independent.









Using Authorization with Swagger in ASP.NET Core

 Create Solution like below LoginModel.cs using System.ComponentModel.DataAnnotations; namespace UsingAuthorizationWithSwagger.Models {     ...