You are on page 1of 13

C#

Variables :
A variable is a name given to a storage area that is used to store values of various atatypes.
Each variable in C# needs to have a specific type, which determines the size and layout of
the variable's memory.
using System;
class Program
{
static void Main(string[] args)
{
String message="The value is ";
Int32 val=30;

Console.Write(message+val);
Console.ReadKey();
}
}

Data Types :
The C# language comes with a set of Basic data types. These data types are used to build
values which are used within an application.
Array :
Arrays are used to store multiple values in a single variable.To declare an array, define
the variable type with square brackets( [] ).
using System;
class Program
{
static void Main(string[] args)
{
int[] myNumbers = {5, 1, 8, 9};
Console.WriteLine(myNumbers.Max()); // largest value
Console.WriteLine(myNumbers.Min()); // smallest value
Console.WriteLine(myNumbers.Sum()); // sum of myNumbers
}
}

List :
List<T> class represents the list of objects which can be accessed by index. It comes
under the System.Collection.Generic namespace. List class can be used to create a
collection of different types like integers, strings etc.
// C# program to create a List<T>
using System;
using System.Collections.Generic;
class ListExample {

// Main Method
public static void Main(String[] args)
{
// Creating a List of integers
List<int> firstlist = new List<int>();

// displaying the number


// of elements of List<T>
Console.WriteLine(firstlist.Count);
}
}
Dictionary :
The Dictionary<TKey, TValue> Class in C# is a collection of Keys and Values. It is a
generic collection class in the System.Collection.Generics namespace. The Dictionary
<TKey, TValue> generic class provides a mapping from a set of keys to a set of values.
// C# code to create a Dictionary
using System;
using System.Collections.Generic;
class MyClass {
// Driver code
public static void Main()
{ // Create a new dictionary of strings, with string keys.
Dictionary<string, string> myDict = new Dictionary<string, string>();
// Adding key/value pairs in myDict
myDict.Add("1", "C");
myDict.Add("2", "C++");
myDict.Add("3", "Java");
myDict.Add("4", "Python");
myDict.Add("5", "C#");
myDict.Add("6", "HTML");

// To get count of key/value pairs in myDict


Console.WriteLine("Total key/value pairs"+
" in myDict are : " + myDict.Count);

// Displaying the key/value pairs in myDict


Console.WriteLine("\nThe key/value pairs"+
" in myDict are : ");
foreach(KeyValuePair<string, string> kvp in myDict)
{
Console.WriteLine("Key = {0}, Value = {1}",
kvp.Key, kvp.Value);
}
}
}

Function :
Function is a block of code that has a signature. Function is used to execute statements
specified in the code block. A function consists of the following components:
 Function name: It is a unique name that is used to make Function call.
 Return type: It is used to specify the data type of function return value.
using System;
namespace FunctionExample
{
class Program
{
public void Show(string message)
{
Console.WriteLine("Hello " + message);
}
static void Main(string[] args)
{
Program program = new Program(); // Creating Object
program.Show("Monika Jadhav"); // Calling Function
}
}
}
Function Overloading :

Function Overloading(Method Overloading), multiple methods can have the same


name with different parameters:

using System;

class GFG {

public int Add(int a, int b) {

int sum = a + b;

return sum;

public int Add(int a, int b, int c) {

int sum = a + b + c;

return sum;

public static void Main(String[] args)

GFG ob = new GFG();

int sum1 = ob.Add(1, 2);

Console.WriteLine("sum of the two "+ "integer value : " + sum1);

int sum2 = ob.Add(1, 2, 3);

Console.WriteLine("sum of the three "+ "integer value : " + sum2);

}
Class and Object :
Class and Object are the basic concepts of Object Oriented Programming which revolve
around the real-life entities. A class is a user-defined blueprint or prototype from which
objects are created. Basically, a class combines the fields and methods(member function
which defines actions) into a single unit.

using System;

class MyClass {

static int Sum(int x, int y){

int a = x;

int b = y;

int result = a + b;

return result;

static void Main(string[] args) {

int a = 12;

int b = 23;

int c = Sum(a, b);

Console.WriteLine("The Value of the sum is " + c);

Call By Value and Call By Reference :


Call by value method copies the value of an argument into the formal parameter
of that function. Therefore, changes made to the parameter of the main function
do not affect the argument.

using System;
namespace CallByValue
{
class Program
{
public void Show(int val)
{
val *= val; // Manipulating value
Console.WriteLine("Value inside the show function "+val);

}
static void Main(string[] args)
{
int val = 50;
Program program = new Program(); // Creating Object
Console.WriteLine("Value before calling the function "+val);
program.Show(val); // Calling Function by passing value
Console.WriteLine("Value after calling the function " + val);
}
}
}

Call by reference method copies the address of an argument into the formal
parameter. In this method, the address is used to access the actual argument
used in the function call. It means that changes made in the parameter alter the
passing argument.

using System;
namespace CallByReference
{
class Program
{
// User defined function
public void Show(ref int val)
{
val *= val; // Manipulating value
Console.WriteLine("Value inside the show function "+val);
// No return statement
}
// Main function, execution entry point of the program
static void Main(string[] args)
{
int val = 50;
Program program = new Program(); // Creating Object
Console.WriteLine("Value before calling the function "+val);
program.Show(ref val); // Calling Function by passing reference
Console.WriteLine("Value after calling the function " + val);
}
}
}

String
String class represents the text as a series of Unicode characters and it is defined
in the .NET base class library. The main use of String class is to provide the
properties and methods so that it becomes easy to work with strings.
The C# string manipulation property, and methods that can handle those such
things can be Length property, ToCharArray(), ToLower(), ToUpper(), IndexOf(),
LastIndexOf(), SubString(), CompareTo(), Trim(), TrimEnd(), TrimStart(),
StartsWith(), EndsWith(), Split(), Insert(), Remove(), and Replace().
String Manipulation in C# Length of a string. ...
 Length of a string. ...
 ToCharArray() method. ...
 IndexOf() and LastIndexOf() methods. ...
 ToLower() and ToUpper() methods. ...
 TrimStart(), TrimEnd(), and Trim() methods. ...
 Substring() method. ...
 CompareTo() method. ...
 StartsWith() and EndsWith() methods.

Constructor :
A constructor is a special method of the class which gets automatically invoked
whenever an instance of the class is created. Like methods, a constructor also contains
the collection of instructions that are executed at the time of Object creation. It is used
to assign initial values to the data members of the same class
namespace MyApplication
{
class Car
{
public string model;
public string color;
public int year;

// Create a class constructor with multiple parameters


public Car(string modelName, string modelColor, int modelYear)
{
model = modelName;
color = modelColor;
year = modelYear;
}
static void Main(string[] args)
{
Car Ford = new Car("Mustang", "Red", 1969);
Console.WriteLine(Ford.color + " " + Ford.year + " " + Ford.model);
}
}
}

C# Inheritance :
In C#, inheritance is a process in which one object acquires all the properties and
behaviors of its parent object automatically. ... In C#, the class which inherits the
members of another class is called derived class and the class whose members
are inherited is called base class.
 Derived Class (child) - the class that inherits from another class
 Base Class (parent) - the class being inherited from

class Vehicle // base class (parent)


{
public string brand = "Ford"; // Vehicle field
public void honk() // Vehicle method
{
Console.WriteLine("Tuut, tuut!");
}
}
class Car : Vehicle // derived class (child)
{
public string modelName = "Mustang"; // Car field
}

class Program
{
static void Main(string[] args)
{
Car myCar = new Car();
myCar.honk();
Console.WriteLine(myCar.brand + " " + myCar.modelName);
}
}

Abstract Classes
Abstraction in C# is the process to hide the internal details and showing only the
functionality. The abstract modifier indicates the incomplete implementation. The
keyword abstract is used before the class or method to declare the class or method as
abstract.
usinobj System;

public abstract class MyClass {


public abstract void show();
}
public class Class1 : MyClass
{
public override void show(){
Console.WriteLine("class Class1");
}
}

public class Class2 : MyClass


{
public override void show(){
Console.WriteLine("class Class2");
}
}

public class main_method {


public static void Main()
{
MyClass obj;
obj = new Class1();
obj.show();
obj = new Class2();
obj.show();
}
}
C# Interface :
An interface is a completely "abstract class", which can only contain abstract methods
and properties (with empty bodies):

interface IAnimal

void animalSound(); // interface method (does not have a body)

class Pig : IAnimal

public void animalSound() {

Console.WriteLine("The pig says: wee wee");

class Program

static void Main(string[] args) {

Pig myPig = new Pig(); // Create a Pig object

myPig.animalSound();

You might also like