My Profile

Showing posts with label C#.Net OOPS. Show all posts
Showing posts with label C#.Net OOPS. Show all posts

Friday, January 28, 2011

Example Program

Example program for printing sum of two numbers


using System;
class VarsDemo
{
       static void Main()
       {
              int x, y, z;
              Console.Write("Enter x Value :");
              x = int.Parse(Console.ReadLine());
              Console.Write("Enter y Value :");
              y = int.Parse(Console.ReadLine());
              z = x + y;
              Console.WriteLine("sum of {0} & {1} is {2}",x,y,z);              
       }
}




Parse is a method under the types, which can convert the string value into an appropriate type on which the method is called.


Examples:



  • int x = "100";                    //invalid
  • int x = int.Parse("100");    //valid
  • bool b = bool.Parse("true");   //valid
  • float f = float.Parse("3.14f");   //valid

using the parse method on an incompatible value to perform the conversion will give an error

Example:

  • int y = int.Parse("I0A");   //Error

----------------------------------------------------------

** If you like this post please like our page in facebook "Dot Net Support

C# Data Types


  • Integer Types


Note: byte, ushort, uint, ulong can store only unsigned integer values. where as short, int, long, sbyte can store signed values.



  • Float or Decimal Types




  • Boolean Type



  • Character Types


  • Root Type


The size of char type has been increased to 2 bytes providing a support for unicode characters i.e., Multi Language usage support other than english. where the numeric representation of other language character is known as unicode value that requires 2 bytes memory for settle.

String type doesn't have any fixed size because it was a variable length type.

The object type is capable of storing any type of value init and it was also a variable length type.


Syntax for variable declaration:



Examples:

  • int x;
  • string str = "hello";
  • double d1, d2, d3;
  • public bool flag = true;
  • public const float pi = 3.14f; //without "f" it will be treated as decimal and assigning value to const var is mandatory.
  • public readonly int y = 300;



----------------------------------------------------------

** If you like this post please like our page in facebook "Dot Net Support




Wednesday, January 26, 2011

Importing a Namespace

If a class is present under a namespace, must and should the class has to be referred by prefixing with the namespace under which it was defined. which will be complicated because some time we have very lengthy namespaces. To overcome the problem we can import a namespace into a program and consumes the classes in it without the namespace prefix.


To import a namespace, we use



  • using statement in CSharp and
  • imports statement in VB.

Note: Importing of a namespace should be done on the top of the class.

Syntax:

                                  using <namespace> ;

                                  eg;  using System;


Example:

using System;
class ImportsDemo
{

    static void Main()

    {

        Console.WriteLine("Importing a namespace");

    }

}

----------------------------------------------------------

** If you like this post please like our page in facebook "Dot Net Support

System.Console.WriteLine

In Console applications, we use this line in every program. So, let's discuss about this in detail.


Console is a predefined class under the base class libraries which provide some standard IO functionalities that can be performed on standard IO devices.


It provides few static methods in it like Write, WriteLine, ReadLine etc.,


System is a namespace where a namespace is a logical container of types ( class or structure or interface etc ).
It is used for performing core operations.


We use namespace for two different reasons, those are:



  • Grouping of related types.
  • To overcome the naming collision i.e., Multiple classes with same name can be defined by putting them under a separate namespace.
fig: Namespace Demo




----------------------------------------------------------

** If you like this post please like our page in facebook "Dot Net Support

Tuesday, January 25, 2011

Writing our first C# Program

We can write a program either by using an editor Visual Studio .net or under a Notepad also. when we write the program under visual studio it will implicitly save, compile and execute the code. whereas, in notepad we need to  manually save, compile and execute the program.


Writing a Program in Notepad:


Step 1: Open the notepad and write the following code in it.


class Example
{
    static void Main()
    {
        System.Console.WriteLine("My First Program");
    }
}


Save the program as Example.cs in the desired location.


Step 2: Compiling the program


To compile the program we were provided with a C# compiler that should be used from visual studi commant prompt. 
To use it  goto Start Menu --> Programs -->  MS Visual Studio --> Visual Studio Tools --> Visual Studio Command prompt.


After opening the command prompt, first move into the folder where your program has been saved and use the following statement to compile the program.


                                CSC  <filename>


eg: C:\CSharp\csc example.cs


Once the program gets compiled, it generates a file Example.exe in the same folder which contains IL code in it.


Step 3: Executing the Program


To execute the program from the command prompt, run the exe file which got created as following


eg:  C:\CSharp\Example.exe


Or


eg:  C:\CSharp\Example


Note: to see IL code in .exe file which got created after compilation, we use a special tool called ILDASM



----------------------------------------------------------

** If you like this post please like our page in facebook "Dot Net Support"  

Syntax to define Main Method

fig: Syntax for main Method


Main method should be explicitly declared as static if at all it has to start its execution without object creation.

It can be either Non-Value returning or return an int values as an output.

We can pass a string array as a parameter to Main method which was optional.

Syntax to define a class in C#

fig: Syntax for C# Class


  • Modifiers are some special keywords which can be applied on classes, variables, methods like public, private, static, sealed etc.,
  • C# is a case sensitive language, So while designing the language Microsoft has followed few conventions in case of Cases.
  • Naming Conventions: 
  1.  All keywords should be in lower case.
  2. Under the Base class Library, pre defined classes and methods adopts propercase convention i.e., first character of every word should be in upper case.
  3. A C# program has to be saved with ".cs" extension.
  4. we can use any name for the program but the best practice will be use of Class name as a filename.

----------------------------------------------------------

** If you like this post please like our page in facebook "Dot Net Support"  

Monday, January 24, 2011

Programming Structure under various programming languages

C Programming Structure:


fig: C Programming Structure


Procedural programming language like C is a collection of members (variable and fucntions) where these members are explicitly called from the main fucntion for execution because it was the entry point of the program.

Note: for most of the programming languages, main is the entry point from where execution starts.

Procedural languages lacks the features like "security" and "re-usability". To overcome the problems, in 70's a new approach in programming has been introduced known as "Object Oriented Approach" which provides security and re-usability.

In object oriented approach members are defined under a special container or wrapper known as a "Class", which provides the basic security for content that is present inside it.

fig: OOP


If we want to invoke the members of a class, it can done only with the permission of class i.e., by creating its "Object".

CPP Programming Structure:

fig: CPP Programming Structure



A class is a user defined type where types can never be consumed directly. To consume a type we need to create a copy of the type.

   Example:  
                         int = 100;     //invalid
                        int x = 100;   //valid

As a class is also a type, the same rule applies to class also, because it was also a type. so, to consume a class first we require to create a copy of the class which will be called as "Object" of the class and then only the members of class can be invoked.


* C++ language suffers from a criticism that it was not fully OOP, because we cannot define the main function within the class but as per the standards of OOP every line of code should be in the class only.

The reason why main cannot be defined inside the class is, if it was defined inside the class it  requires object of the class for invoking or execution which will be created under main only. So, unless object is created main cannot execute and unless main executes object cannot be created.


Java/C# Programming Structure:

The solution for the above problem has been given by the Java language, which was designed in such a way that the main method will be in the class only but to execute without object creation it should be declared as Static, where a static member doesn't require object of the class for execution.



when Microsoft designed the .Net  languages in terms of Main method they have adopted the same guidelines prescribed by Java. So, here also we use static in case of C# and shared in case of VB languages to execute Main without object creation.

Note: a C# program looks exactly same as a Java program.


----------------------------------------------------------



** If you like this post please like our page in facebook "Dot Net Support"  

Sunday, January 23, 2011

Versions of C#.Net

The 1st version of C#.Net language is 1.0 and then after the versions are as following

C#.Net     1.0
C#.Net     1.5
C#.Net     2.0
C#.Net     3.0

the versions 1.0, 1.5 and 2.0 are standardized under ECMA. C#.Net 3.0 is not currently standardized by any standards organization but it is expected that it will also became an ECMA and ISO standard as its predecessors.

Features new in C# 2.0:

  • Partial classes which alows class implementation across more than one source file.
  • Generics or Parameterized types.
  • Static classes that cannot be instantiated and that only allows static members.
  • Anonymous Delegates.
  • The scope of property accessors can be set independently.
  • Nullable Value types which provides improved interaction with SQL Databases.
  • Coalesce Operator ( ? ? ) which returns the first of its operands that is not null, or if no such operand exists.

Features new in C# 3.0:


  • Language Integrated Query (LINQ)
  • Object Initializers & Collection Initializers
  • Anonymous Types
  • Implicitly typed arrays
  • lambda expressions
  • Automatic properties
  • Extension methods
  • Partial Methods.

----------------------------------------------------------

** If you like this post please like our page in facebook

C# Design Goals

The ECMA standard lists these design goals for C#.Net



  • It is intended to be simple, modern, general purpose, and object oriented programming language.
  • It should include strong type checking, array bound checking, detection of attempts to use uninitialized variables, source code portability and automatic garbage collection.
  • It is intended for use in developing software components than can take advantage of distributed environments.
  • As programmer portability is very much important, especially for those programmers already familiar with C & C++, C# is best suitable.
  • Provide support for internationalization, as it was very important.
  • It is intended to be suitable for writing applications for both hosted and embedded systems.


----------------------------------------------------------

** If you like this post please like our page in facebook

Share

Twitter Delicious Facebook Digg Stumbleupon Favorites More