Boxing: If A Value Type Is Converted Into Reference Type We Call This Process Boxing
Boxing: If A Value Type Is Converted Into Reference Type We Call This Process Boxing
Boxing
If a value type is converted into reference type we call this process Boxing
int x = 100;
object obj = x;
Console.WriteLine(x);
Console.ReadLine();
}
UnBoxing:
If a value type is converted into reference type and again reference type is converted into
value type it is know as UnBoxing.
string st = "100";
int x = Convert.ToInt32(st);
Console.WriteLine(st);
Console.ReadLine();
}
Data Types:
Value Type: int,byte,short
Reference Type: string,object
Access modifiers
Public
A member function or variable declared as public can be accessed from outside the class. It
means, you can access public member from anywhere.
Private
It is used to restrict the member function or variable to be called outside from the class.
Internal
The member functions and variables which are declared as Internal only can be accessible
inside the same namespace.
Protected
The Protected members can only be accessible from its child class or in same class.
Protected Internal
It can be accessible within the assembly where it is declared or in derived class of other
assembly.
OOPS:
Inheritance:
parent and child relation
Inheritance is a process of object reusability.
Abstraction:
Console.WriteLine();
namespace OOPS
{
public class User
{
public bool AddUserDetails(string name, string email)
{
//logic
return false;
}
private bool ValidateUser(string name, string email)
{
//validation logic
return true;
}
private int CreateDatabaseConnection()
{
return 1;
}
public class Program
{
static void Main()
{
User user = new User();
user.AddUserDetails("naresh","[email protected]");
Console.ReadLine();
}
Encapsulation:
Encapsulation is like enclosing in a capsule. That is enclosing the related operations and data
related to an object into that object.
class User
{
public bool AddUser(string name, string email, string phone)
{
if (ValidateUser(name, email, phone))
{
if (AddtoDb(name, email, phone) > 0)
{
return true;
}
}
return false;
}
class Program
{
static void Main(string[] args)
{
User objUser = new User();
bool f = objUser.AddUser("Arka", "[email protected]", "1234567890");
}
}
Polymorphism
Method Overloading:
It is an approach of defining a method with multiple behaviours where
behaviours of methods will be changing based on the parameters of the
method
public class Program
{
public void Test()
{
Console.WriteLine("1st method");
}
public void Test(int i)
{
Console.WriteLine("2nd method");
}
public void Test(string s)
{
Console.WriteLine("3rd mehtod");
}
public void Test(string s, int i)
{
Console.WriteLine("4th method");
}
public void Test(int i, string s)
{
Console.WriteLine("5th mehtod");
}
static void Main(string[] args)
{
Program p = new Program();
p.Test();
p.Test(100);
p.Test("hai");
p.Test("hello",100);
p.Test(100,"naresh");
Console.ReadLine();
}
Method Overriding:
It is an approach of re-implementing of parent classes method under a child
class with the same name and signature
class Program
{
public void Test()
{
Console.WriteLine("1st parent method");
}
public virtual void show()
{
Abstract class
1 .It is declared with the abstract keyword
2. It have both concrete methods.
3. we cannot object for the abstract class
4. when ever we want to use base class methods and variables in derived class we have
to use inheritance here.
Console.WriteLine("overide abstract");
}
static void Main()
{
absclaas2 obj = new absclaas2();
obj.test();
obj.show();
Console.ReadLine();
Partial class:
1. it is a type of a class that are dividing into properties,methods and events into
multiple source files and at compile time these files are combined into single class.
Sealed class
1. it is type of a class that cannot be inherited and used to restrict the properties
Static class
1. we cannot create object for the static class such class members can be called directly their
class names
1. using System;
2. static class Shape {
3. public static double GetArea(double Width, double height) {
4. return Width * Height;
5. }
6. }
7. class Ractangle {
8. private void GetRactangleArea() {
9. Double Area;
10. Area = Shape.GetArea(10, 5);
11. }
12. }
Constructors:
A special method of the class that will be automatically invoked when an instance of the
class is created is called a constructor. The main use of constructors is to initialize private
fields of the class while creating an instance for the class.
using System;
namespace DefaultConstractor
{
class addition
{
int a, b;
public addition() //default contructor
{
a = 100;
b = 175;
}
public static void Main()
{
addition obj = new addition(); //an object is created , constructor is called
Console.WriteLine(obj.a);
Console.WriteLine(obj.b);
Console.Read();
}
}
Parameterized constructor:
Copy constructor:
If we want to create multiple instances with the same value then we use copy constructors, in a copy
constructor the constructor takes the same class as a parameter to it.
The purpose of a copy constructor is to initialize a new instance to the values of an existing
instance.
public employee(employee emp)
{
name=emp.name;
age=emp.age;
}
Static constructor:
When a constructor is created as static, it will be invoked only once for all of instances of the class and
it is invoked during the creation of the first instance of the class or the first reference to a static
member in the class. A static constructor is used to initialize static fields of the class and to write the
code that needs to be executed only once.
using System;
namespace staticConstractor
{
public class employee
{
static employee() // Static constructor declaration
{
Console.WriteLine("The static constructor ");
}
public static void Salary()
{
Console.WriteLine("The Salary method");
}
}
class details
{
static void Main()
{
Console.WriteLine("----------Static constrctor example by vithal wadje------------------");
Console.WriteLine();
employee.Salary();
Console.ReadLine();
}
}
Virtual methods have an implementation and provide the derived classes with the option of
overriding it.
Abstract methods do not provide an implementation and force the derived classes to
override the method.
Interface
Every abstract method of the interface should be implemented by the child class not to fail.
(mandatory)
namespace InterfaceImplementation
{
interface Interface1
{
void call();
}
interface Interface2:Interface1
{
void show();
}
class Program:Interface2
{
public void call()
{
Console.WriteLine("call method implemented under child class");
}
public void show()
{
Console.WriteLine("show method implemented under child class");
}
}
}
Destructors :
Destructors are used to destruct objects (instances) of classes. A destructor is a special method just
like a constructor, whereas constructors are called when object of a class is created and destructors
are called when object of a class is destroyed. Both of them will have the same name i.e. the name
of the class in which they are defined, but to differentiate between each other we prefix destructor
with a tilde (~) operator.
For Example:
class Test
Test()
//Constructor
Properties:
Property is a member of class using which we can expose values associated with a class to the
outside environment.
A property is a member that provides a flexible mechanism to read, write, or compute the value of a
private field (Variable).
1.
2. class student
3. {
4. public string Myname = "";
5. public int Myage = 0;
6.
7. //Declare a Name Property of type String
8. public string Name
9. {
10. get
11. {
12. return Myname;
13. }
14. set
15. {
16. Myname = value;
17. }
18. }
19.
20. //Declare an Age Property of type int
21.
22. public int Age
23. {
24. get
25. {
26. return Myage;
27. }
28. set
29. {
30. Myage = value;
31. }
32. }
33.
34. public override string ToString() {
35. return ("Name=" + Name + ",Age= " + Age);
36. }
37.
38. }
39.
40. class Program
41. {
42. static void Main(string[] args)
43. {
44. Console.WriteLine("This is Read and Write Propety");
45.
46. // Create a new object for student class
47. student s = new student();
48. Console.WriteLine("Student details:" + s);
49. s.Name = "Nilesh";
50. s.Age = 24;
51. Console.WriteLine("Student details:" + s);
52. //increment the age property
53. s.Age += 1;
54. Console.Write("Student details:" + s);
55. Console.ReadKey();
56.
57. }
58. }
59. }
Enum in c#
An enum is a value type with a set of related named constants often referred to as an enumerator
list.
Enum week
{Sunday,mon,tues,wednes,thurday,Friday,satur,}
1. class Program
2. {
3. enum week
4. {
5. Sunday = 1,
6. Monday,
7. Tuesday,
8. Wednesday,
9. Thursday,
10. Friday,
11. Saturday,
12. }
13. static void Main(string[] args)
14. {
15. Console.WriteLine((int)week.Sunday);
16. Console.WriteLine((int)week.Monday);
17. Console.WriteLine((int)week.Tuesday);
18. Console.WriteLine((int)week.Wednesday);
19. Console.WriteLine((int)week.Thursday);
20. Console.WriteLine((int)week.Friday);
21. Console.WriteLine((int)week.Saturday);
22. Console.ReadKey();
23. }
Indexers:
Indexer allows classes to be used in more intuitive manner. C# introduces a new
concept known as Indexers which are used for treating an object as an array. The
indexers are usually known as smart arrays in C#.
namespace Indexer_example1
{
class Program
{
class IndexerClass
{
private string[] names = new string[10];
public string this[int i]
{
get
{
return names[i];
}
set
{
names[i] = value;
}
}
}
static void Main(string[] args)
{
IndexerClass Team = new IndexerClass();
Team[0] = "Rocky";
Team[1] = "Teena";
Team[2] = "Ana";
Team[3] = "Victoria";
Team[4] = "Yani";
Team[5] = "Mary";
Team[6] = "Gomes";
Team[7] = "Arnold";
Team[8] = "Mike";
Team[9] = "Peter";
for (int i = 0; i < 10; i++)
{
Console.WriteLine(Team[i]);
}
Console.ReadKey();
}
}