Search This Blog

c# notes or c -sharp notes

C# ( C SHARP)

VARIABLE:
IT IS A MEMORY LOCATION WHICH IS USED TO STORE OR HOLD THE VALUE.
THE VALUE WHICH IS ENTRED IS NOT FIXED IT VARIES.

DATATYPE:
IT IS USED TO REPRESENT WHAT TYPE OF DATA THAT THE USER IS GOING TO ENTER.
THEY ARE
Byte - Byte byte An integer from 0 to 255.
Int16 -Short short An integer from –32,768 to 32,767.
Int32 -Integer int An integer from –2,147,483,648 to 2,147,483,647.
Int64 - Long long An integer from about –9.2e18 to 9.2e18.
Single -Single float A single-precision floating point number from approximately –3.4e38 to 3.4e38.
Double -Double double A double-precision floating point number from approximately –1.8e308 to 1.8e308.
Decimal- Decimal decimal A 128-bit fixed-point fractional number that supports up to 28 significant digits.
Char -Char char A single 16-bit Unicode character.
String -String string A variable-length series of Unicode characters.
Boolean -Boolean bool A true or false value.
DateTime- Date *

* If the language does not provide an alias for a given type, you can just use the .NET class name.
Represents any date and time from 12:00:00 AM,
January 1 of the year 1 in the Gregorian calendar, to 11:59:59 PM, December 31 of the year 9999.
Time values can resolve values to 100 nanosecond increments. Internally, this data type is stored as a 64-bit integer.
TimeSpan * * Represents a period of time, as in ten seconds or three days. The smallest possible interval is 1 tick (100 nanoseconds).
Object Object object The ultimate base class of all .NET types. Can contain any data type or object.

OPERATORS:
AN OPERATOR IS ONE WHICH OPERATES ON OPERANDS OR VARIABLES. THEY ARE
• ARITHMETIC OPERATOR
• RELATIONAL OPERATOR
• LOGICAL OPERATOR
• ASSIGNMENT OPERATOR
• INCREMENT AND DECREMENT OPERATOR
• TERNARY OR CONDITIONAL OPERATOR
• BITWISE
• COMMA

ARITHMETIC: USED TO PERFORM ARITHMETIC OPERATIONS. + , - , * , / , %
RELATIONAL OPERATOR OR COMPARISON OPERATOR: > ,<, >= ,<= ,!= ,== IT PRODUCES BOOLEAN VALUES . THAT IS TRUE OR FALSE. LOGICAL OPERATOR: && (LOGICAL AND) || (LOGICAL OR) ! (LOGICAL NOT) TRUE && TRUE = TRUE TRUE && FALSE =FALSE FALSE && TRUE=FALSE FALSE && FALSE= FALSE NOTE: BOTH THE CONDITIONS MUST BE TRUE TO OBTAIN FINAL VALUE TRUE. || (LOGICAL OR) ANY ONE OF THE CONDITION MUST BE TRUE TO OBTAIN FINAL VALUE TRUE. TRUE || TRUE = TRUE TRUE || FALSE = TRUE FALSE || TRUE = TRUE FALSE || FALSE = FALSE ! (LOGICAL NOT) IT PRODUCES OPPOSITE RESULT. ! ( TRUE ) = FALSE ! ( FALSE ) = TRUE ASSIGNMENT OPERATOR: IT USES THE SYMBOL "=" . USED TO ASSIGN RIGHT HAND SIDE (RHS ) VALUE TO LEFT HAND SIDE (LHS ). EX: a =10 INCREMENT AND DECREMENT OPERATOR: INCREMENTING THE VALUE BY ONE (IT USES ++) DECREMENTING THE VALUE BY ONE (IT USES --) PRE INCREMENT AND POST INCREMENT PRE DECREMENT AND POST DECREMENT PRE INCREMENT: a=10 b=++a a=11 b=11 POST INCREMENT: a=10 b=a++ b=10 a=11 SIMILLARLY PRE AND POST DECREMENT. TERNARY OR CONDITIONAL OPERATOR: TERNARY MEANS THREE. IT CONTAINS 3 EXPRESSIONS. CONDITION, VALUE1,VALUE2 (condition)?value1:value2 IF THE CONDITION IS TRUE , THEN VALUE 1 IS TAKEN. IF THE CONDITION IS FALSE THEN VALUE2 IS TAKEN. c= (a>b)?a:b;

BITWISE OPERATOR:
IT OPERATES ON BINARY DIGITS. THEY ARE & , | , ^ , << , >>






COMMA OPERATOR: TO Access a method and class:

MICROSOFT VISUAL STUDIO
FILE --NEW---PROJECT-----SELECT ( VISUAL C#) AND CONSOLE APPLICATION
ENTER THE NAME EX: program1 (IN THE REQUIRED FOLDER)AND OK.

Console-------------> IT IS A CLASS.
WriteLine------------> METHOD OR FUNCTION

TO RUN THE PROGRAM
PRESS F5 FUNCITON KEY.
<---------------------------------------------------------------------->
NOTE: THE KEYBOARD VALUES ARE ENTERED AFTER THE EXECUTION. IT IS IN THE FORM OF STRING.
int x = int.Parse(Console.ReadLine());
double y = double.Parse(Console.ReadLine());
Console.WriteLine(x);
Console.WriteLine(y);
<-------------------------------------------------------------------------------------------------------------------------------->
Reading From the keyboard
//To accept for the console
Console.Write("Enter name : ");
string name = Console.ReadLine(); //Reads as a string from the keyboard
Console.Clear(); // clears the screen
Console.WriteLine("Name : " + name);
<---------------------------------------------------------------------------------------------------------------------------------->
//To Add two numbers
Console.Write("a = ");
int a = int.Parse(Console.ReadLine());
Console.Write("b = ");
int b = int.Parse(Console.ReadLine());
//To read other data types
//double d = double.Parse(Console.ReadLine());
//long l = long.Parse(Console.ReadLine());
//float f = float.Parse(Console.ReadLine());
int c = a + b;
//Console.WriteLine("sum of " + a + " + " + b + " = " + c);
//To change console fore and back color
Console.ForegroundColor = ConsoleColor.White;
Console.BackgroundColor = ConsoleColor.Red;
Console.WriteLine("sum of {0} + {1} = {2}",a,b,c);
<----------------------------------------------------------------------------------------------------------------------------------------------->
//WAP TO ACCEPT 2 NO. FROM THE USER, AND FIND WHICH IS THE BIG
Console.Write("a = ");
int a = int.Parse(Console.ReadLine());
Console.Write("b = ");
int b = int.Parse(Console.ReadLine());
//To read other data types
//double d = double.Parse(Console.ReadLine());
//long l = long.Parse(Console.ReadLine());
//float f = float.Parse(Console.ReadLine());
if (a > b)
Console.WriteLine(" a is big " + a);
else
Console.WriteLine(" b is big" + b);





//control Statements in C#
// to find the largest of 3 numbers
Console.Write("enter number1 : ");
int a = int.Parse(Console.ReadLine());
Console.Write("enter number2 : ");
int b = int.Parse(Console.ReadLine());
Console.Write("enter number3 : ");
int c = int.Parse(Console.ReadLine());
int i = 10;
if (a > b && a > c) // boolean expression True/Flase
{
Console.WriteLine("{0} is the largest number", a);
}
else if ( b > c)
{
Console.WriteLine("{0} is the largest number", b);
}
else
{
Console.WriteLine("{0} is the largest number", c);
}
<-------------------------------------------------------------------------------------------------------------->
SWITCH CASE :
Syntax:
switch(expression)
{
case label:
statement;
break;
case label:
statement;
break;
case label:
statement;
break;
default:
statement;
break;
}
Console.Write("Input number : ");
int number = int.Parse(Console.ReadLine());
switch (number)
{
case 1:
Console.WriteLine("You entered number : {0}", number);
break;
case 2:
Console.WriteLine("You entered number : {0}", number);
break;
case 3:
Console.WriteLine("You entered number : {0}", number);
break;
default:
Console.WriteLine("You entered number > 3");
break;
<------------------------------------------------------------------------------------------------------------->




Console.Write("Input string : ");
string choice = Console.ReadLine();
switch (choice)
{
case "one":
Console.WriteLine("You entered number : {0}",choice);
break;
case "two":
Console.WriteLine("You entered number : {0}", choice);
break;
case "three":
Console.WriteLine("You entered number : {0}", choice);
break;
default:
Console.WriteLine("choice not found");
break;
}
<-------------------------------------------------------------------------------------------------------------------------->

STRINGS:
BUILT IN FUNCTIONS:
THE FOLLOWINGS ARE THE FUNCTIONS OF string CLASS.
StartsWith()
EndsWith()
Equals()
Length()
Replace()
toUpper: THIS FUNCTIO IS USED TO CONVERT THE STRING INTO UPPERCASE.
toLower : THIS FUNCTION IS USED TO CONVERT THE STRING INTO LOWERCASE.
//declaration
string s = "Welcome to Strings in C#";
Console.WriteLine("Length : {0}", s.Length);
string upperCase = s.ToUpper();

string lowerCase = s.ToLower();
Console.WriteLine("Message : {0}", s);
Console.WriteLine("Uppercase : {0}", upperCase);
Console.WriteLine("LowerCase : {0}", lowerCase);

<----------------------------------------------------------------------------------------->
StartsWith() : THIS FUNCTION IS USED TO CHECK STRING THAT STARTS WITH.
string URL = "http://www.yahoo.com";
if (URL.StartsWith("http"))
{
Console.WriteLine("Its a HTTP request");
}
if (URL.EndsWith(".com"))
{
Console.WriteLine("Its a Commercial website");
}
<--------------------------------------------------------------------------------------->










Equals() : THIS FUNCTION IS USED TO COMPARE THE TWO STRINGS.
string s1 = "smith";
string s2 = "smith";
if (s1 == s2)
//if(s1.Equals(s2))
// if (s1.Equals("amith"))
{
Console.WriteLine("s1 == s2");
}
else
{
Console.WriteLine("s1 != s2");
}
<------------------------------------------------------------------------------------------------------------->
Replace () : THIS FUNCTION IS USED TO REPLACE THE WORD. REPLACE FUNCTION:
string message = "Google is fast, Google is Best, Google uses AJAX";
Console.Write("Enter word to search : ");
string searchWord = Console.ReadLine();
Console.Write("Enter word to replace : ");
string replaceWord = Console.ReadLine();
Console.WriteLine("Message : {0}", message);
//Replace the word
message = message.Replace(searchWord, replaceWord);
Console.WriteLine("Message after Replacing : {0}", message);
<----------------------------------------------------------------------------------------->
ITERATIVE OR REPETATIVE LOOP:
FOR LOOP
Console.WriteLine("enter the value from the keyboard");
int n = int.Parse(Console.ReadLine());
int sum, i;
sum = 0;
for (i = 1; i <= n; i++) sum = sum + i; Console.WriteLine("total sum is " + sum); output: 1+2+3+4+5 <----------------------------------------------------------------------------------------------->
WHILE LOOP
Console.WriteLine("enter the value from the keyboard");
int n = int.Parse(Console.ReadLine());
int i = 2;
while (i <= n) { Console.Write(i + " "); i += 2; } i = i +2------------------------------> i+=2
i = i *2------------------------------>i *=2
i=i / 2--------------------------------> i / = 2
i = i+1---------------------------->i++
<---------------------------------------------------------------------------------------------->








DO WHILE LOOP
Console.WriteLine("enter the value from the keyboard");
int n = int.Parse(Console.ReadLine());
int i = 1;
do
{
Console.Write(i + " ");
n = n - 1;
i = i + 1;
} while (n != 0);
<--------------------------------------------------->
using System.Collections;
FOR EACH
ArrayList numbers = new ArrayList();
for (int i = 0; i < 10; i++) numbers.Add(i); foreach (int x in numbers) Console.Write(x+" "); <-------------------------------------------------------->
ArrayList numbers = new ArrayList();
for (int i = 0; i < 10; i++) numbers.Add(10+i); foreach (int x in numbers) Console.Write(x + " "); <-------------------------------------------------------------------------->


CLASS AND OBJECT:

OBJECT ORIENTED PROGRAMMING STRUCTURE (OOPS):
CLASS AND OBJECT:
CLASS IS AN USER DEFINED DATA TYPE, WHICH CONTAINS DATA MEMBERS AND FUNCTIONS.

CLASS SAMPLE
{
INT X;---------------------------->DATA MEMBER
VOID GETDATA();----------->FUNCTION
}

FRUIT----->CLASS
MANGO, BANANA, ORANGE ETC-=---> OBJECTS

CLASS ANIMAL
{
EYE,MOUTH;--------DATA MEMBERS
SEE() FUNCTION
EAT()
}
TIGER,CAT, DOG----->OBJECTS

CLASS EXAM
{
WHAT IS C++
WHAT IS JAVA
}
ST1,ST2,ST3,ST4--------->OBJECTS
CLASS BANK
{
DEPOSIT()
WITHDRAWAL()
BALANCE()
}
CUST1,CUST2,CUST3-------->OBJECTS

CLASS IS A TEMPLATE OR BLUEPRINT OR GENERAL REPRESENATION OR COMMON REPRESENTATION OR SKELETON OF APPLICATION

OBJECT:
IT IS AN ENTITY WHICH SHARES OR DRAWS THE COMMON PROPERTIES OF THE CLASS AND CAN HAVE ITS OWN BEHAVIOUR.

PRIME CONCEPTS OF OOPS:
1. DATA HIDING
2. ABSTRACTION
3. ENCAPSULATION
4. INHERITANCE
5. POLYMORPHISM

DATA HIDING:

IN PROCEDURAL ORIENTED PROGRAMMING LANGUAGE "C", THERE IS NO SECURITY FOR THE DATA. THE DATA COULD BE ACCESSED IN ANY FUNCTION AND COULD BE MODIFIED.
DATA FLOWS FREELY AROUND THE PROGRAM.

IN ORDER TO PROVIDE EXCLUSIVELY SECURITY FOR THE DATA AND NOT FOR THE FUNCTIONS, THE PRIME CONCEPT OF OOPS , DATA HIDING CAME IN. HERE DATA IS TREATED AS
AN IMPORTANT ELEMENT OR CRITICAL ELEMENT.

ABSTRACTION OF DATA:

THE DATA MEMBER OF THE CLASS SHOULD BE USED WITH IN THE MEMBER FUNCTIONS OF THE CLASS. THE DATA IS ABSTRACTED.

ENCAPSULATION:

THE GROUPING UP OF DATA MEMBERS AND FUNCTIONS TOGETHER IS ENCAPSULATION.
IT IS USED TO SEPARTE THE ESSENTIAL ENTITIES FROM NON ESSENTIAL.

DATA MEMBERS AND FUNCTIONS ARE ENCAPSULATED TO FORM A FAMILY CALLED "CLASS"

INHERITANCE:

IT IS THE PROCESS OF CREATING NEW CLASS BY MAKING USE OF EXISTING CLASS.
IT PROVIDES A CONCEPT CALLED "REUSABILITY". RATHER THAN WRITTING THE CODE AGAIN AND AGAIN REUSE THE EXISTING PROPERTIES.
IT AVOIDS WASTAGE OF SYSTEM TIME, USER TIME AND SYSTEM MEMORY.












POLYMORPHISM:

POLY MEANS MANY AND MORPHISM MEANS FORMS. AN OBJECT CAN TAKE MANY OR MULTIPLE FORMS.

SYNTAX:
class classname
{
datamembers
functions or methods
}

ex:
class sample
{
int x;------------>DATA MEMBER
void get()-------->FUNCTION OR METHOD
{
}
}

OBJECT = DATA + METHOD


SYNTAX:
class classname
{

}

ex:
class test
{

}

CREATION OF OBJECT:
test t=new test();

ACCESS SPECIFIER:
IT IS USED TO SPECIFY THE ACCESSIBILITY OF THE MEMBERS OF THE CLASS.
THEY ARE

 PRIVATE,
 PUBLIC,
 PROTECTED
 INTERNAL

NOTE:
BY DEFAULT THE DATA MEMBERS OF THE CLASS ARE PRIVATE.














class test
{
public int x;
}
class test1
{
static void Main(String [] args)
{
test t=new test(); // t IS AN OBJECT OF CLASS test
t.x=100;
Console.WriteLine(t.x);
}
}
<-------------------------------------------------------->
class test
{
int x;
public void get()
{
x = 250;
}
public void put()
{
Console.WriteLine(x);
}

}
class test1
{
static void Main(String[] args)
{
test t = new test();
t.get();
t.put();

}
}
<----------------------------------------------------------->
class demo
{
int x, y;
public void getdata(int m,int n)
{
x = m;
y = n;
}
public void putdata()
{
Console.WriteLine(x);
Console.WriteLine(y);
}


}











class demo1
{
static void Main(String[] args)
{
demo d1 = new demo();
demo d2 = new demo();
int a = int.Parse(Console.ReadLine());
int b = int.Parse(Console.ReadLine());
int c = int.Parse(Console.ReadLine());
int d = int.Parse(Console.ReadLine());

d1.getdata(a,b );
d2.getdata(c,d );
d1.putdata();
d2.putdata();

}
}
<-------------------------------------------------------------->
class demo
{
int x, y;
public void getdata(int x,int y)
{
this. x =x;
this. y =y;
}
public void putdata()
{
Console.WriteLine(x);
Console.WriteLine(y);
}

}
class demo1
{
static void Main(String[] args)
{
demo d1 = new demo();
demo d2 = new demo();
int a = int.Parse(Console.ReadLine());
int b = int.Parse(Console.ReadLine());
int c = int.Parse(Console.ReadLine());
int d = int.Parse(Console.ReadLine());

d1.getdata(a,b );
d2.getdata(c,d );
d1.putdata();
d2.putdata();file:///G:/extra/032.jpg

}
}
<----------------------------------------------------------->


NOTE:
DATA MEMBER IRRESPECTIVE OF ANY CATEGORY CAN BE INITIALIZED.
(private,public,protected)
private int x=100;

No comments:

Post a Comment