Working with C# .Net

Ratings:
(4)
Views:921
Banner-Img
  • Share this blog:

 

Implicit type Declaration

 

Screenshot_61

  Int SQL

MBS(SQL);

(OR)

MBS(SQL+””);

Int SQL;                                                                                                var SQL;X var SQL=5000;

SQL=5000;                                                                                           SQL=5000; MGS(SQL +“”);                                                                                  Msg(SQL +“”);

  1. Var is a keyword
  2. Var is a new data type introduced in .net framework 3.5(C#.NET3.0)
  3. Var data type holds any type of data.
  4. Var variables must be initialized in the same line of declaration.
  5. Var data type can be used only in a local scope.
  6. Var variables are not allowed as function arguments.

   Interested in mastering .Net Training? Enroll now for a FREE demo on ".Net Training".

Example on var

Screenshot_24Place a button on the form

Screenshot_24Code for Button1-click

{

Var x = 1500;

Message box. show(X. Get Type().name);

}  

Working with auto-implemented properties

  1. A property is a collection of ser & Get methods with logic.

  Property                                                                                                  Auto Implemented property

Data type var name;                                                   Public data type Property name Public

Data type pname                                                       {

{                                                                                                                  

set”             (no logic) Set{var name - value                                                                          Get:

Get{return var name}                                                                        }

}

Auto implemented property is a  collection of set and get methods without any logic.

Auto implemented property are required for object initializes and collection initializes.  

Working with object initializes

Class em {

Private int double SQL, Eno;

Public string e name;

Public emp(int){}                                                                                 

Emp E1= new Emp(id)

P Emp(string x){}                                                                                 

Emp E2= new Emp(‘Teja’) (double x)…                                                                                           

Emp E3 = NEW Emp(71.3) Emp

E4 = NEW Emp(101,teja)

------- =

NEW Emp()

  1. Object initializers are bit similar to construction
  2. To work with object initializers, Auto- Implemented properties are required.
  3. Syntax to create an object with constructors.

EMP E1=new EMP(101,”Teja”);

4.Syntax to create an object with object Initialize EMP E1=New EMP{Property name=value} Auto Implemented property 5. The main objective of the object Initializes to reduce the number of lines in the source.

Code

Example on object Initializers

Screenshot_24Place a button on the form

Screenshot_24code in GD Class emp

{

Public int eno

{

set;get;

}

Public string ename

{

set; get;

}

Public int SQL

{

set; get;

} }//Emp     

Code for button1click

{

Emp E1 New EMP{Eno=101, Ename=”Bangaru”, SQL=5000};

Message Box.show(E1.Eno+””+E1.Ename+”” E.SQL);

}

  Screenshot_62  

Working with collection Initializer

  1. It is an extension of object initialize

Int x; Int[] x= new int[10]

  1. Collection Initializer works like an array
  2. If the class name is EMP then

EMP[]X=new EMP[10]; //Orcate an array with 10 elements but not with 10 objects If we want 10 objects.

  1. Collection Initializers must be stored in a generic data type is called a list
  2. Generic data types will be indicated with<>
  3.  

Check Out .Net Tutorial

Example

Screenshot_24Place a button

Screenshot_24Code in GD Class EMP { Public int Eno {set; get;} Public string Ename{ set; get;} }//Emp  

Code for button1click

{

List<Emp>x=new List<Emp>

{

New Emp {Eno=101,Ename=”Teja”},

New Emp {Ename=”ravi”},

New Emp {Eno=203},

}//print the data For each

(EMP e1 in x) Message Box. Show (e1.Eno+””+e1.Ename);

}    

Working with Anonymous types

  • While creating anonymous types, CLR generates class automatically for these anonymous types we cannot see the internal problem.
  • These auto-implemented classes are not visible to be programmers.

 

Syntax to create anonymous types

Var x=new

{eno=101,ename=”teja”}

Message box. show(x.eno+””+x. ename);  

Extension Methods

  1. It is the concept of adding new functionality to an existing library(DLL files)
  2. It requires some new methods that can be added to existing data types also.
  3. Extension methods must be written in a. OUTLINE class

Class form1 { Class test{} Inline class }//form1 Class best{} outline class

  1. Extension methods must be declared as static
  2. Extension methods must be PART Of Static Classes Only
  3. Extension methods must contain at least one argument and the first argument must be specified with this keyword.

 

Example on extension methods

Screenshot_24Adding a new method to an existing data type int

Screenshot_24Open windows forms application project

Screenshot_24Place a button

Screenshot_24

Code after form1 class { } }

//form1 Static class test

{

Public static void print(this int1)

{

Message box. show(i+””);

}//print method

}

//test

Screenshot_24

code for button1-click

{

Int x =10;

print();
}    

Methods

  1. When a class needs to be implemented in multiple locations with the same name, then those classes needed to be declared as partial classes.
  2. Partial methods must be part of partial classes.
  3. By default partial methods are private.
  4. Hence to call a partial method, a public method must be written with in the class
  5. The partial method needs to be declared in one class and the same partial method needs to be defined (write the body) in another class.

Partial class test { Partial void print(); } Partial class test { Partial void print() { Message  box. Show(“from print”); } Public void display { PRINT(); }  

Example on partial methods

Screenshot_24place a button on the form

Screenshot_24code in GD Partial class test

{

Partial void print();

//declaration

}

Partial class test

{

Partial void print();

//default private

{

Message  box. Show(“from print”);

}

Public void display

{

PRINT();

}

}//test

Screenshot_24Code for button1-click

{

Test t = new test();

display();
}

  

Working with WPF(windows presentation foundation)

  1. WPF is a new programming concept developed in .NET Frame work 3.5
  2. WPF programming and silver light programming concepts are same.
  3. WPF is used to develop RICH INTERNACTIVE DESK TOP APPLICATIONS.
  4. WPF is used to develop

(a) Shapes (b) Graphics (c) Allows to use brushes (d)Transformations (e)Animations

  1. To develop WPF program, .NET introduced WPF Application project template.
  2. When WDF Application is opened, then by default a window will be displayed which contains 3 modes.

    Screenshot_63  

  1. XAML is a new programming concept introduced for WPF and Silver light programming.
  2. XAML – Extensible application markup language.

XAML is similar to XML Syntactically.  

HTML XML XAML
  1. Web application
To describe the data To develop WPF Applications
  1. Not case sensitive
case sensitive case sensitive
  1. Error free Language
Structured Language Structured Language
  1. Collection of pre defined tags
Collection of  user defined tags Collection of  pre defined tags

   

Syntax of xml

<peers>Screenshot_24 Root element

<student>Screenshot_24entity <sno>101

</sno >        sub entities <sno> taja</sn>           (or)

Attributes

</stu>

<faculty <FN>Ravi</FN>

<SAL>3800</SAL>

</Faculty>

</peers>  

Syntax of XAML

<Window>

<GRID>

<Button>------< /Button >

<Label>------< /Label >

< /GRID >

</Window>  

Rules to be followed in XAML or XML

  1. Every opened tag must be closed.
  2. The document must contain a root element.
  3. The structure of the tags need to be maintained
  4. It is case sensitive.

 

EX-1:- A WPF APPLICATION TO DISPLAY COMPANY TITLE WITH MUTTIPLE TEXT COLORS

Screenshot_24Open WPF Application project.

Screenshot_24Write the following with in Grid tag.

< Grid >

<Text block text=”MICROSOFT”>

<Font size=”40” Font family=”Arial”>

<Text block. Fore ground>

<Linear Gradient brush>

< Gradient stop offset=”.1”color=”Red”/>

< Gradient stop offset=”.3”color=”yellow”/>

< Gradient stop offset=”.5”color=”blue”/>

< Gradient stop offset=”.8”color=”Green”/>

< / Linear gradient brush>

< / Text block. Fore ground>

< / Text block >

< / Grid >  

Obs:-

Screenshot_24offset value must be in between 0 to1.

Screenshot_24offset indicates the color starting point.

Screenshot_24<Grid>is called a container.  

Working with containers

Screenshot_24WPF Supports two types of containers GRID Container

Screenshot_64  

Example on Grid with multiple rows and columns

Screenshot_24Open WPF

Screenshot_24Write the following within Grid tag

<Grid>

<Grid. Row Definitions>

<Row Definition Height=”150”>

</ Row Definitions>

</Grid. Row Definitions>

<Grid. Column Definitions>

< Column Definitions width=”150”>

< /Column Definitions>

</Grid. Column Definitions>

<Button Grid. Row =”o” Grid.  Column=”o”>Click1< /Button >

< Button Grid. Row =”o” Grid. Column=”1”>Click1< /Button >

</Grid>

</Grid. Row Definitions>

<Row Definitions Height=”150”>

</ Row Definitions >  

Working with brushes

WPF supports 4 types of brushes

  Solid color Brush

Screenshot_65

Linear Gradient Brush

Screenshot_66  

Radial gradient Brush

Screenshot_67  

Image Brush

Technology  

Examples

Open WPF Application project Remove Grid tag and type canvas Type the following within canvas tag.

<canvas>

<Ellipse width=”200”Height=”200” Stroke=”Red” Stroke Think news=”5” Canvas. top=”20”canvas.left=”40”>

< Ellipse. Fill>

<solid color brush color=”blue”>

</solid color brush>

< /Ellipse>

< Elliple. Fill>

< /Convas>

< Ellipse. Fill>

< Linear Gradient brush>

< Gradient stop offset =”2” color=”Blue”/ >

< Gradient stop offset =”4” color=”yellow”/ >

< Gradient stop offset =”6” color=”Red”/ >

< Gradient stop offset =”7” color=”Green”/ >

< Gradient stop offset =”9” color=”Orange”/ >

< /Linear Gradient brush>

</ Ellipse. Fill>

(or) <Radial gradient Brush>

</Radia L Gradient Brush>  

Example on Image Brush

Screenshot_24open WPF Application project

Screenshot_24ViewàSolution ExploreràRt click on project Name

Screenshot_24AddScreenshot_24Existing ItemScreenshot_24Choose an image Type the following within grid tag

<Grid>

<Text block Text=”Microsoft” Font family=”Arial” Font size=”200”>

<Text block. fore ground>

<Image brush Image source=”Sybset.jpg”>

</Image brush>

</Text block. fore ground>

</text block>

</Grid>    

Working with wpf transformations

Screenshot_24Trans formation is the concept of moving  an object in a particular angle

Screenshot_24WPF Supports 3 types of transformations.  

Rotate transformation

Screenshot_68  

Skew Transformation  

Screenshot_69

  Scale transformation  

Screenshot_70  

Syntax of transformations

<control>

< control. Render Transform>

<Rotate Transform . . . . ./>

</control. Render Transform >

</control>  

Obs Transformations must be the part of render transform tag.  

Example on Transformations

Screenshot_24Open WPF Application project

Screenshot_24Replace Grid tag with canvas

< canvas>

<Button width=”100”Height=”60” Convas. Top=”30” Convas. Left=”20”>

click

<Button. Render. Transform>

<Rotate Transform  Angle=”45”>

<Rotate Transform>

</Button. Render. Transform>

</Button>

</ Convas >  

The syntax for skew Transformation

<Skew Transform  Angle x =”50”>

Angle y =”20”></skew Transform >  

Syntax for scale Transformation

<canvas> <Button width=”100”Height=”60” > <Scale Transform scale x=”2” scale y=”2”></ scale Transform >

  • Moring an an object in a particular angle continuously is called as “Animation”.
  • Generally in c#, to develop animated programs, Timer control is required
  • In WPF, a Story Board tag is introduced, which works similarly to a timer control.
  • Storyboard tag must be the part of beginning storyboard tag in wpf events that are called triggers.

 

Syntax to write animated programs

<canvas>

<Control---------->

<Controls. Triggers>

<Event  Trigger------>

<Begin story board>

<Story board>

<Double Animation -------->

</Story board>

</Begin Story board >

</Control. Triggers>

</Control>

</Control>  

Example:- Developing line animation

àOpen WPF Application project. Replace Grid with canvas tag top

Screenshot_71

<canvas>

Ellipse /

<Line x1=”10” y1=”10”x2=”50”y2=”10” Stroke=”RED” Stroke Thickness =”5”> X:Name =”abc”>

Line object name

<Line. Triggers>

<Event Triggers  Routed Event= “Convas. Loaded”>

<Begin story board>

<Story board >

<Double Animation Story board . Target name=”abc” Story board Target property=”(canvas. left)” From = “o” To =”800”>

Auto reverse =”True” Repeat Behavior=”Forever” Duration=”00:00:03”>

</Double animation>

</ Story board >

</Begin story board>

</Event Trigger>

</Line. Triggers>

</Line>

</Canvas>  

Working with LINQ[Language integrated Query]

C#.NET                                                                            

Oracle Int[]x=new int[]                                                          

Works on tables {7,5,8,2,7,6,1}                                                               

All the records From I in x                                                                      

select I; Select*from emp: select I;

Select*from emp: Where SQL>1000; >5 From i in x where i>5 Select i;

Sorting                                                                            

SQL Ascending order

From I in x  order by I                                                

select * from emp Ascending select I;                                                     

order by SQL ASC;

  1. LINQ is bit similar to SQL Queries
  2. SQL is used to retrieve the data from the data bases
  3. LINQ is used to retrieve the data from .NET objects.
  4. LINQ Queries works on reference types only.
  5. Generally, the LINQ Query starts with a keyword called from and ends with a select keyword
  6. LINQ Supports Aggregate functions which are in traduced as extension methods.

SUM(),Average(),MAX(),Min() etc.  

Example on LINQ

Screenshot_24Open windows form application project

Screenshot_24place a button

Screenshot_24Code for Button 1-click

{

Int[]x=new int[]{4,6,2,6,8,9,1,3};

Strings=””;

//print the values which are>5 Var v=from I in x where i>5 select I;

//Sort the numbers in ascending order.

Var v = from I in x order by I ascending select I;

//printing distinct numbers

Var v=(from in x select i)Distinct();

//printing the sum of the numbers

Int total=x.sum();

Message box. show(total+””);

//Printing the arrange of the numbers

Double total =x. average();

Message box. show(total+””);

For each(int k in v) S=s+k+””;

Message box. show(s);  

Screenshot_24Place one more button(button2)

Screenshot_24code for button2-click

{

Int[]x=new int[]{4,6,2,6,8,9,1,3};

Strings=””;

//print the values which are>5 Var v=from I in x where i>5 select I;

//Sort the numbers in ascending order.

Var v = from I in x order by I ascending select I;

//printing distinct numbers

Var v=(from in x select i)Distinct();

//printing the sum of the numbers

Int total=x.sum();

Message box. show(total+””);

//Printing the arrange of the numbers

Double total =x. average();

Message box. show(total+””);

For each(int k in v) S=s+k+””;

Message box. show(s);  

Example: LINQ With a data set

Open windows form application project place a button and a data grid view Using system.

Data. Sql client;

Code for button1-click

{

Sql connection cn = new sql connection (user id=SQ;

Password=;database=north wind; Server = Local host” );

SQL Data Adapter da=new Sql data Adapter (“Select*from products”,cn);

Data set ds= new  dataset(); Da. fill(ds, ”d”); Data table dt=ds. Tables[“d”];

Innumerable<data Row>ie=dt. as Enumerable();

IQuery able<data row>dr=i.e. As Query able();

Dr=from I in dr select I;

Dr=from I in dr where i[“Product name”]. To string()==”chai” select I;

Dr=from I in dr where int. parle(i[“product id”]. To string())<5 select I;

Data grid viewl. data sources= dr. copy to data table();

}          

You liked the article?

Like : 0

Vote for difficulty

Current difficulty (Avg): Medium

Recommended Courses

1/15

About Author
Authorlogo
Name
TekSlate
Author Bio

TekSlate is the best online training provider in delivering world-class IT skills to individuals and corporates from all parts of the globe. We are proven experts in accumulating every need of an IT skills upgrade aspirant and have delivered excellent services. We aim to bring you all the essentials to learn and master new technologies in the market with our articles, blogs, and videos. Build your career success with us, enhancing most in-demand skills in the market.


Stay Updated


Get stories of change makers and innovators from the startup ecosystem in your inbox