C# NOTES
Topic Covered :
1) basic
2) UserInput
3) Variable
4) Program Management (loops,conditional statement )
5) function
6) oops (class and objects )
7) constructor
8) Inheritance
9) class seciruty(abstract and sealed class)
10) struct
11) Enum
12) C# collections(arrayList,stack,hashTable,SortedList)
13) c# generics (dynamic variable)
Dot net application development:-
1)In .net framework developed all application with the help of c# programming
C# programming language:-
1)c# programming is used to support application development environment
2)c# is provide pre statement facility
3)the extension of c# programming (.cs)
How to create first c# application in .net framework :-
1)open .net framework
2) click file menu -> click new option -> click project option -> select visual c# -> select console app(.net framework) -> write ur project name(without space) -> click browse button -> select ur folder -> click on ok
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BasicProgramme3
{
class Program
{
static void Main(string[] args)
{
}
}
}
-using is representing library.
-namespace is representing group of programme.
-class representing object oriented concept.
-mainbody representing all basic programme.
CSHARP STATEMENT :-
1) output statement
2) input statement
-hold output screen.
namespace BasicProgramme4
{
class Program
{
static void Main(string[] args)
{
//display welcome
Console.WriteLine("welcome");
//hold output screen
Console.ReadKey();
}
}
}
-in dotnet framework all purple color is representing function().
-------------------------------------------------------------------
readkey last mei hi rahega
C# VARIABLE:-
-variable is name of location where user can store different type date element.
-there are following types of variables used in c#:-
1)numerical variable - int
2)decimal variable - double
3)character variable - char
4)string variable - string
5)boolean variable (true/false)- bool/boolean
EXAMPLE:-
int a;
double b;
char c;
string city;
bool a;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Basicvariable
{
class Program
{
static void Main(string[] args)
{
int a = 10;
Console.WriteLine(a);
Console.ReadKey();
}
}
}
2)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Basicvariable
{
class Program
{
static void Main(string[] args)
{
int a = 10;
Console.WriteLine(a);
Console.WriteLine("The value of A " + a);
Console.ReadKey();
}
}
}
USER INPUT VARIABLE:-(numerical input)
-in c# programming all types of input is used to ReadLine method.(in place of prompt line)
-in c# accepting all types numerical data with the help of int.Parse()
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Basicvariable
{
class Program
{
static void Main(string[] args)
{
int age;
Console.WriteLine("Enter your age");
age = int.Parse(Console.ReadLine());
Console.WriteLine(age);
Console.ReadKey();
}
}
}
HOW TO INPUT DECIMAL DATA:-
-all types of decimal data is to be input with the help of double.Parse()
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Basicvariable
{
class Program
{
static void Main(string[] args)
{
double cost;
Console.WriteLine("Enter your cost");
cost = double.Parse(Console.ReadLine());
Console.WriteLine("Your cost is" + cost);
Console.ReadKey();
}
}
}
HOW TO INPUT STRING DATA VALUE:-
-in string data value can not used any types parse statement
-string data value directly used ReadLine statement.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Basicvariable
{
class Program
{
static void Main(string[] args)
{
string name;
Console.WriteLine("Enter your Name");
name = Console.ReadLine();
Console.WriteLine("Your name is " + name);
Console.ReadKey();
}
}
}
TASK 1:-
Input following from the user
-name
-city
-state string
-country
-branch
-subject
-mark1
-mark2
-mark3
-mark4 int
-mark5
logic-
calculate total
calculate percentage
display all information.
TASK 2:-
addition of two number
TASK 3:-
input amount from the user and calculate 18% gst and display final amount.
--------------------------------------------------------------
C# PROGRAM MANAGEMENT:-
-there are two types of management
1) condition management
2) flow management
1) condition management:-
-if else statement
-else if statement
-switch case statement
2) flow management:-
-while loop
-do while loop
-for loop
-for each loop
1)CONDITION MANAGEMENT:-
IF ELSE STATEMENT:-
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace IFELSESTATEMENT
{
class Program
{
static void Main(string[] args)
{
int age;
Console.WriteLine("Enter your age");
age = int.Parse(Console.ReadLine());
if (age>18)
{
Console.WriteLine("vote");
}
else
{
Console.WriteLine("not vote");
}
Console.ReadKey();
}
}
}
ELSE IF STATMENT:-
-it used to multiple condition.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ELSEIFSTATMENT
{
class Program
{
static void Main(string[] args)
{
int per;
Console.WriteLine("enter your percentage");
per = int.Parse(Console.ReadLine());
if (per>60)
{
Console.WriteLine("first division");
}
else if(per>50 && per<60)
{
Console.WriteLine("second division");
}
else if(per>40 && per<50)
{
Console.WriteLine("third division");
}
else
{
Console.WriteLine("fail");
}
Console.ReadKey();
}
}
}
SWITCH CASE STATMENT:- (option mei string lagega)
-this statement is used to option base programming.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SWITCHPROGRAM
{
class Program
{
static void Main(string[] args)
{
int ch;
Console.WriteLine("enter your choice");
ch = int.Parse(Console.ReadLine());
switch(ch)//switch only used choice variable
{
case 1: Console.WriteLine("one");
break;
case 2: Console.WriteLine("two");
break;
case 3: Console.WriteLine("three");
break;
default: Console.Write("invalid choice ");
break;
}
Console.ReadKey();
}
}
}
-------------------------------------------------------------
class missed
2) flow management:-
*While loop:-
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace While
{
class Program
{
static void Main(string[] args)
{
int i=1;
while(i<=10)
{
console.WriteLine(i);
}
Console.ReadKey();
}
}
}
*Do while loop:-
-Exit loop
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace doWhile
{
class Program
{
static void Main(string[] args)
{
int i = 1;
do
{
Console.WriteLine(i);
i = i + 1;
} while (i<= 10);
Console.ReadKey();
}
}
}
* for loop:-
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace doWhile
{
class Program
{
static void Main(string[] args)
{
int i;
for(i=1; i<=10; i++)
{
Console.WriteLine(i);
}
Console.ReadKey();
}
}
}
Task1:-
sum of 1 to 10 number
Task2:-
display 1 to 10 even number
Task3
1 to 10 odd number
Task4:-
input any number from the user and display table of given number
Task5:-
input any number from the user and display table of specific format
format -
2 * 1 = 2
2 * 2 = 4
2 * 3 = 6 ....
* Function:-(most important)
-Function is a small job program
Function terminologies:-
function separator()
eg-
display()
*How to create a function :-
Syntax:-
type functionName()
{
}
- Type:-
-Type is representing types of function (int , string, double)
-by default all function is to be created void type
eg-
void display()
{
}
void show()
{
}
Note:- All function is to be created before the main body
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace function
{
class Program
{
// create a function
void display()
{
Console.WriteLine("welcome");
}
static void Main(string[] args)
{
}
}
}
*How to access function():-
-In javascript function is aaccess using button control with on click event
-In c# access all the function using object
-All object is to be created inside the main body
Syntax:-
className objectName = new className();
-new is called allocation operator
*Access function with object:-
-Access all function with object using dot(.) operator
- dot is called member access operator
-Syntax:-
objectName.functionName();
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace function
{
class Program
{
// create a function
void display()
{
Console.WriteLine("welcome Nagpur");
}
static void Main(string[] args)
{
//create object
Program p1 = new Program();
p1.display();
Console.ReadKey();
}
}
}
--------------------------------------------------------------------
FUNCTION PARAMETER:-
-parameter is special type of variable used in function().
-there are some types of parameter used in function().
1) receiver parameter-(at the time of function creation)
2) sender parameter -(at the time of function calling (access))
1) receiver parameter-
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace FUNCTIONPARAMETER
{
class Program
{
void input(int a)//receiver parameter
{
Console.WriteLine(a);
}
static void Main(string[] args)
{
Program p1 = new Program();
p1.input(20);//sender parameter
Console.ReadKey();
}
}
}
USER INPUT PARAMETER:-
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace USERINPUTPARAMETER
{
class Program
{
void input(int n)
{
Console.WriteLine(n);
}
static void Main(string[] args)
{
Program p1 = new Program();
int a;
Console.WriteLine("Enter any number");
a = int.Parse(Console.ReadLine());
p1.input(a);
Console.ReadKey();
}
}
}
FUNCTION WITH IN A FUNCTION CONCEPT:-
-in this concept user can access one function to other function.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace FUNCTIONWITHNFUNCTION
{
class Program
{
void display()
{
Console.WriteLine("Nagpur");
show();
}
void show()
{
Console.WriteLine("Pune");
}
static void Main(string[] args)
{
Program p1 = new Program();
p1.display();
Console.ReadKey();
}
}
}
assignment 1:-
write a program to generate following result.
create a function- input()
create a parameter - roll, name, city, m1,m2,m3,m4,m5
logic:-
1) all parameter given by the user.
2)calculate total if all mark > 40
3) check the condition
total> 250 - A grade
total >150< 250 - B grade
otherwise fail.
Assignment 2:-
create a your own ideas using function parameter
doubts :-
if all mark > 40 ye kaise likhenge
-------------------------------------------------------------
* Local and Global variable concept:-
- Local (inside the block)
- global (outside the block)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace LOCALGLOBAL
{
class Program
{
//global area
int a1;
void input(int a)
{
//pass local variable to global varial
a1 = a;
}
void display()
{
//print global variable
Console.WriteLine(a1);
}
static void Main(string[] args)
{
//create object
Program p1 = new Program();
p1.input(10);
p1.display();
Console.ReadKey();
}
}
}
HOW TO PASS DATA BETWEEN FUNCTION TO MAIN BODY:-
-in this concept using return keyword.
-if we have use return keyword than not used void statement.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace PASSBETWEENFUNCTIONBODY
{
class Program
{
int addition(int a, int b)
{
return a + b;
//return result to main body
}
static void Main(string[] args)
{
Program p1 = new Program();
int result;
result = p1.addition(10, 20);
Console.WriteLine(result);
Console.ReadKey();
}
}
}
TASK-1
-write a program to generate following result.
-create a function input().
-create a parameter- id, name, salary, HRA, DA
-create a other function grade()
-accept only totalsalary parameter and check the condition
totalsalary>5000 than display A GRADE employ and provide 5% incriment.
totalsalary>2000 AND <5000 B grade employee and provide 2% incriment.
otherwise display new joining staff and print salary value as it i
-create a other function display var all the emplyee detail with grade or incriment. (loacal global)
TASK-2
-implement any two ideas using return keyword.(return statment)
-rewised object concept
-rewised function with parameter
--------------------------------------------
OBJECT ORIANTED PROGRAMMING:- VVIMP
-object oriented programming based on class and object
WHAT IS CLASS:-
-Class is a collection of member and method
*MEMBER:-
all types of variable inside the class is called member
*method:-
-all types of function inside the class is called method
-all types of classes is to be created class keyword.
SYNTEX:-
class name
{
member
method()
}
ex.
class student
{
int role; --member
void display() ----function
{
}
}
RULES AND REGULATION:-
-All class is to be before existing class.
-inside the class all member and method(public specifier)
-all class is to be access with object
-existing class can not used object oriented programming.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CLASS
{
//Create a class
class student
{
//method
public void display()
{
Console.WriteLine("this is my 1st class program");
}
}
class Program
{
static void Main(string[] args)
{
//create object
student s1 = new student();
s1.display();
Console.ReadKey();
}
}
}
HOW TO PASS PARAMETER INSIDE CLASS METHOD:-
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace PASSPARAMETER
{
class emplyee
{
public void input(int a)
{
Console.WriteLine(a);
}
}
class Program
{
static void Main(string[] args)
{
emplyee e1 = new emplyee();
e1.input(10);
Console.ReadKey();
}
}
}
*USER INPUT PARAMETER IN CLASS and OBJECT:-
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace userInputParameterInsideClass
{
class student
{
public void input(int n)
{
Console.WriteLine(n);
}
}
class Program
{
static void Main(string[] args)
{
student s1 = new student();
int a;
Console.WriteLine("Enter any number");
a = int.Parse(Console.ReadLine());
s1.input(a);
Console.ReadKey();
}
}
}
Task1:-
write a program to generate fallowing result
create a class -> student
create a method -> input()
create a parameter -> role,name,city
create an other method -> college()
create a parameter -> branch,semister,total,per
create an other method -> display()
Logic:-
1) all parameter input by the user
2) all information is to be display inside the display function
3) check the condition inside display()
total > 250 -> A grade
total > 150 && total < 250 -> B grade
otherwise fail
Hint: Implement with local and global concept
Task2 :-
Task1:-
write a program to generate fallowing result :-
create a function input()
create a parameter - id,name,salary,hra,DA
create an other function grade()
accept only totalSalary parameter
and check the condition
totalSalary > 5000 then display A grade employee and provide 5% increament
totalSalary > 2000 then && < 5000 display B grade employee and provide 2% increament
otherwise display new joining staff and print salary value as it is
create an other function display where display all the employee detail with grade and increament
Logic:- implement using class and object
----------------------------------------------------------------
* Object oriented programming pillars(Property):-
-There are fallowing pillars used in object oriented property
1)Constructor
2)Inheritance
3)Data Encapsulation
4)Class security
5)Polymorphism
6)Interface
1))) CONSTRUCTOR:-IMP
-constructor is property of object orient property.
-constructor is a object initialization process.
-constructor can not used .DOT operator.
*PROPERTY OF CONSTRUCTOR:-
-in constructor class name or method name similar.
EX. class student
{
public void student()
{
}
}
- constructor cannot used void statement.
EX. Class student
{
public student()
{
}
}
-constructor always used public specifier.
EX.
class student
{
public student()
{
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace PROPERTY_CONSTRUCTOR
{
class student
{
public student()
{
Console.WriteLine("this is constructor");
}
}
class Program
{
static void Main(string[] args)
{
student s1 = new student();
Console.ReadKey();
}
}
}
*TYPES OF CONSTRUCTOR:-
-DEFUALT CONSTRUCTOR.
-PARAMETERIZED CONSTRUCTOR.
-CONSTRUCTOR WITH DEFAULT ARGUMENT.
-COPY CONSTRUCTOR.
1) DEFUALT CONSTRUCTOR:-
- in this types of constructor cannot used parameter.
- in above program we have to use default constructor.
2) PARAAMETERIZED CONSTRUCTOR:-
-in this types of constructor using parameter.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace PARAAMETERIZED_CONSTRUCTOR
{
class student
{
public student(int a)
{
Console.WriteLine(a);
}
}
class Program
{
static void Main(string[] args)
{
student s1 = new student(10);
Console.ReadKey();
}
}
}
3) CONSTUCTOR WITH DEFAULT ARGUMENT:-
- in this types constructor parameter is used some default value.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CONSTUCTOR_WITH_DEFAULT_ARGUMENT
{
class student
{
public student(int a=20)
{
Console.WriteLine(a);
}
}
class Program
{
static void Main(string[] args)
{
student s1 = new student();
Console.ReadKey();
}
}
}
TASK 1:-
generate the following result.
-select your option
1.default constructor
2.parameterized constructor
3.constructor with default argument
4.exit
ENTER your choice.
(do while loop)
TASK 2:-
convert parameterized constructor with user input
(name, city , state, country, age)
4) COPY CONSTRUCTOR:-
-
---------------------------------------------------------------
(balance topic)
2)))INHERITENSE:-
**Inheritance:-
-Inheritance is a reusability of class
- In inheritance using multiple classes
-Types of class in inheritance
There are some types of classes used in inheritance
1)Base class
2)Derived class
class A
{
}
class B
{
}
class C
{
}
-All previous class is called base class
eg- B,A base class for c
*Derived class :-
- After the base class the class is called derived class
eg- B,C derived class
-Inheritance operator:-
-Inheritance operator is called colon(:)
**Types of inheritance:-
1) single inheritance
2) multi-level inheritance
3) Multiple inheritance (not supported)
4) hierarchical inheritance
1) Single inheritance:-
-In this type of inheritance only use one base class and one derived class
eg-
class A -> base
class B -> derived
syntax:-
DerivedClass : baseClass
** Rules for inheritance:-
1) In inheritance create only last class object
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace singleInheritance
{
class A
{
public void display()
{
Console.WriteLine("This is base class");
}
}
class B : A //This is inheritance
{
public void show()
{
Console.WriteLine("This is derived class");
}
}
class Program
{
static void Main(string[] args)
{
B b1 = new B();
b1.show();
b1.display();
Console.ReadKey();
}
}
}
2) Multi-level inheritance:-
-Each class is to be access own base class
class A
class B -A
class C - B
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace multiLevelInheritance
{
class A
{
public void display()
{
Console.WriteLine("This is A class");
}
}
class B : A
{
public void show()
{
Console.WriteLine("This is B class");
}
}
class C : B
{
public void print()
{
Console.WriteLine("This is C class");
}
}
class Program
{
static void Main(string[] args)
{
C c1 = new C();
c1.display();
c1.show();
c1.print();
Console.ReadKey();
}
}
}
3) Heirarchical Inheritance:-
-In this type of inheritance all derived class is to be access top of the class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace multiLevelInheritance
{
class A
{
public void display()
{
Console.WriteLine("This is A class");
}
}
class B : A
{
public void show()
{
Console.WriteLine("This is B class");
}
}
class C : A
{
public void print()
{
Console.WriteLine("This is C class");
}
}
class Program
{
static void Main(string[] args)
{
C c1 = new C();
B b1 = new B();
c1.display();
b1.show();
c1.print();
Console.ReadKey();
}
}
}
4) Multiple Inheritance(not supported):-
-If we have to use multiple inheritance then using interface
-In multiple inheritance one derived class is to be access by multiple base class
Class A
class B
class C: A,B
Task1:-
create a class -> student
create a method -> input()
create a parameter -> role,name,city
create an other class -> collge
create a method -> display()
display all the information using college class using inheritance
----------------------------------------------------
HOW TO IMPLMENT MULTIPLE INHERITANCE :- IMP
-implementation of multiple inheritance using interface concept
-interface use to be created using interface keyword.
-inside the interface only declare the function(cannot created)
-inside the interface all function is to declared (public)
-interface function is to be created inside the class.
-interface use to be access inside the class using : operator
SYNTEX:-
interface name
{
function declaration
}
EX.
- cannot use public specifier.
interface student
{
void display(); //(declartion)
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace IMPLMENT_MULTIPLE_INHERITANCE
{
interface student
{
void display(); // declartion
}
interface collage
{
void show();
}
class my : student,collage // this is concept of multiple ineheritance
{
public void display()
{
Console.WriteLine("this is first interface");
}
public void show()
{
Console.WriteLine("this is second interface");
}
}
class Program
{
static void Main(string[] args)
{
//create object
my m1 = new my();
m1.display();
m1.show();
Console.ReadKey();
}
}
}
2) CLASS SECURITY:-
-there are two basic method used in class security concept.
1. Abstract class
2. sealed class
que. different between abstract and sealed classes?
1) ABSTRACT CLASS:- V IMP
- abstract class is to be created abstract keyword.
- abstract class cannot a create a object.
- abstract class only inherite to an other classes.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ABST_CLASS
{
abstract class student //this is abstract class
{
public void display()
{
Console.WriteLine("this is first class");
}
}
class colleage : student // abstract class only inherite
{
public void show()
{
Console.WriteLine("this is second class");
}
}
class Program
{
static void Main(string[] args)
{
// student s1 = new student(); not create object abstract class
colleage c1 = new colleage();
c1.display();
c1.show();
Console.ReadKey();
}
}
}
2) SEALED CLASS:-
-sealed class is to be created using sealed keyword.
-sealed class only access with object.
-sealed class cannot inherit to an other class.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SELD_CLSS
{
sealed class student // this is sealed class
{
public void show()
{
Console.WriteLine("this is first class");
}
}
class colleage // : student - cannot ineheiate
{
public void display()
{
Console.WriteLine("this is second class");
}
}
class Program
{
static void Main(string[] args)
{
student s1 = new student();
colleage c1 = new colleage();
s1.show();
c1.display();
Console.ReadKey();
}
}
}
TASK 1:=
-implementation of interface in student marks assignment.
TASK 2:=
-implementation of abstract class in marks student.
-------------------------------------------------------
C# STRCUTURE:- IMP
-structure is collection of different type of data value.
-structure is to created struct keyword.
-structure is to be created before the main class.
-inside the structure member only use public specifier.
-all structure member is to access with object.
SYNTEX:-
struct name
{
member
}
HOW TO CREATE STUCTURE OBJECT:-
-
structure objectname;
SYNTEX:-
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace C_stct
{
//create structure
struct student
{
public int roll;
public string name;
};
class Program
{
static void Main(string[] args)
{
//create structure object
student s1;
s1.roll = 101;
s1.name = "mahesh";
Console.WriteLine(s1.roll);
Console.WriteLine(s1.name);
Console.ReadKey();
}
}
}
USER INPUT DATA IN STRUCTURE MEMBER:-
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace USER_INPUT_DATA_IN_STRUCTURE_MEMBER
{
struct student
{
public int roll;
};
class Program
{
static void Main(string[] args)
{
student s1;
Console.WriteLine("Enter roll number: ");
s1.roll = int.Parse(Console.ReadLine());
Console.WriteLine(s1.roll);
Console.ReadKey();
}
}
}
HOW TO PASS STRUCTURE IN FUNCTION PARAMETER:-
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HOW_TO_PASS_STRUCTURE_IN_FUNCTION_PARAMETER
{
struct student
{
public int roll;
};
class Program
{
void display(int r1)
{
Console.WriteLine(r1);
}
static void Main(string[] args)
{
student s1;
Program p1 = new Program();
Console.WriteLine("Enter roll number: ");
s1.roll = int.Parse(Console.ReadLine());
p1.display(s1.roll);
Console.ReadKey();
}
}
}
HOW TO PASS STRUCTURE MEMBER INSIDE THE CLASS METHOD:-
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HOW2PASS_STRUCTURE_MEMB_INSIDE_THE_CLASS
{
struct Student
{
public int roll;
};
class College
{
public void display(int r1)
{
Console.WriteLine(r1);
}
}
class Program
{
static void Main(string[] args)
{
Student s1;
College c1 = new College();
Console.WriteLine("Enter roll no.");
s1.roll = int.Parse(Console.ReadLine());
c1.display(s1.roll);
Console.ReadKey();
}
}
}
TASK:-
1) implementation of structure inheritance
2)implementation of structure in constructor?
--------------------------------------------------------------
ENUM DATA TYPE:- imp
-Enum is provide static(fixed) data value.
-Enum is used to create Enum keyword.
-Enum is used to be create before the main class
Syntex:=
Enum name
{
value
};
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ENUM
{
enum city
{
pune,
mumbai,
nagpur
}
class Program
{
static void Main(string[] args)
{
//access enum data value
Console.WriteLine(city.nagpur);
Console.WriteLine(city.mumbai);
Console.WriteLine(city.pune);
Console.ReadKey();
}
}
}
C# COLLECTION :- VVIMP
-collection is special type of library is used to manage group of data element.
-there are following collections used in c#.
1)Arraylist
2)stack
3)hashtable
4)shortedlist
-Jab bhi ye 4 use honge tb collection libarary lagegi.
1)Arraylist :-
-arraylist is provide data collection in a sequential format.
- in c# programming create object looping with the help of for each loop.
syntax:-
foreach(var in object)
{
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
//include collection libarary
using System.Collections;
namespace ARRAYLIST
{
class Program
{
static void Main(string[] args)
{
// create arraylist
ArrayList a1 = new ArrayList();
//store data in arraylist (using add function)
a1.Add(40);
a1.Add(2);
a1.Add(20);
a1.Add(15);
a1.Add(11);
//display number of count
Console.WriteLine(a1.Count);
//display arraylist element
foreach(int i in a1)
{
Console.WriteLine(i);
}
//arrange arreylist element(using sort function)
a1.Sort();
Console.WriteLine("after arranage element");
foreach(int j in a1)
{
Console.WriteLine(j);
}
// remove element in arraylist(using remove function)
a1.Remove(15);
Console.WriteLine("after remove element");
foreach(int k in a1)
{
Console.WriteLine(k);
Console.ReadKey();
}
}
}
TASK 1:-
create user define Arraylist concept and the result is
enter no. of element-5
enter no. -10
enter no.-50
enter no.-34
enter no.-56
enter no.-77
display element
10
50
34
56
77
enter remove element-50
after remove element
10
34
56
77
do you want to arrange element (1,0)-1 is called true
10
34
56
77
-----------------------------------------------------------------
2) STACK:-
-stack is used to display data in lifo format.
-lifo is called last in first out method.
-in stack add data element using push function.
-In stack removed element using pop function.
product key:-
NJVYC-BMHX2-G77MM-4XJMR-6Q8QF
A,B,C,D
D,C,B,A - result
-remove 1st element D:-
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;
namespace STACK
{
class Program
{
static void Main(string[] args)
{
//Create stack object
Stack s1 = new Stack();
//add data element(push function)
s1.Push('A');
s1.Push('B');
s1.Push('C');
s1.Push('D');
//Display stack Element
foreach(char CH in s1)
{
Console.WriteLine(CH);
}
//Remove stack element(pop function)
s1.Pop();
s1.Pop();//pop is removing only 1 element at a time
Console.WriteLine("after remove Element");
foreach(char A1 in s1)
{
Console.WriteLine(A1);
}
Console.ReadKey();
}
}
}
TASK 1:-
-write a program to generate following result.
-enter number of element in stack- 5
enter character- A
enter character-b
enter character-c
enter character-d
enter character-e
select your choice
1.remove
2.display
enter your choice-1
enter no. of element deleted-2
Ans- c,b,a
enter your choice-2
print- e,d,c,b,a
3) HASHTABLE:-
-Arraylist is used to store data in sequential format.
-stack is used to store data in lifo format.
-Hashtable is used to store data in particular key (index).
EX.
101-Nagpur yes
102-Pune
103-Mumbai
-hashtable is used to display data in random format.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;
namespace HASHTABLE
{
class Program
{
static void Main(string[] args)
{
//create hashtable object
Hashtable s1 = new Hashtable();
//insert element in hashtable
s1.Add("101","Nagpur");
s1.Add("102","Pune");
s1.Add("103", "Mumbai");
s1.Add("104","Delhi");
s1.Add("105","Hyderabad");
//Display hashtable element
foreach(string H1 in s1.Keys)
{
Console.WriteLine(H1 +"-"+ s1[H1]);
}
Console.ReadKey();
}
}
}
-------------------------------------------------------------
SORTED LIST:-
-sorted list is used to arrange data element automatically
-sorted list is used to pair object.
-pair object is special type of concept is used to accepting multi type data value inside the class.
-pair object is representing <>.
NOTE:-
-if we have to use for each loop in pair object than using dynamic variable
//dynamic variable is representing var keyword
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;
namespace sortedList
{
class Program
{
static void Main(string[] args)
{
//create sorted list object
SortedList<int, string> s1 = new SortedList<int, string>();
// Insert data element in sortedList
s1.Add(3,"Nagpur");
s1.Add(1, "Puna");
s1.Add(5, "Mumbai");
s1.Add(2, "Indore");
//if we have to use foreach loop in pair object then using dynamic variable
//dynamic varaible is representing 'var' keyword
//display sortedList element
foreach(var k1 in s1) // var will read both data element (int ,string)
{
Console.WriteLine(k1.Key); // print only keys
}
//print both key value
foreach (var k1 in s1)
{
Console.WriteLine(k1.Key + " - " + k1.Value);
}
Console.ReadKey();
}
}
}
________________________________________________________________
C# GENERICS:- VVIP
-Generic is concept used in class or method.
-generic is special type of variables is used to store random data.
-generic is representing <T> inside the bracket.
IMPLEMENTAION OF GENERICS:-
-there are two implenatation used in generics
1) generics in class (generic class)
2) generics in method (generic method)
1)GENERICS CLASS:- (gro class name save file)
-generic class is used to constructor method.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace GRO_CLASS
{
class genericclass<T> //generic class
{
public genericclass(T msg) //this is constructor / msg is generic variable store random data
{
Console.WriteLine("msg "+ msg);
}
}
class Program
{
static void Main(string[] args)
{
genericclass<int> g1 = new genericclass<int>(20);
genericclass<string> s1 = new genericclass<string>("welcome home");
Console.ReadKey();
}
}
}
________________________________________________________
DOUBT SESSION:=
-abstract and sealed class with assignment
-collection with assignment
-inheritance with assignment
---------------------------------------------------------------------
DOUBT SESSION:-
que:-
generate the following result.
-select your option
1.default constructor
2.parameterized constructor
3.constructor with default argument
4.exit
class student
{
public student()
{
console.wr("default construction");
}
}
class employee
{
public employee(int a)
{
c.s(a);
}
}
class college
{
public college(int b=10)
{
}
}
main body=
{
int ch;
c.s("any choice");
ch=int.parse(c.s readline());
switch(ch)
{
case 1:c.s default constructor
student s1=new student();
break;
case 2:c.s parametrized constructor
employee e1=new employee(20);
break;
case 3:c.s default argument
college c1=new college();
break;
default c.s(inavlid)
break;
}
}
shortcut :=
class student
{
public student()
{
c.s (default);
}
public student(int a)
{
c.s (a);
}
public student(int b=20, int c=30)
{
c.s(b,c);
}
}
main body:=
{
int ch
c.s(enter your choice);
ch=int.parse(c.s readline());
switch(ch)
{
case 1: default
student s1=new student();
break;
case 2:parametrized
student s2=new student(20);
break;
case3:
student s3=new student(60,70);
break;
default: invalid
}
}
----------------------------------------------------------------
doubt 2:-
q. arreylist user input
ans:-
include collection
main body=
{
int n, data;
c.s("enter last number-");
n=int.parse(c.s readline());
ArrayList a1=new ArrayList();
for(int i=1; i<=n; i++)
{
c.s("enter any number-");
data=int.parse(c.s readline());
a1.Add(data)
}
}
________________________________________________________________
doubt 3:-
real uses abstract and sealed class :-
ans=
all part are implemented in web application development environment like data base, web pages, ap eyes, etc
__________________________________________________________________
doubt 4:-
q.
why void not use in constructor:-
ans:-
a=10
b=20
c=a+b
a=10,b=20,c=a+B
class a
{
public void display()
{
}
}
public a()
{
//data base path
}
main body=
{
a a1= new a();
a1.display(); //data base path
steps tep to read the logic:-
crate a class object and search the class
verify a1 object
find out the a1 object
go to the a class
find out display function
construction step to read the logic:-
crate object directly find construction method
a a1=new a();
}
_________________________________________________________________
ADVANCE C# :-
-namespace
-acception imp
-over loading
-variable properties.
-file handling.
** NameSpace(imp):-
- namespace is a Collection of classes
- namespace is to be created using 'namespace' keyword
- namespace create before the existing class
syntax:
namespace name
{
// create a class
}
Eg-
namespace college
{
class A
{
// create method
}
}
** how to create the object of class inside namespace :
syntax:
namespacename.className objectName = new namespacename.className();
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace nameSpace_
{
namespace College
{
class A
{
public void display()
{
Console.WriteLine("First class");
}
}
class B
{
public void show()
{
Console.WriteLine("Second class");
}
}
}
class Program
{
static void Main(string[] args)
{
// A a1 = new A(); can't create the direct object of class inside the namaespace
College.A a1 = new College.A();
College.B b1 = new College.B();
a1.display();
b1.show();
Console.ReadKey();
}
}
}
** Uses of Namespace:-
Create a fallowing classes of student management system:
1) class SubmitForm
2) class viewResult
3) class submitExam
4) class HoDApproval
5) class viewForm
6) class examCell
7) class uploadResult
namespace student
{
class submitForm
class viewResult
}
namespace HOD
{
class HODApproval
class viewForm
}
namespace Exam
{
class ExamCell
class uploadResult
}
Task1:
create a namespace - Student
create a class - submitForm
create a method - input()
create a parameter - roll,name,city,country,college ,branch
create an other namespace - examCell
create a class - markDetail
create a method - submitMark()
create a parameters - m1,m2,m3,m4,m5
create a method - gradeCalculate()
logic1:
calculate total
logic2:
check the condition
total > 250 - 'A'
total > 150 && toal < 250 - 'B'
otherwise Fail
create an other method - display()
display all the student information using this function
doubt=1
class markDetail : Student.submitForm
nameSpaceAss1.Student.examCell.markDetail d1 = new nameSpaceAss1.Student.examCell.markDetail();
//nameofnamespace.classname object=newspacenameofnamespace.classname();
student.submitform s1 = new student.submitform();
______________________________________________________________
** Exception(VIP):-
1) console.WriteLine("Welcome") -> syntax error
2)
a = 10;
b = 0;
c = a/b; -> logical error
-Exception is representing logical error in ur program
-In c# programming exception is predefined class where store all types of exception massages
-There are fallowing exception handling statement used in a program
1) try catch
2) finally
3)throw
There are two type of exception in c#
1) built in exception
2) user defined exception
1) Built in exception:
- It's already stored in a exception classes
2)user defined Exception:
- create by the user
1) TRY CATCH STATEMENT:-
-trycatch is a acception handling statement.
-in trycatch statement all types of logic use inside the try block.
-catchblock is representing all types of logical error.
SYNTEX:-
try
{
//Crete logic
}
catch(Exception obj)
{
// display logical error
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace TROG_catch
{
class Program
{
static void Main(string[] args)
{
//exception program
int a = 10, b=2, c;
try
{
//create logic
c = a / b;
Console.WriteLine(c);
}
catch(Exception obj)
{
//display error
Console.WriteLine(obj);
}
Console.ReadKey();
}
}
}
2) THROW:-
HOW TO DISPLAY MEASURE MASSAGE IN EXCEPTION:-
-by default all exception is to be display predefined massage.
-if user can print own exception massage than using throw keyword.
SYNTEX:-
throw new Exception("massge");
ex.
throw new Exception("Number not divisible by 0");
note:- trch implement in folder
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace TRch_implement
{
class Program
{
static void Main(string[] args)
{
int a, b, c;
Console.WriteLine("Enter number");
a = int.Parse(Console.ReadLine());
Console.WriteLine("Enter any number");
b = int.Parse(Console.ReadLine());
try
{
if(b==0)
{
throw new Exception("Number not divisible by 0");
}
c = a / b;
c = int.Parse(Console.ReadLine());
}
catch(Exception obj)
{
Console.WriteLine(obj);
}
Console.ReadKey();
}
}
}
TASK-1
-create any 5 program using throw exception.
3) FINALLY:-
-finally is an other type of exception statement.
-finally can not related to exception.
SYNTEX:-
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace TRch_implement
{
class Program
{
static void Main(string[] args)
{
int a, b, c;
Console.WriteLine("Enter number");
a = int.Parse(Console.ReadLine());
Console.WriteLine("Enter any number");
b = int.Parse(Console.ReadLine());
try
{
if(b==0)
{
throw new Exception("Number not divisible by 0");
}
c = a / b;
c = int.Parse(Console.ReadLine());
}
catch(Exception obj)
{
Console.WriteLine(obj);
}
finally
{
Console.WriteLine("this is my program");
}
Console.ReadKey();
}
}
}
_______________________________________________________________
USER DEFINE EXCEPTION:-
-in this type of exception user can create own exception statement.
-user define exception stored in exception classes.
-exception class is directly access exception massages.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace USER_DEFINE_EXCEPT
{
//step1-inherit exception class insight the user define exception
public class InvalidAgeException:Exception
{
//step2- create parameterized constructor
public InvalidAgeException(string msg) :base(msg) // step-3 using base keyword for transfering msg varible to Exception class
{
//base keyword automatic find out base classes
}
}
public class TestUserDefineException
{
public void ValidAge(int age)
{
if(age < 18)
{
throw new InvalidAgeException("please input proper Age");
}
}
}
class Program
{
static void Main(string[] args)
{
TestUserDefineException T1 = new TestUserDefineException();
try
{
T1.ValidAge(11);
}
catch(InvalidAgeException obj)
{
Console.WriteLine(obj);
}
Console.ReadKey();
}
}
}
TASK-1
write a program to implement any 3 user define exception in single program.
Task-2
implantation of constructor using try catch statement
Task-3
implantation of inheritance using try catch statement
_____________________________________________________________________
FILE HANDLING SYSTEM :- imp
-file is collection of information in proper manner.
-file handling is system is based on user define file.
-there are some library is used to file handling method.
library:-
using system.io
-there are following object is to be created file system.
1) FileStream(create a file)
2) StreamWriter (write any types of data using Stream)
3) StreamReader (read any types of data using Stream)
4) TextWriter (write any types of data direct to the file)
5) TextReader (read any types of data direct to the file)
1) FileStream:-
-filestream object is basically used to create a file in computer memory.
-stream is called cell.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
//step no.1 include file libarary
using System.IO;
namespace filestream
{
class Program
{
static void Main(string[] args)
{
//create file stream object
FileStream fs = new FileStream("D:\\demo.txt",FileMode.OpenOrCreate ); //file mode is represnting a file property(open,create,read, write)
//write bite data value inside the file
fs.WriteByte(65); //display A in demo.text
//close file object
fs.Close();
Console.WriteLine("file create sucessfully");
Console.ReadKey();
}
}
}
que- display A to Z character in a file.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
namespace filestream_2
{
class Program
{
static void Main(string[] args)
{
FileStream fx = new FileStream ("D:\\demo1.txt",FileMode.OpenOrCreate);
for(int i=65; i<=90; i++)
{
fx.WriteByte((byte)i);
}
fx.Close();
Console.WriteLine("file create succesfully");
Console.ReadKey();
}
}
}
TASK-1
-write a program to input file name from the user and create on specific location and input ask cot from the user and display specific file.
--------------------------------------------------------------
(balance topic)
** StremWrite object:-(imp)
- In this object is used to write any types of massage to the file in a form of
stream
-drawback of this method is that it allocate multiple cell in a computer memory
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
namespace streamwriter
{
class Program
{
static void Main(string[] args)
{
FileStream fs = new FileStream("D:\\demo\\demo5.txt",FileMode.Create); // create a new file
// create StreamWrite object
StreamWriter sw = new StreamWriter(fs); // streamwriter always use filestream object
// write data to file
sw.WriteLine("This is my first file handling program");
// close all objects
sw.Close();
fs.Close();
Console.WriteLine("File crreated");
Console.ReadKey();
}
}
}
** StreamReader object :-
- In this object is used to reading data from text file in a form of cell(stream)
- same drawback like StramWriter
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
namespace streamreader
{
class Program
{
static void Main(string[] args)
{
FileStream fs = new FileStream("D:\\demo\\demo5.txt", FileMode.OpenOrCreate); // open existing file
// create StreamReader object
StreamReader sr = new StreamReader(fs); // using filestream object
// create a variable (string format)
string data = sr.ReadLine(); // read data from file
Console.WriteLine(data);
// close all objects
sr.Close();
fs.Close();
Console.ReadKey();
}
}
}
** TextWriter object :-
- TextWriter object is used to write any types of massage to the file without using stream(cell)
- faster method than stream
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
namespace textWriter
{
class Program
{
static void Main(string[] args)
{
using (TextWriter tw = File.CreateText("D:\\demo\\demo6.txt"))
{
// write data
tw.WriteLine("Hello");
tw.WriteLine("Nagpur");
}
Console.WriteLine("File created successfully");
Console.ReadKey();
}
}
}
** TextReader object :-
- Read data from the file
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
namespace textReader
{
class Program
{
static void Main(string[] args)
{
using (TextReader tr = File.OpenText("D:\\demo\\demo6.txt"))
{
Console.WriteLine(tr.ReadToEnd());
}
Console.ReadKey();
}
}
}
(balane till)
----------------------------------------------------------------
//formating using \t and \n
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
//formating using \t and \n
int roll = 101;
string name = "rahul";
Console.Write("roll no. \t\t\t name \t\t\t \n");
Console.Write(roll +"\t\t\t\t"+ name );
Console.ReadKey();
}
}
}
------------------------------------------------------------
pdf by nr solution
-USING object oriented progamming concepts:-
ops ke logic
jo logic se woh bana sakte
try catch
exceptional handling
using trough statement
use comment line compulsory
grade calculation same condition (total> 250-A GRADE)
TOTAL>150 and <250 otherwise fail
implantation of choice 5-
1.display marksheet
2.enter your file name- my.txt
than massage print= your marksheet is to be display inside the my.text file and pls check my.text file in your d-drive.
formatting:-
space ke \t
console.writeline("name\t city \t")
newline \n
file ka logic lagega textwriter lagega.
3.
Comments
Post a Comment