C# Concepts

39
Ui Introduction 1. Microsoft .Net release by Microsoft corporation. 2. Java developed by sun micro system. Microsoft is big competitor to the sun micro system. Sun Micro system is Group of the companies developed java. It is open source. Difference between the Microsoft and Sun Micro system? Sl.No Microsoft Corporation Sun Micro system 1 It is developed MS.Net It is developed Java 2 .Net is not a open source. We need to pay money to use this software. Java is open source. 3 .net having rich graphic user interface editor. Java not having the editors except third party editors. Versions History of the MS.Net: Visual Studio VS2003 VS200 5 VS2008 Vs2010 VS2012 Framework 1.0 & 1.1 2.0 3.0 & 3.5 4.0 4.5 MS.Net nothing but a collection of following languages: Note: Each .net framework language having the facility to develop windows and Web applications

description

C#

Transcript of C# Concepts

Ui

Introduction 1. Microsoft .Net release by Microsoft corporation.2. Java developed by sun micro system. Microsoft is big competitor to the sun micro system. Sun Micro system is Group of the companies developed java. It is open source.

Difference between the Microsoft and Sun Micro system?

Sl.NoMicrosoft CorporationSun Micro system

1

It is developed MS.NetIt is developed Java

2

.Net is not a open source. We need to pay money to use this software.Java is open source.

3

.net having rich graphic user interface editor.Java not having the editors except third party editors.

Versions History of the MS.Net:Visual StudioVS2003VS2005VS2008Vs2010VS2012

Framework1.0 & 1.12.03.0 & 3.54.04.5

MS.Net nothing but a collection of following languages:

Note: Each .net framework language having the facility to develop windows and Web applicationsWhat is the Web Application:

Web application is a website (ASP, ASPX, PHP, html etc) and will always be viewed through web browser.

What is the Windows Application:

Windows application is mostly an exe-file for example MS Word, Notepad and these applications run "standalone".

Difference between the Windows and Web Applications?

Sl.NoWebWin

1

Web application is a website (ASP, ASPX, PHP) and will always be viewed through web browser.

Windows application is mostly an exe-file for example MS Word, Notepad and these applications run "standalone".

2

IIS server required to run the web application.IIS server not required to run the web application.

Before the MS.Net which technologies used for Windows and Web:a) VB 6.0(Visual Basic) ( for windows Application

b) ASP 3.0(Active Server Pages)( for web ApplicationActive Server Pages (ASP) 3.0:

It is a combination of Html+ JavaScript + VBScript.HTML Used for web page developmentJavaScript used for client-side validations. so we called JavaScript is Clint-side script.

VBScript is used for server-side Transactions. Mainly used data base transactions.C#.Net Basic Concepts:

C#.Net is a fully object oriented language and component based language.

It is developed by combining the concepts of C,C++,Java and productivity of VB 6.0.Difference between the C#.Net and Java?C#.NetJava

Unlike java,C#.net data type are objectsJava Data types are not an objects

C#.Net support the struct keywordJava does not support.

In C#.Net parameters passed by reference by using ref keywordIn Java Parameters passed by values

In the C#.Net series of statements terminated with a semicolon.Type of comments:

1) Single-Line comments2) Multiline comments

Single-Line comments:

Single line comments begin with double backslash(//) symbol.Ex: // this is Gsoft TechnologyMulti-Line Comments:

Multiline comments used to comment the multiple statements. Multiline comments starts with /* and ends with */Example:

using System. Data;

/*using System;*/public partial class _Default : System.Web.UI.Page{

protected void Page_Load(object sender, EventArgs e)

{ // this is C# concepts (this is single line comment) Below is the multiline comment:

/* This is Gsoft

This is india

Gsoft Technologies */ }

}

Access Modifiers or Access specifies:C#.Net Supports five types of access specifies or access modifiers.1.Public2.Private3.Protected

4.Internal

5.Protechted internal

Example:

Public:

Public members have no access restriction. We can access public members by creating object, in derived class and with in the assembly and in other assemblies also.(in another application). . It can be accessed everywhere.Field , Properties and Members declared with public keyword are accessible from anywhere in the class and outside of the class also we can access.

Within the class - Visible

a)Visible the public members within the same Classb) Visible the public members in other Class by creating the objectc)Visible public members in derived/inherited/child Class4)Visible public members from other namespace(application) by creating the object.5)Visible public members from other namespace(application) by inheritance.Private:

The scope of the private members is limited.

They are not accessible outside the Class or structure , block or module .

We cannot access private members by creating object and also we cannot access private members in derived class.

Private keyword allows a class to hide its member and variables from other class objects and functions. So it is not visible outside the class.

When you do not specify any data member as public, protected, or private, then the default access specifies for a data member is private.a)Visible the private members within the same Classb) We cannot visible the private members in other Class by creating the object.c) We cannot visible private members in derived/inherited/Child Class.d)We cannot visible private members from other application by creating the object.e) We cannot visible private members from other application by inheritance.

Protected:

You cannot access protected members by creating object. You can access protected members only in derived/inherited/child class.The type or member can only be accessed by code in the same class or struct, or in a derived class.

a)Visible the protected members within the Same Classb)We cannot visible the protected members in other Class by creating the object.c)We can visible protected members in derived/inherited/Child Class.

c)We cannot visible protected members from other application by creating the object.

d)We can visible protected members from other application by the inheritance.

Note: Variable declared with protected keyword are accessible within the class and inherited classes.

The protected access specified in C# allows a class to hide its variables and member from other class objects except the child class. This

Field , Properties and Members declared with Protected keyword are accessible in the inherited classesInternal:Internal members can be accessed from any class or method defined within the application but not from another application. If we do not specify any data member as public, protected, or private the default access specifier for a class is internal.a)Visible the internal members within the same Classb)We can visible the internal members in other Class by creating the object.

c)We can visible internal members in derived/inherited/Child Class.

d)We cannot visible internal members from other application by creating the objecte)We cannot visible internal members from other application by inheritance.Protected internal:The scope of this pecifier is similar to the protected as well internal.access the member within the class and from other application we able to get it through the inheritance.

a)Visible the protected internal members within the same Classb)Can visible the protected internal members in other Class by creating the object.

c)Can not visible protected internal members in derived/inherited/Child Class.d)We cannot visible protected internal members from other application by creating the object.d)We can visible protected internal members from other application by inheritance.Destructors cannot have accessibility modifiers.The type or member can be accessed by any code in the same assembly, or by any derived class in another assembly

C#.Net Data Types:Data types(type of data) that can be stored in variable.

Variables:

Variable is a small amount of memory which is used to store specified data type values.

Variables are storage area for data.

All the variable names are case-sensitive

All the local variable must be declared and initialized before using.

Note:

To extend the lifetime of a variable in vb.net we use the static keyword instead of Dim keyword.

There are two types of the data types:

Value Type: Value data type variables contain the data directly.Value types are stored in the stock memory.

Reference Types:The reference type used to store the address location of the data. reference of another memory location that contain the data.Difference between the Value type and Reference type?SL.No

Value TypeReference Type

1

Value types are stored on the stackReference types stored on the managed heap.

2

A value type variable contain the data directly.The reference type used to store the address location of the data

3Value types cannot be inheritedreference types can inherited.

4

Pre-defined Value Types:

how to declare the data types:'VB

Dim b As Boolean = False// C#

bool b = false

Predefined Value Types or Primitive Types:1Int(4bypes)2.long(8byptes)

3.byte(8bits)

4.sbyte(8bits)

5.Short

6.ushort

7.uint(4bits)

8.ulong(8 bytes)

9.float(4 bytes)

10.double(1byte)

11.bool(1 byte)

12.char(2 bytes)

13.(Decimal)

14.DateTime

What is the boxing and un-boxing?Boxing is implicit conversion of value type into the reference type.un-boxing is reverse process for boxing.

Un-Boxing:To return the boxed object back to a value type ,We must explicitly un box it.how to declare the data types:protected void Page_Load(object sender, EventArgs e)

{

int i = 123;

object o = i;//boxingint j = (int)o;//un boxing}

Creates an integer i and implicitly boxes it when it assigned to the object o.The value is then explicitly unboxed and assigned to a new int whose value is displayed.Difference between the string and string builder:System. String provides a set of members for working with textString : At the time of concatenation each time it will re- allocate the new memory and previous memory allocations also available to wastage of memory is remain.

in StringBulder:It append the string with the same memory allocation.

Parameters:

Difference between the Ref and Out keywords:

Ref:

1. Ref is a keyword. It keyword used while passing and receiving the variables.2.It allows to modify the original variable.

Difference between the Ref and Out Keywords?

Ref

Out

Variable must be initialized

public class A {

public int Fun(ref int x)

{

return 2 * x;

}

}

Calling:

A obj = new A();

int x;

x = 6;//Variable need to be initialized obj.Fun(ref x);

Initialization of the variable is optional.

public class A {

public void Fun(out int x)

{

x= 2 * 5;

}

}

Calling:

A obj = new A();

int x;//Variable initialization should be optional. obj.Fun(out x);

The ref variable should not be constant

The variable should not be assigned.

Out:

1.Out is a keyword. Initialization of the out variable optional.

Pre-defined User Types:Enumerations:

Enumerations is a user-defined value type.

Enumerations provide powerful alternative to the constants or

An Enumeration is a group of related constants.The enum keyword is used to declare an enumeration

set of named constants are called the enumerator listAn Enumeration begins with the keyword enum which is generally followed by an identifiers sugh as

Enumerations are special sets of named values which all maps to a set of numbers, usually integers.Ex: Enum Temparature.

Example:public class Default2 : System.Web.UI.Page{

public enum Days { Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday };

protected void Page_Load(object sender, EventArgs e)

{

Days day = Days.Monday;

Response.Write(day);

}

}

The enum declaration ends with the enumerator list. The enumerator list contains the constant assignments for the enumeration, each separated by comma.

The enumeration begins at 0 and each subsequent value count up from the previous

Example:public class Default2 : System.Web.UI.Page{

int first, second, third, fourth;

public enum Count {

first,

second,

third=20,

fourth

};

protected void Page_Load(object sender, EventArgs e)

{

Response. Write((int)Count.first);

Response. Write((int)Count.fourth); }

}

Output:

0

21(the value of 3rd will be 20 and fourth will be 21).

The enumeration begins with 0 and each subsequence value count up from the previous so fourth value is 21

Note: the enumerator cannot declare inside the procedure. it can declare at class or module.

Constants:

The variables whose values do not change during the execution of a program are known as constants

ex: const int rows =10;

Difference between the Read only and Constant?

The read-only and constants are same they can never be modified but

Read-onlyConstant

Read-only allows to initializing variables at run-time.Constant must be initialized at compile time.

public readonly DateTime dt = DateTime.Now;

public const int x=2;

Pre-defined Reference Types:string

object

User-defined Reference Types:Class:

Class is collection of fields, properties, methods and eventsclass keyword used to define the class.

syntax:

class A

{

}

Arrays:

Array is a collection of objects with similar types.In C#.Net arrays are objects.

Name space is : system. array.

Arrays have same name with different index no which starts from zero.

square brackets [ ] are used to declare an array.ex: a[ ]

dis-advantages:1.size is fixed2.Allows only similar type of data.

Types of the arrays:a

Single dimensional

b

Multi dimensional arrays

c

Jagged arrays

Single-Dimension arrays:Array have only in on dimensionDeclaration of single dimension array:int[] x = new int[5];

int[] x1 = new int[] { 1, 2, 3, 4, 5, 6 };

int[] x2 = { 1, 2, 3, 4, 5 };

string[] WeekDays = new string[] {"Sun","Mon","Tues","Wed","Thu","Fri","Sat" };

Example:protected void Button1_Click(object sender, EventArgs e)

{

int[] x = { 1, 2, 3, 4, 5 };

for(int i=0;i